1

using the android 4.4 BLE APIs on my Nexus7, i'm able to successfully interact with a peripheral BLE device -- connect, disconnect, read, write....

if however an active connection breaks for whatever reason (in this case, the peripheral is reset), i observe the following behavior....

  • my peripheral (by design) begins advertising after any active connection is terminated (for whatever reason); i can see this via my bluetooth packet sniffer....

  • i receive the onConnectionStateChanged callback as expected in my android app, at which point i invoke close() on my active BluetoothGatt instance; this is the same procedure i follow during a "normal" disconnect initiated from the client...

  • shortly after this, the android BLE stack tries to re-connect to the same peripheral; through the packet sniffer i can see the BLE connection request going out over the air...

my app, however, did not initiate this re-connection; indeed, i see no information from any bluetooth log suggesting this even happened!!!!

is there some "mode" in the BLE stack where it attempts to re-establish busted connections automatically???

thanks....

user1817180
  • 113
  • 1
  • 7

2 Answers2

4

This happens on various Android phones whether the autoConnect flag is set to false or true.

Couldn't yet find a complete solution, it seems as the android BLE stack is spontaneously re-initiating the connection once it is getting the advertising signal again, just ignoring that it was the app that disconnected on purpose...

A partial solution may involve not using the BluetoothGatt.connect() method as explained here:

https://stackoverflow.com/a/23749770/4144487

So, a sample connect method can look like:

void connect(Context context) {
    if (mGatt != null) {
        mGatt.close();
     }
    mGatt = mDevice.connectGatt(context, false, callback);
}

To explain the importance of this issue, when it happens the peripheral thinks it is connected and my "real" app can't find it any more. At some phones like Galaxy S3 and Redmi note 3 I found that closing the bluetooth switch from the notification bar is "releasing" the peripheral and allowing me to discover the device. At others like Nexus 5x only a phone reboot will do the trick.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Didi
  • 350
  • 1
  • 4
  • 10
0

I've observed this happening if you use autoConnect=true when calling BluetoothGatt#connectGatt(). Generally I've found that it is best to use autoConnect=false, but with some devices you simply cannot connect unless you use true, so I usually do both. I try false first and if that fails then use true and then the behavior you're describing is something you simply have to work around.

Doug Koellmer
  • 407
  • 2
  • 8