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