PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
661 stars 321 forks source link

[Android] Not able to connect after disconnecting #725

Open maciej-szulc opened 1 year ago

maciej-szulc commented 1 year ago

Describe error. [Android only, all versions - on iOS it works fine for me]

After disconnecting (canceling the stream), when I try to reconnect new device the status remains "connecting" indefinitely.

It happens when I want to connect another device. I am able to connect the same device, but I am not able to connect a new device.

Simply put, when I clear the connections to zero connected devices by canceling the stream, I can't connect to any other device than what was previously connected. After turning the app off and on, it works fine.

Interestingly, when one device is connected and tries to change the other device - the bug does not occur.

Does anyone have any idea?

To reproduce. Steps to reproduce the behavior:

  1. connect to the device.
  2. disconnect by canceling the stream.
  3. try to connect to the another device.

Expected behavior. When I disconnect from any device, the app should be ready to reconnect to any new/same device.

Smartphone / tablet.

Peripheral device.

Additionally. This is how I disconnect to device. image

Do you think it may be causes by custom BLE device? It should work anyway, maybe any suggestion what to try?

maciej-szulc commented 1 year ago

301 I think it's the same issue.

maciej-szulc commented 1 year ago

I think I have found a solution. Actually it was also proposed in #301 . I inserted a long delay after canceling the stream and before taking any other action. image

William-Geng commented 1 year ago

I agree, for me instead of just wait a fixed amount of time, I check the connectedDeviceStream and wait for the disconnected event for that specific device, just do a firstwhere and check device ID match and state is "disconnected"

pawlowskim commented 6 months ago

For me, the problem occurs only on Android. Stream returned from the connect method never fires disconnect event. I had to change the listener for connectedDeviceStream as @William-Geng suggested. So I just ignored events from connect method and only used the subscription to disconnect from the device.

Moreover, I have faced issues with asynchronous actions, also only on Android. After disconnecting, I call some writes on the device, which creates unhandled connection that is never terminated (requires application to be killed, BT turned off / on on the device or restart the device manually). More here: https://github.com/PhilipsHue/flutter_reactive_ble/issues/589

rsubramanyam2994 commented 1 month ago

+1. I have set a connection timeout of 1 second. Even after inserting a delay of 4 seconds between disconnecting device 1 and connecting to device 2, I am facing the same issue. @maciej-szulc Is the delay working consistently for you?

maciej-szulc commented 1 month ago

@rsubramanyam2994 I haven't tested it recently. Have you tried the more elegant solution that William suggested above?