Всем привет! Вот код из https://github.com/mplaza/react-native-ble-advertiser, они показывают мне, как обнаруживать новые устройства, но не показывают, как удалить, когда устройства go далеко. Кто-нибудь может мне помочь! Я новичок ie в реакции на родной.
addDevice(_uuid, _name, _rssi, _date) {
let index = -1;
for(let i=0; i< this.state.devicesFound.length; i++){
if (this.state.devicesFound[i].uuid == _uuid) {
index = i;
}
}
if (index<0) {
let dev = {uuid:_uuid, name:_name, rssi:_rssi, start:_date, end:_date};
this.setState({
devicesFound: update(this.state.devicesFound,
{$push: [dev]}
)
});
} else {
//let dev = this.state.devicesFound[index];
//const newList = this.state.devicesFound.splice(index, 1);
const itemIndex = index;
this.setState({
devicesFound: update(this.state.devicesFound,
{[itemIndex]: {end: {$set: _date}, rssi: {$set: _rssi || this.state.devicesFound[itemIndex].rssi }}}
)
});
}
}
start() {
console.log(this.state.uuid, "Registering Listener");
const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser);
this.onDeviceFound = eventEmitter.addListener('onDeviceFound', (event) => {
//console.log('onDeviceFound', event);
if (event.serviceUuids) {
for(let i=0; i< event.serviceUuids.length; i++){
if (event.serviceUuids[i] && event.serviceUuids[i].endsWith('00'))
this.addDevice(event.serviceUuids[i], event.deviceName, event.deviceAddress, event.rssi, new Date())
}
}
});
console.log(this.state.uuid, "Starting Advertising");
BLEAdvertiser.broadcast(this.state.uuid, MANUF_DATA, {
advertiseMode: BLEAdvertiser.ADVERTISE_MODE_BALANCED,
txPowerLevel: BLEAdvertiser.ADVERTISE_TX_POWER_MEDIUM,
connectable: false,
includeDeviceName: false, includeTxPowerLevel: false })
.then(sucess => console.log(this.state.uuid, "Adv Successful", sucess))
.catch(error => console.log(this.state.uuid, "Adv Error", error));
console.log(this.state.uuid, "Starting Scanner");
BLEAdvertiser.scan(MANUF_DATA, {scanMode: BLEAdvertiser.SCAN_MODE_LOW_LATENCY})
.then(sucess => console.log(this.state.uuid, "Scan Successful", sucess))
.catch(error => console.log(this.state.uuid, "Scan Error", error));
this.setState({
isLogging: true,
});
}