don / cordova-plugin-ble-central

Bluetooth Low Energy (BLE) Central plugin for Apache Cordova (aka PhoneGap)
Apache License 2.0
942 stars 603 forks source link

Android: ble device not returned from scan results after failed connection attempt #958

Closed sam-higgs closed 1 year ago

sam-higgs commented 1 year ago
  1. Scan for devices
  2. Attempt to connect to a Device A
  3. Connection to Device A fails for whatever reason
  4. Re-scan for devices
  5. Device A no longer appears in the list

I also see similar behaviour on a successful connect but there I assume the scan starts before the disconnect event finishes.

HankLloydRight commented 1 year ago

Sounds like the app lost connection to the device, but the phone didn't. When you capture a disconnect event, try doing a ble.disconnect() anyway to get the phone to release the connection. Once a BLE device is connected to a host, it won't advertise as available, so that's why it's not showing up in the scans.

Also, try going into the Settings..Bluetooth and 'forget' the device. Sometimes it gets listed in BT devices, and that holds the connection. But normally that shouldn't happen.

sam-higgs commented 1 year ago

@HankLloydRight thanks for the info, will investigate further.

peitschie commented 1 year ago

Just in case you hadn't already considered, it's often worth building in a single retry of the connect command if the first one fails.

For some reason, there's been quite a few complaints (across several BLE plugins, not just this one) that the initial connection reliability in Android 13 seems a bit worse. In my own applications, I often will retry the connection once before giving up.

This doesn't need another scan in order to do the retry, as you already have all the identifiers needed for things.

For the tech specifics of your question though, @HankLloydRight has the right idea here. On Android especially, the OS will keep the connection open, despite being told to disconnect.

peitschie commented 1 year ago

I'll close this out as there doesn't seem to be any defect in the plugin behaviour, given the phone has failed to drop the connection for some reason.

Feel free to re-open if you are still having problems however and the workarounds aren't helping.