Possible to re-connect to multiple devices?
Prerequisites
Please answer the following questions for yourself before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.
- [*] I am running the latest version
- [*] I checked the documentation and found no answer
- [*] I checked to make sure that this issue has not already been filed
- [*] I'm sure that question is related to the library itself and not Bluetooth Low Energy or Classic in general. If that so, please post your question on StackOverflow or on our Gitter channel.
Question
Please describe the question.
In my current use case I am using the library to connect to multiple devices. Suppose after some time 2 of the connected devices disconnect due to various reason like low battery, out of range, etc. Suppose let's consider 2 devices (could be scaled to more than 2 ideally) out of the 4 disconnect as they went out of range and both of them come back within the server's range at the same time. As of now only one of the disconnected devices reconnect and the other does not connect. I know that the device which reconnects is the last one which gets disconnected. So the scan which I use to reconnect now only scans for the last disconnected device and the device scan for the one which disconnected first is lost and never connects back. Is there some functionality from the library which I can use in such a scenario so that both the disconnected device reconnects.
Some code snippets of how I am using the library as of now to detect disconnection and reconnect back
// my scan function
const scanAndConnect = deviceData => {
bleManager.startDeviceScan(null, null, (error, device) => {
if (error) {
console.log(error);
return;
}
if (device.id === deviceData.address) {
bleManager.stopDeviceScan();
connectToDevice(device.id);
}
});
};
// connect to ble device and monitor a characteristic for a service
connectToDevice = (deviceID, bluetooth_id) => {
bleManager.connectToDevice(deviceID).then(device => {
if (device) {
if (!userDisconnect) {
bleManager.onDeviceDisconnected(deviceID, (error, device) => handleDisconnection(error, device, data));
}
device.discoverAllServicesAndCharacteristics().then(device => {
device.characteristicsForService(serviceUUID).then(chars => {
chars.forEach(char => {
if (char.uuid === characteristicUUID) {
char.monitor((error, char) => {
if (error) {
if (error.errorCode === 201) {
if (!userDisconnect) {
scanAndConnect(data);
} // if user disconnects ignore the disconnection
return;
}
}
});
}
});
});
});
}
});
};
// my handle disconnection method to re-fire scanning and connection
const handleDisconnection = (error, device, data) => {
if (error) {
return;
}
scanAndConnect(data);
};
Do I need to use async or promise structure to take care of such a situation since I know JS is single threaded.
Any sort of help would be appreciated. Thanks!