react-native-ble-plx icon indicating copy to clipboard operation
react-native-ble-plx copied to clipboard

Bug Report

Open MaximeLozach opened this issue 2 years ago • 10 comments

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

MaximeLozach avatar Apr 22 '22 09:04 MaximeLozach

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.

MaximeLozach avatar Apr 25 '22 14:04 MaximeLozach

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, bleRestoredStateis null.

MaximeLozach avatar Apr 25 '22 15:04 MaximeLozach

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.

MaximeLozach avatar Apr 26 '22 07:04 MaximeLozach

+1

AkashWaghule avatar Jun 23 '22 10:06 AkashWaghule

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.

tspiechaczek avatar Oct 29 '22 22:10 tspiechaczek

any updates?

kavindadilshan avatar Apr 27 '23 06:04 kavindadilshan

+1

viaesys avatar Feb 01 '24 21:02 viaesys