每个连接请求都被视为直接连接请求 + android ble [英] Every connection request is being treated as direct connect request + android ble

查看:36
本文介绍了每个连接请求都被视为直接连接请求 + android ble的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 whenever we loose a existing connection. So our android application as a central just tries to reconnect by creating new bluetoothgatt object by calling bluetoothdevice.connectgatt with autoconnect as true.

但是每当我们尝试这样做时,我们的重新连接都会因

But whenever we try to do that our reconnection gets failed with

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

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

because our connection request is being treated as a direct connection request on nexus 5 lollipop

这是日志

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.

推荐答案

该问题已在 2016 年 5 月的 master android 分支中修复.关于它是否已在 Nougat 中结束的报告不一,可能与设备有关,但它绝对仍然是 Marshmallow 中的一个错误.

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.

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

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)

代替

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

它对我来说效果很好.我对此不以为然,这完全是 uKL

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

另外,请注意它在 Apache License 2.0 版权所有 2016 Polidea Sp.z o.o

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

这篇关于每个连接请求都被视为直接连接请求 + android ble的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆