Every connection request is being treated as direct connect request + android ble

时光总嘲笑我的痴心妄想 提交于 2019-11-30 14:42:41

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!