react-native-ble-plx
react-native-ble-plx copied to clipboard
Bug Report
Expected Behavior
When app or device loose connection I should be hable to see it in device scan or if I don't see it, be able to disconnect to it.
Current Behavior
Sometimes, it looks like I loose connection. I don't see my device when performing startDeviceScan(). But I can't disconnect. Result of connectedDevices([serviceUUID]) is empty and cancelDeviceConnection(deviceId) return an error saying I'm not connected to device. My device don't change it's mac so I'm certain that my deviceId is correct.
The only way to get out of this situation I found is to shutdown bluetooth on my phone et put it back on. After that I'm able to see my device with startDeviceScan().
Steps to Reproduce
It's not something I can reproduce with precise steps but it goes on "disconnected" state when I don't do anything for several minute or sometimes when I reload with metro.
And I can only know I'm on this state by trying to scan or disconnect and it doesn't work.
Context
- Library version: 2.0.3
- react-native: 0.68.0,
- Platform: Android
- JS logs:
await this.getBleManager().connectedDevices([serviceUUID]) => []
await this.getBleManager().devices([deviceID]) => []
await this.getBleManager().cancelDeviceConnection(deviceID); => [BleError: Device CE:A5:1C:75:99:EE is not connected]
this.getBleManager().startDeviceScan(null, null, (error, device) => {console.log(device.id)}) => see all other device but mine
Going through all the issue I think my solution might be in implementing restoreStateIdentifier
and restoreStateFunction
when I create the BleManager. Because the issue seems to occure when the object get lost with metro reloading.
But I can't find a proper example of implementation and for me the doc is not clear enough to understand how to set up the implementation.
My question are :
-
restoreStateIdentifier
: is it something I should get from the BleManager ? Is it something I set up ? If so, should it be totally arbitrary and constant, or change each time some state change ? -
restoreStateFunction
: when exactly this function is called in the lifecycle ? what sould I do with restoredState parameter.
Update:
I implemented this
stateFunction = (bleRestoredState) => {
console.log(`[RESTORED STATE]`)
console.log(bleRestoredState)
if (bleRestoredState == null) {
console.log("BleManager was constructed for the first time.")
} else {
console.log("BleManager was restored. Check `bleRestoredState.connectedPeripherals` property.")
console.log(bleRestoredState.connectedPeripherals)
}
}
getBleManager = () => {
if (!this.bleManager) {
console.log(`--- NEW BLE MANAGER ---`);
this.bleManager = new BleManager({
restoreStateIdentifier: 'LockManager-state-id',
restoreStateFunction: this.stateFunction
});
}
return this.bleManager;
};
but in fact on each reload, bleRestoredState
is null.
After continuing my research; It looks like the issue occurs on reload with metro. I lost the instance of BleManager and it's what stuck my app in connected but not recoverable state. Is there a way to save BleManager object in something like localstorage to keep the instance upon reload ? Or something I missed in the doc to make BleManager recover connection.
+1
I'm having the same issue.
It looks like BleManager is not persisting connected devices between Metro reloads. I need to disable and reenable Bluetooth adapter to be able to scan for the same device again.
any updates?
+1