Skip to content Skip to sidebar Skip to footer

Every Connection Request Is Being Treated As Direct Connect Request + Android Ble

We are writing a ble application where want to have a persistent connection with the peripheral we want to connect. For the same purpose we want to re-connect with the peripheral w

Solution 1:

The problem has been fixed in the master android branch, May 2016. There are mixed reports about whether it has ended up in Nougat or not, possibly device dependent, but it is definitely still a bug in Marshmallow.

The reflection code required to do the workaround quickly becomes complicated because the classes IBluetoothManager and IBluetoothGatt are not available in user code.

Fortunately, someone has already written a very small, clear library which does this exact routine for us.

https://github.com/Polidea/RxAndroidBle/blob/master/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/util/BleConnectionCompat.java

Using this class, one need only call:

mBluetoothGatt = (new BleConnectionCompat(context)).connectGatt(device, autoConnect, callback)

instead of

mBluetoothGatt = device.connectGatt(context, autoConnect, callback);

It is working beautifully for me. I take no credit whatsoever for this, it is entirely the work of uKL

Also, note it is under Apache License 2.0 Copyright 2016 Polidea Sp. z o.o

Solution 2:

The problem is a race condition described here: https://code.google.com/p/android/issues/detail?id=69834

A possible solution until they fix it (will they?) is to use reflection to manually construct a gatt object, set the mAutoConnect flag to true and call connect.

Post a Comment for "Every Connection Request Is Being Treated As Direct Connect Request + Android Ble"