离子2 BluetoothSerial不会工作

时间:2017-03-05 19:54:59

标签: ionic2

我想编辑一个程序,通过蓝牙发送信息,我的源代码就在这里,即使我在我的设备上试用它,它仍然给我“无法启用蓝牙”。

import { Injectable } from '@angular/core';
import { BluetoothSerial } from 'ionic-native';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

/*
Generated class for the BluetoothTracker provider.

 See https://angular.io/docs/ts/latest/guide/dependency-injection.html
 for more info on providers and Angular 2 DI.
 */
 @Injectable()
 export class BluetoothTracker {

     constructor(public http: Http) {
         console.log('Hello BluetoothTracker Provider');
     }

     startConnection(){
        BluetoothSerial.isEnabled()
        .then(
             ( succes ) => { alert( "bluetooth enabled." ); },
             ( err ) => {
                BluetoothSerial.enable()
               .then(
               ( succes ) => { alert( "enabling bluetooth worked. " ); },
               ( err ) => { alert( "can't enable bluetooth. " ); }
              );
        }
      );
   }

 }

1 个答案:

答案 0 :(得分:0)

这是解决方案,我必须使用功能BluetoothSerial.list(),来选择我的设备的蓝牙接口,从那里我想发送信息。

  /** Select bluetooth interface from where i should be connected **/
BluetoothSerial.list().then(
  ( devices ) => { JSON.stringify( devices ); },
  ( err ) => { alert( " cant list devices" + err ); }
);