Hi am working on Android BLE, with GATT connections.
As Android is responsible for creating the bonding and we do not call createBond().
The problem what i am facing is with few Android 12 devices, where the Pairing and connection is happening successfully for the first time, but it fails when we try to remove the bonding from the device(via reflection method "removeBond") and re-connect again.
Re-pairing happen, but when we are trying to do a BluetoothSocket.connect() again for the second tiome connection, the connection fails and program is stuck in the connect() method.
As we know connect() is a blocker function and does not have any callbacks, thus i am not able to make a re-connect.
Tried multiple ways of re-connection and re-bonding but FAILED.
I have got some reference from this documents which excellently explains this particular issue.
but does not provide any solution.
As per the document the Android 12 AOSP Stack code here:
The supervision time is has been changed to 5 sec. Still the issue remains the same.
NOTE: The same scenario works on Android 13 devices(Pixel 7 Pro/Samsung S2@ ultra) but fails to work on few Android 12 devices( majorly Samsung A Series(One UI version 4.1) and few Pixel devices)
fun createL2CapChannel(bluetoothDevice: BluetoothDevice, psmInt: Int): BluetoothSocket
{
if (socket == null) {
Log.d(TAG, "Creating L2Cap channel")
socket = bluetoothDevice.createL2capChannel(psmInt)
}
return socket as BluetoothSocket
}
fun connectToBluetoothSocket() {
if (socket!!.isConnected) {
Log.d(TAG, "L2Cap Socket connected")
} else {
Log.d(TAG, "L2Cap Socket connecting")
socket!!.connect()
}
}
The Re-Connection stuck at socket.connect()
Disconnection code after first successful connection
fun disconnectGattServer() {
if (bleGatt != null) {
Log.d(TAG, "Closing Gatt")
bleGatt?.disconnect()
bleGatt?.close()
bleGatt = null
}
}
I am stuck in this issue and require some support on this issue
Thanks in advance.
After days of research and hard work, I found some workaround for the problem which I was facing.
Testing Devices used:
From my testing I came across few points.
The BLE for android handles bonding on its own, so while creating the l2Capp socket the bonding was not completed (Bonding does not goes to BONDED state) and we were starting the socket before that, their were no check for bonding.
I tried using the createBond() API provided by the Android, but it seems the API internally calls TRANSPORT_AUTO as the parameter, for the GATT, I require TRANSPORT_LE as the connect needs to be with LE itself(not with Classic Bluetooth or TRANSPORT_AUTO), So this solution was still not accurate as bonding didnt established.
Now, the problem:
In Android 12: The bonding is started by BLE while you connect to GATT, but for the first instance the bonding sometimes start late(from the stack itself) So, I kept the check for the BONDED/BOND_NONE onConnectionStateChange() callback and than start the discovery. Now, In onCharacteristicsRead() for Android 12, i checked for Bonding state BONDED and than go for l2Capp socket connection. On the Other hand, if the bond is still in progress, I registered a Broadcast for ACTION_BOND_STATE_CHANGED. and After receiving the BONDED condition I went to l2Capp Connection. Make Sure you are not calling the l2capp connection twice.
In Android 13, Did the same but i saw the bonding is generally finished while moving for the l2Capp connection. So the flow remains the same. just connecting for the socket as soon as i am reading.
One more observation was sometimes for both Android 12/13 i saw the BLE does not discover the service. So in the receiver callback of BONDED I checked for the services and if it is not there i start the discoverService() again.
This solved my problem for Android 12 and 13.
I am still facing one issue with Goople Pixel 7 Pro. I am sometimes getting GATT_INSUFFICIENT_AUTHORISATION aftyer doing a Gatt connect().
But this is reproducible 2 out of 10 times.
Will update if I get any solution for the same.
Thanks