【问题标题】:Manage a return's promise from BluethoothSerial ionic 2管理来自 BluethoothSerial ionic 2 的退货承诺
【发布时间】:2017-08-19 04:44:22
【问题描述】:

我正在编写一个代码来检查蓝牙连接是否启用。所以我这样做了:

$ ionic plugin add cordova-plugin-bluetooth-serial
$ npm install --save @ionic-native/bluetooth-serial

在我的构造中我有这个:

construnct(....
  private bluetoothSerial: BluetoothSerial,
  .....){..

现在来了

construnct(....
  private bluetoothSerial: BluetoothSerial,
  .....){..
this.bluetoothSerial.isEnabled(/*here*/);
 ...}

我需要管理蓝牙是否启用?

我创建了一个布尔变量,我想将其分配为真或假,这取决于蓝牙是否启用。但是怎么做呢?

参考:

bluetoothSerial.isEnabled(
    function() {
        console.log("Bluetooth is enabled");
        //myboolflag=true; dosen't work!!!
    },
    function() {
        console.log("Bluetooth is *not* enabled");
    }
);

here

【问题讨论】:

    标签: angular cordova typescript ionic2 ionic3


    【解决方案1】:

    你应该像这样使用箭头函数:

    bluetoothSerial.isEnabled(
        () => {
            console.log("Bluetooth is enabled");
            this.myboolflag = true; // Should work now!!!
        },
        () => {
            console.log("Bluetooth is *not* enabled");
        }
    );
    

    通过使用箭头函数,this 属性不会被覆盖,仍会引用组件实例。

    【讨论】:

    • 它给了我这个错误提供的参数与调用目标的任何签名都不匹配。
    • 它是否提供了更多详细信息?或者isEnabled方法应该怎么调用?
    • 伙计,我遇到了这个错误:- 内联模板:0:43 原因是:没有 BluetoothSerial 的提供者!
    • 请尝试将 BluetoothSerial 包含在 NgModule 的提供程序数组中
    • 我做到了:providers: [........, BluetoothSerial,........] ,在 appModule.ts
    猜你喜欢
    • 2016-06-01
    • 2019-11-09
    • 2016-10-25
    • 2023-03-09
    • 2015-07-17
    • 1970-01-01
    • 2017-01-22
    • 2012-09-19
    • 1970-01-01
    相关资源
    最近更新 更多