每个连接请求都被视为直接连接请求+ android ble

kap*_*aps 5 android bluetooth-lowenergy android-bluetooth

我们正在编写一个应用程序,希望与我们想要连接的外围设备建立持久连接.出于同样的目的,我们希望在松开现有连接时重新连接外围设备.所以我们的android应用程序作为一个中心只是尝试通过调用bluetoothdevice.connectgatt与autoconnect为true来创建新的bluetoothgatt对象来重新连接.

但每当我们尝试这样做时,我们的重新连接就会失败

12-02 21:47:11.865: D/BluetoothGatt(31963): onClientConnectionState() - status=133 clientIf=6 device=******** callback.
Run Code Online (Sandbox Code Playgroud)

因为我们的连接请求被视为nexus 5棒棒糖的直接连接请求

这是日志

12-03 11:46:12.804: D/BluetoothGatt(6902): connect() - device: 58:EB:14:3D:2A:38, auto: true
12-03 11:46:12.804: D/BluetoothGatt(6902): registerApp()
12-03 11:46:12.804: D/BluetoothGatt(6902): registerApp() - UUID=b8f9298b-4a95-41be-80d6-22d82c498c5c
12-03 11:46:12.807: D/BtGatt.GattService(31817): registerClient() - UUID=b8f9298b-4a95-41be-80d6-22d82c498c5c
12-03 11:46:12.808: D/BtGatt.GattService(31817): onClientRegistered() - UUID=b8f9298b-4a95-41be-80d6-22d82c498c5c, clientIf=6
12-03 11:46:12.808: D/BluetoothGatt(6902): onClientRegistered() - status=0 clientIf=6
12-03 11:46:12.808: D/BtGatt.GattService(31817): clientConnect() - address=58:EB:14:3D:2A:38, isDirect=true
12-03 11:46:12.809: D/BtGatt.btif(31817): btif_get_device_type: Device [58:eb:14:3d:2a:38] type 2, addr. type 0
12-03 11:46:12.811: D/BLEManager(6902): Trying to create a new connection.
Run Code Online (Sandbox Code Playgroud)

Mar*_* Ch 8

这个问题已在2016年5月的主安卓分支中得到修复.关于它是否已经在Nougat结束,可能与设备有关,有混合的报道,但它绝对仍然是Marshmallow中的一个错误.

快速解决方法所需的反射代码变得复杂,因为IBluetoothManager和IBluetoothGatt类在用户代码中不可用.

幸运的是,有人已经写了一个非常小的,清晰的库,它为我们做了这个精确的例程.

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

使用这个类,只需要调用:

mBluetoothGatt = (new BleConnectionCompat(context)).connectGatt(device, autoConnect, callback)
Run Code Online (Sandbox Code Playgroud)

代替

mBluetoothGatt = device.connectGatt(context, autoConnect, callback);
Run Code Online (Sandbox Code Playgroud)

它对我来说很漂亮.对此我没有任何信任,这完全是uKL的工作

此外,请注意它是在Apache License 2.0版权所有2016 Polidea Sp.动物园


Emi*_*mil 3

问题是这里描述的竞争条件:https://code.google.com/p/android/issues/detail?id =69834

在他们修复它之前(他们会吗?),一个可能的解决方案是使用反射手动构造一个 gatt 对象,将 mAutoConnect 标志设置为 true 并调用 connect。