When using the BluetoothGattCallback
on Android, I'm able to see when a bonded device connects and disconnects by overriding onConnectionStateChange
and checking the newState
field. However, I'm wondering if there's a means by which my app can be informed of a re-connect which is what I imagine when a bonded device becomes unreachable for a while.
Note that I'm using bonded devices here so that I'm able to obtain a stable MAC address.
About the answer
While I never managed to entirely test the answer, the answer given does make sense to me. A few things I've learned:
- devices have to be bonded to reliably re-connect as their MAC addresses are randomised
- with Android, a direct connection must first be achieved so that Android can cache connection information - subsequent connections can be performed with a re-connect
- bonding on Android is difficult to get right (I'm yet to) - instead, I'm now leaning more toward leveraging the system UI to pair devices
- use a library like the Nordid Android BLE Library - there are many quirks to Android
Old supplement to the question below - should now be ignored
The logs show that onConnectionStateChange
is called:
16:21:33.681 D/BluetoothGatt: connect() - device: 57:D4:E9:34:A4:CB, auto: true
16:21:33.682 D/BluetoothGatt: registerApp()
16:21:33.683 D/BluetoothGatt: registerApp() - UUID=b4751d3b-ccdd-44c2-823e-deed18057af3
16:21:33.689 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
16:22:44.154 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=57:D4:E9:34:A4:CB
If the device later becomes disconnected, I also see the state change:
16:24:25.800 D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=6 device=57:D4:E9:34:A4:CB
If the device reconnects given the auto-connection then a connection is re-established but I don't receive any notification:
16:26:30.519 D/BluetoothGatt: connect() - device: 57:D4:E9:34:A4:CB, auto: true
16:26:30.520 D/BluetoothGatt: registerApp()
16:26:30.521 D/BluetoothGatt: registerApp() - UUID=a973e2a7-b881-404a-875b-b2d25460e023
16:26:30.528 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
So, how can an app detect a re-connect?