question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android - ConnectToDeviceAsync never completes, connecting to a different device 'fixes' issue

See original GitHub issue

I’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:open
  • Created 3 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
IainS1986commented, Oct 5, 2020

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…

// Get device object for a mac address
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(peripheralAddress)
// Check if the peripheral is cached or not
int deviceType = device.getType();
if(deviceType == BluetoothDevice.DEVICE_TYPE_UNKNOWN) {
    // The peripheral is not cached
} else {
    // The peripheral is cached
}
0reactions
IainS1986commented, Sep 17, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ConnectToDeviceAsync never completes, connecting to a ...
I've ran into a periodic issue I see from time to time. I'm doing lots of builds, DEBUG and RELEASE and generally have...
Read more >
xamarin-bluetooth-le
Hi there,. Question: I've got a "vanilla"-App using this plugin. It works quite well, however, the amount of time of connections made by...
Read more >
How to fix 'Gatt write characteristic FAILED.' exception in ...
You are trying to run IO operation on MAIN thread. That is not possible and Android won't let you do this. This rises...
Read more >
BLE-device stops connecting with Android after working ...
We have tried various tricks: rebooting the phone, uninstalling and reinstalling the app, resetting the device. None of those work. The only way ......
Read more >
How to cancel the BLE task when the device gets disconnected?
ConnectToDeviceAsync getting a CancellationToken . There you pass you cancellationToken , which when cancelled will cancel the execution. This will raise an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found