Android - ConnectToDeviceAsync never completes, connecting to a different device 'fixes' issue
See original GitHub issueI’ve ran into a periodic issue I see from time to time. I’m doing lots of builds, DEBUG and RELEASE and generally have no issues. I have a couple of devices I connect too.
Sometimes, I find on Android that I can no longer connect to a given device. I can scan fine, see the device, but any call to ConnectToDeviceAsync or ConnectToKnownDeviceAsync just never completes…no errors are thrown and the CancellationToken timeout eventually just fires.
If I try to connect to another device (not the previous one used), that works…then trying to reconnect to the previous device that never completed - that then works and everything is ‘fine’ again.
I can uninstall the app, reinstall the current build, older builds, DEBUG build or RELEASE build and I still can’t connect the previous device.
Has anyone seen anything similar?
Heres the trace output when trying to connect to a device that never completes (DeviceConnected never fires)…eventually the cancellation token timeout fires.
[Info] (BluetoothConnectionService.ConnectAsync) Lock Waiting
[Info] (BluetoothConnectionService.ConnectAsync) Lock Acquired
[BluetoothManager] getConnectionState()
[BluetoothManager] getConnectedDevices
[BluetoothGatt] connect() - device: CE:9F:97:95:BE:46, auto: true
[BluetoothGatt] registerApp()
[BluetoothGatt] registerApp() - UUID=5402ae97-da5d-4721-9f97-6ca17c077b43
[BluetoothGatt] onClientRegistered() - status=0 clientIf=11
[OpenGLRenderer] endAllActiveAnimators on 0x70245ca100 (RippleDrawable) with handle 0x70225e7900
<-- Cancellation Token duration expires -->
[BluetoothGatt] cancelOpen() - device: CE:9F:97:95:BE:46
Failed to evaluate expression in conditional breakpoint. Unknown identifier: inflaterFailed to evaluate expression in conditional breakpoint. Unknown identifier: inflater[Info] (BluetoothConnectionService.ConnectAsync) Lock Releasing
[Info] (BluetoothConnectionService.ConnectAsync) Lock Released
Issue Analytics
- State:
- Created 3 years ago
- Comments:14
Top GitHub Comments
Update for anyone - more time has passed and I’ve seen the version of the App that tries to do a ConnectToKnownDevice call fail multiple time while the version of the app that does a fresh Scan + ConnectToDevice call work fine.
Pretty confident I’ve confirmed it to be Android internal BLE cache clearing out, and therefore ConnectToKnownDevice will fail and AutoConnect=TRUE will fail.
Propose maybe having a ‘CanConnectToKnownDevice’ call that for iOS etc just returns TRUE and for Android does a check on the device type to see if its Unknown (not cached), similar too this Android code…
Update:
Another week passed. Checked the two apps.
App A: AutoConnect=FALSE, called ConnectToKnownDevice -> Failed to connect App B: AutoConnect=FALSE, did a scan and then ConnectToDevice -> Connected
So far it seems like my suspicions around trying to connect to a known device when the internal OS BLE cache has been cleared does not work are correct.
I also suspect this repo is potentially abandoned at this point? Either way, anyone who runs into this…
If you use AutoConnect=TRUE, if the device BLE cache is cleared, ConnectToDevice and ConnectToKnownDevice can fail to ever connect
If you use ConnectToKnownDevice, this can fail to connect if the device BLE cache is cleared, regardless of AutoConnect value
My advice, avoid both if you’re not sure, otherwise you will have to do your own handling for fallback to Scan->Connect. I might look to add functionality to this plugin to have a ‘check can reconnect’ function in Droid that checks if the Device object type is Unknown or not, as that (apparently) lets you know if you can do a ConnectToKnownDevice call or not
I might report back periodically to confirm my findings, or if they change.