[英]List of BLE devices being duplicated
Hi so currently I'm working on a Ionic project called BLE Scanner. 嗨,所以我目前正在从事一个名为BLE Scanner的离子项目。 I was struggling to find a solution and finally managed to connect to those devices.
我一直在努力寻找解决方案,最后设法连接到这些设备。 Here are the code that I found on the internet:
这是我在互联网上找到的代码:
home.ts (just ignore the DetailPage) 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
});
}
}
and home.html 和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>
So here comes a problem: The list of BLE devices is being duplicated because of the RSSIs keep fluctuating and I don't know how to show each device only once while its RSSI is being update continuously. 因此出现了一个问题:由于RSSI不断波动,BLE设备的列表正在重复,并且我不知道如何在不断更新其RSSI的情况下仅显示每个设备一次。
Here is my current app, as you can see there's only 1 device called RADUSB but being shown multiple times 这是我当前的应用,如您所见,只有1个名为RADUSB的设备,但多次显示
Please help me figure out the way to solve this problem. 请帮助我找出解决此问题的方法。 I appreciate every advice.
我感谢每一个建议。 Thank you!
谢谢!
You are inserting into the list in startScan
. 您将插入
startScan
的列表中。 Add a check to see if it already contains the device. 添加检查以查看其是否已包含该设备。
this.ble.startScan([]).subscribe(
device => {
if(this.devices.findIndex((dev:any)=>dev.id===device.id)==-1)
this.devices.push(device);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.