don/cordova-plugin-ble-central

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

sam-higgs opened this issue · 4 comments

  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.

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.

@HankLloydRight thanks for the info, will investigate further.

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.

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.