noble
noble copied to clipboard
Gets stuck in the function discoverSomeServicesAndCharacteristicsAsync without any errors
Problem
After about 0 to 10 minutes of scanning it get stuck in the function discoverSomeServicesAndCharacteristicsAsync. Any suggestions?
Scanning code
const noble = require('@abandonware/noble/with-custom-binding')({extended: true});
noble.on('stateChange', async (state) => {
console.log('stateChange received: '+state);
if (state === 'poweredOn') {
console.log('poweredOn received');
noble.startScanningAsync([],true);
}
});
noble.on('warning', async () => {
console.log('warning');
});
noble.on('disconnect', async () => {
console.log('disconnect');
});
noble.once('rssiUpdate', async () => {
console.log('rssiUpdate');
});
noble.on('discover', async (peripheral) => {
console.log('discovered: '+peripheral.address + ' rssi: '+peripheral.rssi);
if (peripheral.address === 'xx:xx:xx:xx:xx:aa') {
console.log('tag found')
console.log("state: ", peripheral.state)
printDetailsBattery(peripheral);
}
});
async function printDetailsBattery(peripheral) {
if (peripheral.state !== 'disconnected') {
return;
}
//noble.reset();
console.log("stopScanningAsync")
await noble.stopScanningAsync()
console.log("connectAsync")
await peripheral.connectAsync();
// 180f = battery service
// 2a19 = battery level
console.log("discoverSomeServicesAndCharacteristicsAsync")
const {characteristics} = await peripheral.discoverSomeServicesAndCharacteristicsAsync(['180f'], ['2a19']);
console.log("readAsync")
const batteryLevel = (await characteristics[0].readAsync())[0];
console.log(`battery level: ${peripheral.address}, ${batteryLevel}%`);
console.log("disconnectAsync")
await peripheral.disconnectAsync();
console.log("startScanningAsync")
await noble.startScanningAsync([],true);
}
Last output before getting stuck
discovered: xx:xx:xx:xx:xx:88 rssi: -85
discovered: xx:xx:xx:xx:xx:77 rssi: -90
discovered: xx:xx:xx:xx:xx:66 rssi: -72
discovered: xx:xx:xx:xx:xx:55 rssi: -72
discovered: xx:xx:xx:xx:xx:44 rssi: -62
discovered: xx:xx:xx:xx:xx:33 rssi: -97
discovered: xx:xx:xx:xx:xx:22 rssi: -76
discovered: xx:xx:xx:xx:xx:11 rssi: -98
discovered: xx:xx:xx:xx:xx:aa rssi: -59
tag found
state: disconnected
stopScanningAsync
discovered: xx:xx:xx:xx:xx:ab rssi: -93
connectAsync
discoverSomeServicesAndCharacteristicsAsync
I had a similar issue but it wasn't finding my characteristic at all and worked around it by enumerating all of them and filtering after
const characteristics = await services[0].discoverCharacteristicsAsync();
const tempCharacteristic = characteristics.find(c => c.uuid === TEMP_CHARACTERISTIC_UUID);