简体   繁体   中英

How to receive a notification on reconnect when using Android BLE Gatt

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?

What you should do is to simply call connectGatt once with the autoConnect parameter set to true . When the device connects for the first time, you will get the onConnectionStateChange callback indicating that it's now connected. When the connection drops for some reason, you will get onConnectionStateChange indicating it's now disconnected. But you don't have to, and shouldn't, call connectGatt again. As soon as the peripheral advertises again, your Android device will automatically reconnect and you will get the onConnectionStateChange .

This will work until Bluetooth is turned off (as all Bluetooth objects are silently destroyed at that time), or your app process is terminated. After Bluetooth is turned on, you need to call connectGatt again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM