2017-02-18 10 views
0

私はIonic2アプリケーションでBluetoothシリアルを使用しています。接続可能なデバイスをリストしたいと思います。Bluetooth serial in ionic2、デバイスのリストなし

これは.tsファイルにありますが、動作しません。私はブルートゥースを有効にすることができますが、デバイスが検出されたかリストすることができるかどうかを調べることはできません。

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

@Component({ 
    selector: 'page-page1', 
    templateUrl: 'page1.html' 
}) 
export class Page1 { 
public working:string; 
public var2: string ; 
public lists = []; 

    constructor(private alertCtrl: AlertController, public platform: Platform, public navCtrl: NavController) { 
    platform.ready().then(() => { 
     BluetoothSerial.isConnected() 
     .then(() => { 
      console.log('is connected'); 
     }, (err) => { 
      console.log(' not connected') 
     }) 
     }); 
     } 


     enableBluetooth(){ 
     BluetoothSerial.enable() 
     } 
     discoverBluetooth(){ 
     /* BluetoothSerial.setDeviceDiscoveredListener(function(device) { 
      console.log('Found: '+device.id); 
     });*/ 
     } 
     unpairedBluetooth(){ 

     BluetoothSerial.discoverUnpaired().then(function(devices) { 
       devices.forEach(function(device) { 
     console.log(device.id)}); 
      }) 

     } 


    listDevices(){ 

      BluetoothSerial.isEnabled().then((data)=> { 
       console.log(data); 
      BluetoothSerial.list().then((allDevices) => { 
      this.lists = allDevices; 

       let result = []; 
      for (var key in this.lists) {   
       result.push(key);} 

      }) 




})}} 

.htmlファイルでは、いくつかのリストを試してみましたが、これはうまくいきません。

<ion-content padding> 


    <ion-buttons> 
<button ion-button (click) = "enableBluetooth()">Enable!</button> 
</ion-buttons> 
<ion-buttons> 
<button ion-button (click) = "listDevices()">List devices</button> 
</ion-buttons> 
Lijst1 
<ion-list> 
     <ion-item *ngFor="let list of lists">{{list.devices}}</ion-item> 
    </ion-list> 
    Lijst2 
    <ion-list> 
     <ion-item *ngFor="let list of lists">{{list.device}}</ion-item> 
    </ion-list> 
    Lijst3 
    <ion-list> 
     <ion-item *ngFor="let list of lists">{{list.allDevices}}</ion-item> 
    </ion-list> 
    Lijst4 
    <ion-list> 
     <ion-item *ngFor="let key of lists">{{list.allDevices}}</ion-item> 
    </ion-list> 
    Lijst5 
    <ion-list> 
     <ion-item *ngFor="let key of lists">{{key}}</ion-item> 
    </ion-list> 
    Lijst 
    <ion-list> 
     <ion-item *ngFor="let list of lists">{{list}}</ion-item> 
    </ion-list> 
<ion-buttons> 
<button ion-button (click) = "unpairedBluetooth()">Unpair</button> 
</ion-buttons> 
</ion-content> 

答えて

0

あなたはこの方法を試すことができます。

BluetoothSerial.list(function(result){ 
    console.log(result); 
}, 
function(failure){ 
    console.log(failure); 
}); 

はまだ見ていない場合は、プラグインのREADMEと例を見てみましょうお勧めします。それは多くを助ける。 https://github.com/don/BluetoothSerial

関連する問題