我在我的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>
您可以这样尝试:
BluetoothSerial.list(function(result){
console.log(result);
},
function(failure){
console.log(failure);
});
建议您查看插件的读数和示例,如果您尚未查看。这将有助于很多。https://github.com/don/bluetoothserial