正在复制的 BLE 设备列表
List of BLE devices being duplicated
您好,目前我正在开发一个名为 BLE Scanner 的 Ionic 项目。我一直在努力寻找解决方案,并最终设法连接到这些设备。以下是我在网上找到的代码:
home.ts(忽略DetailPage)
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BLE } from '@ionic-native/ble';
import { DevicePage } from '../device/device';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
devices: any[];
isScanning:boolean;
constructor(public navCtrl: NavController,
private ble: BLE) {
this.devices=[];
this.isScanning =false;
}
startScanning(){
console.log("Scanning ..");
this.devices=[];
this.isScanning=true;
this.ble.startScan([]).subscribe(
device => {
this.devices.push(device);
}
);
setTimeout(() => {
this.ble.stopScan().then(() => {
console.log("Scanning has stopped");
console.log(JSON.stringify(this.devices))
this.isScanning = false;
});
}, 5000);
}
connectToDevice(device){
console.log("Connect To Device");
console.log(JSON.stringify(this.devices));
this.navCtrl.push(DevicePage, {
device: device
});
}
}
和home.html
<ion-content>
<ion-list inset>
<ion-item-sliding *ngFor="let device of devices">
<button ion-item (click)="connectToDevice(device)">
<h2>Device: {{ device.name }}</h2>
<p>ID: {{ device.id }}</p>
<p>RRSI: {{ device.rssi }}</p>
</button>
</ion-item-sliding>
</ion-list>
<ion-spinner *ngIf="isScanning==true" name="circles"></ion-spinner>
</ion-content>
所以问题来了:由于RSSI不断波动,BLE设备列表被复制,我不知道如何在RSSI不断更新时只显示一次每个设备。
这是我当前的应用程序,您可以看到只有 1 个名为 RADUSB 的设备被多次显示
my current app
请帮我想办法解决这个问题。我感谢每一个建议。谢谢!
您正在 startScan
中插入列表。添加检查以查看它是否已包含该设备。
this.ble.startScan([]).subscribe(
device => {
if(this.devices.findIndex((dev:any)=>dev.id===device.id)==-1)
this.devices.push(device);
});
您好,目前我正在开发一个名为 BLE Scanner 的 Ionic 项目。我一直在努力寻找解决方案,并最终设法连接到这些设备。以下是我在网上找到的代码:
home.ts(忽略DetailPage)
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BLE } from '@ionic-native/ble';
import { DevicePage } from '../device/device';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
devices: any[];
isScanning:boolean;
constructor(public navCtrl: NavController,
private ble: BLE) {
this.devices=[];
this.isScanning =false;
}
startScanning(){
console.log("Scanning ..");
this.devices=[];
this.isScanning=true;
this.ble.startScan([]).subscribe(
device => {
this.devices.push(device);
}
);
setTimeout(() => {
this.ble.stopScan().then(() => {
console.log("Scanning has stopped");
console.log(JSON.stringify(this.devices))
this.isScanning = false;
});
}, 5000);
}
connectToDevice(device){
console.log("Connect To Device");
console.log(JSON.stringify(this.devices));
this.navCtrl.push(DevicePage, {
device: device
});
}
}
和home.html
<ion-content>
<ion-list inset>
<ion-item-sliding *ngFor="let device of devices">
<button ion-item (click)="connectToDevice(device)">
<h2>Device: {{ device.name }}</h2>
<p>ID: {{ device.id }}</p>
<p>RRSI: {{ device.rssi }}</p>
</button>
</ion-item-sliding>
</ion-list>
<ion-spinner *ngIf="isScanning==true" name="circles"></ion-spinner>
</ion-content>
所以问题来了:由于RSSI不断波动,BLE设备列表被复制,我不知道如何在RSSI不断更新时只显示一次每个设备。
这是我当前的应用程序,您可以看到只有 1 个名为 RADUSB 的设备被多次显示
my current app
请帮我想办法解决这个问题。我感谢每一个建议。谢谢!
您正在 startScan
中插入列表。添加检查以查看它是否已包含该设备。
this.ble.startScan([]).subscribe(
device => {
if(this.devices.findIndex((dev:any)=>dev.id===device.id)==-1)
this.devices.push(device);
});