BLE设备绑定在Android中自动删除

him*_*tel 14 android bluetooth bluetooth-lowenergy

我们正在做下面的过程做对与BLE装置.

Connect()+ discoverServices()+ Pairing(Bonding).

有时Android操作系统以奇怪的方式取消配对我们的BT设备,即:

  • 而不发送广播通知该键合状态已经改变
  • 甚至系统蓝牙设置应用程序认为设备仍然配对
  • 只有bt重新启动(关闭并打开通过设置应用程序)刷新状态并显示设备不再配对

当设备成功配对的ACTION_BOND_STATE是如下改变.

[6:19:28 PM] Himen Patel:04-09 18:18:27.325:D/BluetoothGatt(8380):onCharacteristicWrite() - Device = C2:69:E9:57:93:A4 UUID = 860b2c07-e3c5- 11e2-a28f-0800200c9a66状态= 5 04-09 18:18:27.365:E/millisUntilFinished(8380):millisUntilFinished = 15 04-09 18:18:28.105:E/BelwithDeviceActor(8380):债券状态改为:C2: 69:E9:57:93:A4新状态:11上一个:10

04-09 18:18:28.105:E/millisUntilFinished(8380):millisUntilFinished = 20 04-09 18:18:29.135:E/millisUntilFinished(8380):millisUntilFinished = 18 04-09 18:18:30.135:E/millisUntilFinished (8380):millisUntilFinished = 17 04-09 18:18:31.145:E/millisUntilFinished(8380):millisUntilFinished = 16 04-09 18:18:32.145:E/millisUntilFinished(8380):millisUntilFinished = 15

04-09 18:18:33.105:D/BluetoothGatt(8380):onCharacteristicWrite() - Device = C2:69:E9:57:93:A4 UUID = 032a0000-0000-0000-0000-000000000000 Status = 137 04-09 18:18:33.115:E/BelwithDeviceActor(8380):C2:69:E9:57:93:粘结状态改变A4新的状态:12前:11

04-09 18:18:33.115:I/System.out(8380):unregisterReceiver true

现在,当操作系统以奇怪的方式删除配对时,ACTION_BOND_STATE将如下更改..... 债券状态改为:C2:69:E9:57:93:A4新状态:10.

我们也在我们的APP中立即获得了act = android.bluetooth.device.action.ACL_DISCONNECTED flg = 0x4000010的事件.

这里有什么重要的,在这一点上,我们失去了与设备的配对,受保护的特性对我们来说不再适用.如果我们使用系统设置应用程序或BluetoothAdapter :: disable()和enable()重新启动bt,我们可以看到我们没有与设备配对.

有趣的是,没有重启,系统设置应用程序仍然认为并显示我们与设备配对.

使用运行4.4.2的nexus 4,运行4.4.2的nexus 5以及运行4.3的三星galaxy s4进行测试.

我们的期望是:

  • 如果不配对,应该有系统广播
  • 系统预置应用程序应该显示当前配对状态,即使没有BT重启

我们还发现,并得到中,我们发现,当我们的结合是通过OS在不可思议的方式去除我们的加密设置到0x000000,嗅探数据.

Hun*_*r S 11

我不知道你是否还需要帮助,或者你是否最终解决了自己的问题(你知道,因为你在四月份发布了这个问题),但是我想继续发布我提出的解决方法,因为我知道其他的人们有这个问题.

使用Nexus 7,我运行了与您相同的测试并得出了相同的结论:如果Android平板电脑和远程设备已经绑定,则调用BluetoothGatt.discoverServices()的机会很可能会断开连接并取消绑定来自远程设备的平板电脑.但是,Android操作系统的某些部分似乎完全忘记了没有绑定; 虽然您注册监听绑定更改的广播接收器被通知两个设备之间的绑定已被破坏,但其他Android操作系统认为绑定仍然完好无损.由于操作系统认为平板电脑和远程设备要绑定,因此平板电脑无法写入远程设备上的任何加密描述符,每当尝试描述符写入时,写入状态为15(GATT_INSUFFICIENT_ENCRYPTION).

解决方案

关键是他们有机会以这种奇怪的方式解开自己之前,取消Nexus和远程设备的配对.我所做的是在开始蓝牙低功耗扫描之前检查平板电脑和远程设备是否正确绑定.如果它们已配对,我使用下面的功能删除粘合,然后开始扫描.以编程方式取消配对这两个设备可确保Android操作系统意识到它们不再被绑定,因此将经历通常的绑定过程.

以下代码用于检查远程设备是否与平板电脑配对,如果是,则取消配对:

// Get the paired devices and put them in a Set
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

// Loop through the Set of paired devices, checking to see
// if one of the devices is the device you need to unpair
// from. I use the device name, but I'm sure you can find
// another way to determine whether or not its your device
// -- if you need to. :)
for (BluetoothDevice bt : pairedDevices) {
        if (bt.getName().contains("String you know has to be in device name")) {
            unpairDevice(bt);
        }
}

// Function to unpair from passed in device
private void unpairDevice(BluetoothDevice device) {
    try {
        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
    } catch (Exception e) { Log.e(TAG, e.getMessage()); }
}
Run Code Online (Sandbox Code Playgroud)



为什么等待错误然后通过重新启动蓝牙解决它是一个坏主意...

正如您在问题中已经指出的那样,在平板电脑和远程设备彼此神秘地脱钩之后重新启动蓝牙会迫使Android操作系统意识到它已不再与远程设备绑定.这是我使用的原始解决方法,但很快就发现这个"解决方案"带来了两个主要问题:

  1. 打开和关闭蓝牙将断开连接到平板电脑的所有设备.
  2. 打开和关闭蓝牙会浪费很多时间.

我只会重新启动蓝牙作为最后的手段.例如,如果仍然奇迹般地发生了未绑定错误,那么您唯一的选择就是重新启动蓝牙.