【问题标题】:Bluetooth Serial Ionic 2蓝牙串行离子 2
【发布时间】:2017-07-20 15:52:38
【问题描述】:

我正在尝试构建一个简单的蓝牙串行应用程序来连接到我的 arduino。我正在使用 ionic2 制作 android app。现在,我要做的就是列出所有可用的蓝牙设备。以下是我的代码:

import { Component } from '@angular/core';
import { BluetoothSerial } from 'ionic-native';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

public working:string;
public var2: string ;

bluetoothSerial.isEnabled(
    function() {
        this.working= "Bluetooth is enabled";
    },
    function() {
        this.working="Bluetooth is *not* enabled";
    }
);

public lists = [];

bluetoothSerial.discoverUnpaired(function(devices) {
    this.lists.push(devices);
}, function(){  this.var2 = 'could not find any bluetooth device';});

  constructor() {  }

}

每当我执行离子服务时,我都会犯很多错误,主要是因为无法识别蓝牙(缺少功能实现)。它也不允许我构建应用程序。

请帮忙。

非常感谢

【问题讨论】:

    标签: ionic-framework bluetooth ionic2 android-bluetooth


    【解决方案1】:

    查看文档

    isEnabled()

    平台:Android iOS Windows Phone

    报告蓝牙是否启用

    返回:Promise 返回一个承诺

    几件事。你不能这样调用你的方法。 你应该大写你的BluetoothSerial 你应该把它放在一个函数中

    import { Component } from '@angular/core';
    import { BluetoothSerial } from 'ionic-native';
    import { NavController } from 'ionic-angular';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
    
    public working:string;
    public var2: string ;
    public lists = [];
    
    constructor(){
       this.getAlBluetoothDevices();
    }
    
    // put BluetoothSerial inside a function, can't be called different
    getAllBluetoothDevices(){
        // async so keep everything in this method
        BluetoothSerial.isEnabled().then((data)=> {
            // not sure of returning value, probably a boolean
            console.log("dont know what it returns"+data);
    
            // returns all the available devices, not just the unpaired ones
            BluetoothSerial.list().then((allDevices) => {
                // set the list to returned value
                this.lists = allDevices;
                if(!this.lists.length > 0){
                   this.var2 = "could not find any bluetooth devices";
                }
            });
        });
       }
    }
    

    }

    【讨论】:

    • 非常感谢兄弟!!!我非常感谢您的时间和帮助。我会尝试您给定的代码,并会在更新时返回给您。谢谢
    • 当然,我自己没有使用过 tis 库。如果您有空闲时间,请查看 ionic2 文档 :) ionicframework.com/docs/v2/native/bluetooth-serial
    • 嘿伙计,您的代码通过从 constrcutor () 中删除 this.getAlBluetoothDevices(); 完美运行,我不知道为什么会出现问题。此外 BS.list 仅列出已配对的设备而不是未配对的设备(BS 文档中也提到了这一点)。但是,它的工作伙伴......接下来我要尝试连接到一个 arduino 设备,看看它是否有效。
    • @AmritSohal 很高兴听到。我自己从未使用过它,但离子文档看起来像 .list() 全部返回。我的坏:)
    • @AmritSohal 请接受答案或写一个更好的答案来结束问题。
    【解决方案2】:

    以下代码用于扫描蓝牙设备

        startScanning(){
            this.isScanning =true;
              this.bluetoothSerial.isEnabled().then(()=>{
                this.bluetoothSerial.discoverUnpaired().then((allDevices)=>{
                    this.devices=allDevices;
                    this.isScanning =false;
                });
            });
    

    此代码用于连接设备

    onDeviceReady(){
      let device = this.navParams.get('device');
    
    
            this.bluetoothSerial.connect(device.id).subscribe((data)=>{
                let alert = this.alertCtrl.create({
                    title: 'Bluetooth',
                    subTitle: data,
                    buttons: ['ok']
                    });
                    alert.present();
            },error=>{
                let alert = this.alertCtrl.create({
                    title: 'Bluetooth',
                    subTitle: error,
                    buttons: ['ok']
                    });
                    alert.present();
            });
    
      }
    

    注意:它对我有用

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 2021-05-19
      • 2017-02-09
      • 2017-11-19
      • 2023-03-21
      • 2013-03-10
      • 2017-06-23
      • 2016-01-28
      • 1970-01-01
      相关资源
      最近更新 更多