NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
1.98k stars 413 forks source link

Fix autoConnect true not working with Android 14 after the second disconnection #531

Closed corentin-c closed 10 months ago

corentin-c commented 10 months ago

Hi,

The issue :

I have been testing Android 14 with bidirectionnal client and server connection, using BleManager.connect with autoConnect = true to connect to a Ble device, it works great with all versions below, but seems to be broken on Android 14. I can connect with it, the first disconnection and reconnection works, but after the second disconnection, the device never reconnects.

The cause :

From what I understand, if autoconnect is used with true, the library first connects with autoConnect false. Then the library uses gatt.connect() on the first disconnection to trigger the autoconnect to true. The problem seems that the gatt.connect() on Android 14 does not do the same as before, it doesn't set autoConnect to true and works only for the next connection. So after the second disconnection, the device does not reconnect anymore.

Diagnosis :

I first noticed that on the first disconnection on Android 14, we have a log not present on previous Android versions :

Connecting... 10:36:10.030 D gatt.connect() 10:36:10.031 D connect(void) - device: XX:XX:XX:XX:6D:DE, auto=false // HERE

It seems like its autoconnecting with false here. It can only reconnect once after this.

So I have been playing with autoconnect without the library and Android 14 :

Fix :

To fix it, use a connectGatt with autoConnect = true if we detect Android 14 on the phone.

philips77 commented 10 months ago

Great catch! That was "corrected" here: https://cs.android.com/android/_/android/platform/packages/modules/Bluetooth/+/213409cb6bceecb570898fccf48ccbc6d2e31fda:framework/java/android/bluetooth/BluetoothGatt.java;dlc=16ee6e5270e5ad92ce667718ed925f00db17f382

corentin-c commented 10 months ago

Yes this is exactly my "bug" !

corentin-c commented 10 months ago

Confirmed working on 2.7.2 thank you @philips77