3

I have a problem connecting to the bonded BLE device on Android. The first connection to the device, which includes the bonding is successful and I'm able to read all the characteristics and the connection remains stable.

When I kill the app or disconnect manually and try to connect to the device again using the same code except the bonding itself, I'm disconnected from the Android side with error code 22, which means "Connection terminated by localhost".

Is there any difference under the hood from Android side in these 2 scenarios that I'm not aware of? I use basically the tutorial code which I can provide if necessary but I've tried basically everything from changing gatt parameters to auto connect/not, adding the BluetoothDevice.TRANSPORT_LE as a transport flag and still nothing.

This problem keeps happening on my Pixel, Moto G4 and all the latest Nexus devices.

The peripheral is using Qualcomm chip CSR1010 uEnergy, is advertising and needs to be paired in order to establish the connection. Here are the snippets of the connection stuff:

val bondCreated = device.createBond()

In the Broadcast receiver of the bonding state change I call the connect methods. I've tried to experiment with these two methods omit one and the other, change parameters but nothing

bluetoothGatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
bluetoothGatt?.connect()

If I receive the disconnect info, I call:

bluetoothGatt?.close()

bluetoothGatt?.disconnect()

And I try to connect again using the same code as above

I've already tried to put delays everywhere on every gatt operation but still no success. Changing auto connect to true after I'm disconnected by Android doesn't work.

I've also tried some 3rd party iOS apps and all of those connect to the device without a problem. Their Android versions always disconnect the same way mine does.

Could you please provide any help? It will be most appreciated.

The logs are here:

D/BleGattController: DEVICE ALREADY BONDED: Device

D/BluetoothGatt: connect() - device: 40:80:08:AF:DC:6B, auto: false

D/BluetoothGatt: registerApp()

D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8

D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=40:80:08:AF:DC:6B

D/BleGattController$gattC: onConnectionStateChange 0 - connected

D/BleGattController$gattC: STATE_CONNECTED

D/BluetoothGatt: onConnectionUpdated() - Device=40:80:08:AF:DC:6B interval=6 latency=0 timeout=500 status=0

D/BluetoothGatt: onClientConnectionState() - status=22 clientIf=8 device=40:80:08:AF:DC:6B

D/BleGattController$gattC: onConnectionStateChange 22 - disconnected

D/BleGattController$gattC: STATE_DISCONNECTED

Vena
  • 56
  • 4
  • You should check the hci snoop log file to better find out what happens. – Emil Oct 31 '17 at 19:51
  • Thanks for the reply..I did that but all I can see there is a disconnect request from the phone after successfully connecting to the peripheral. – Vena Oct 31 '17 at 21:30
  • @Vena did you find the cause of this problem? – svenmeier Dec 17 '19 at 21:32
  • Similar problem: gatt server on bonded devices and after bonding connection doesn't work – Georgiy Chebotarev Jun 03 '21 at 19:30
  • We found out it was a FW error in the end caused by multiple things - wrong connection handling that iOS somehow accepted but Android didn't but also one particular issue stood out - There was a change of connection parameters between Android 5 and 6: https://stackoverflow.com/questions/34617061/android-6-0-marshmallow-ble-connection-parameters Since iOS is using yet another different interval, it might be the reason this is happening if you're testing it on different versions of Android and iOS is working fine – Vena Jun 18 '21 at 08:10

0 Answers0