BLE设备绑定Android中自动删除 [英] BLE Device Bonding Remove Automatically in Android

查看:1600
本文介绍了BLE设备绑定Android中自动删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在做下面的过程做对与BLE设备。

We are doing below process to do pair with BLE Device.

连接()+ discoverServices()+配对(绑定)。

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

有时候Android操作系统未成对我们的蓝牙设备在一个奇怪的方式,那就是:

Sometimes Android OS unpaired our BT device in a weird way, that is:


  • ,而不发送广播通知,结合状态发生了变化

  • 甚至系统蓝牙设置应用程序认为设备仍配对

  • 仅限BT重启(通过设置应用程序关闭和打开)刷新状态,并显示设备没有配对不再

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

[下午6点19分28秒] Himen帕特尔:04-09 18:18:27.325:D / BluetoothGatt(8380):onCharacteristicWrite() - 设备= 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 previous:10

[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 Status=5 04-09 18:18:27.365: E/millisUntilFinished(8380): millisUntilFinished = 15 04-09 18:18:28.105: E/BelwithDeviceActor(8380): Bond state changed for: C2:69:E9:57:93:A4 new state: 11 previous: 10

18 04-09: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: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

18 04-09:18:33.105:D / BluetoothGatt(8380):onCharacteristicWrite() - 设备= C2:69:E9:57:93:A4 UUID = 032a0000-0000-0000-0000-000000000000状态= 137
04-09 18:18:33.115:E / BelwithDeviceActor(8380):邦德状态改变:C2:69:E9:57:93:A4新状态:12 previous:11

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): Bond state changed for: C2:69:E9:57:93:A4 new state: 12 previous: 11

18 04-09:18:33.115:我/的System.out(8380):unregisterReceiver真

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

现在。




C2:69:E9:57:93:邦德状态改变A4新状态:10

我们还可以得到ACT = android.bluetooth.device.action.ACL_DISCONNECTED FLG = 0x4000010的即时事件在我们的应用程序。

we also get immediate event of act=android.bluetooth.device.action.ACL_DISCONNECTED flg=0x4000010 in our APP.

什么是这里重要的,在这一点上,我们只是失去了配对与设备和受保护的特性不会对我们有什么不再起作用。
如果我们重新启动BT使用系统设置应用程序或BluetoothAdapter ::禁用()和使能(),我们可以看到,我们未与设备配对。

what's important here, at this point we just lost pairing with the device and protected characteristics don't work for us any longer. if we restart bt using system settings app or BluetoothAdapter::disable() and enable() we can see that we are not paired with the device.

有什么好笑的,如果没有BT重启,系统设置,应用程序仍然认为,并表明我们与设备配对。

what's funny, without the bt restart, system settings app still thinks and shows that we are paired with the device.

使用的Nexus 4运行4.4.2,Nexus 5的运行4.4.2,甚至三星galaxy S4测试运行4.3。

tested with nexus 4 running 4.4.2, nexus 5 running 4.4.2 and even Samsung galaxy s4 running 4.3.

我们的期望是:


  • 在解除配对应该有系统广播的情况下

  • 系统preferences应用程序应该显示当前状态配对,即使没有BT重启

我们也观察,并得到中,我们发现,当我们的结合是由OS在不可思议的方式去除我们的加密设置为0x000000,都将嗅探数据。

We have also Observed and get the sniffed data in which we found that our encryption is set to 0x000000 when our bonding is removed by OS in weird way.

推荐答案

我不知道你是否还需要帮助,或者是否最终会解决你自己的问题(大家知道,自从你做了后这个问题早在4月份),但我想继续和张贴的解决方法,我想出了,因为我知道其他人有这个问题。

I have no idea whether you still need help or whether you eventually solved your own problem (you know, since you did post this question back in April), but I wanted to go ahead and post the workaround I came up with because I know other people are having this problem.

使用的Nexus 7,我跑基本上你做了同样的测试,并得出了相同的结论:
如果Android平板电脑和远程设备已经粘合,有一个高的机会,调用BluetoothGatt.discoverServices()会从远程设备上断开和无粘结平板电脑。但是,Android操作系统的某些部分似乎完全无视了unbonding;虽然广播接收机您注册监听绑定变化被告知,在两个设备之间的结合已被打破,由于Android操作系统的其余部分考虑债券仍然是完整的。因为操作系统认为片剂和远程设备要接合,片剂不能写入任何远程设备上的加密描述符,得到每当一个描述符写入尝试15(GATT_INSUFFICIENT_ENCRYPTION)的写入状态。

Using a Nexus 7, I ran basically the same tests you did and came to the same conclusion: If the Android tablet and the remote device were already bonded, there was a high chance that calling BluetoothGatt.discoverServices() would both disconnect and unbond the tablet from the remote device. But, certain parts of the Android OS seemed completely oblivious to the unbonding; although the Broadcast Receiver you registered to listen for bonding changes was notified that the bond between the two devices had been broken, the rest of the Android OS considered the bond to still be intact. Since the OS considered the tablet and the remote device to be bonded, the tablet could not write to any of the encrypted descriptors on the remote device, giving a write status of 15 (GATT_INSUFFICIENT_ENCRYPTION) whenever a descriptor write was attempted.

解决方案

关键是要取消配对的Nexus和他们要取消配对自己在不可思议的方式的机会之前,在远程设备的。我要做的就是检查,看看平板电脑和远程设备粘合之前,我开始一个低功耗蓝牙扫描。如果它们是成对的,我使用下面的函数去除键,然后开始扫描。解除配对两个设备编程可确保Android操作系统意识到,他们不再是保税的,因此,会经过通常的粘合工艺。

The key is to unpair the Nexus and the remote device before they have the chance to unpair themselves in that weird way. What I do is check to see if the tablet and the remote device are bonded right before I start a Bluetooth Low Energy scan. If they are paired, I remove the bond using the function below and then start the scan. Unpairing the two devices programmatically ensures that the Android OS is aware that they are no longer bonded and, therefore, will go through the usual bonding process.

下面是code为用于检查是否远程设备与所述片剂配对并取消配对它,如果它是:

Below is the code is use to check to see if the remote device is paired with the tablet and unpair it if it is:

// 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()); }
}

结果
结果



为什么等待错误,然后重新启动蓝牙解决它是一个糟糕的主意......

正如你已经在你的问题中指出,平板电脑和远程设备相互神秘的力量无粘结Android操作系统来实现,它不再粘结到远程deivce后重新启动蓝牙。这是我用原来的解决办法,但它很快变得清晰,有迹象表明,这种解决方案来了两个主要问题:

As you have already pointed out in your question, restarting the Bluetooth after the tablet and the remote device mysteriously unbond from each other forces the Android OS to realize that it is no longer bonded to the remote deivce. This was the original workaround I used, but it soon became clear that there were two major problems that came with this "solution":


  1. 打开蓝牙和关闭将断开所有已连接到平板电脑的所有设备。

  2. 时间
  3. 打开蓝牙和关闭浪费了许多

  1. Turning the Bluetooth on and off will disconnect all of the devices that were connected to the tablet.
  2. Turning the Bluetooth on and off wastes a lot of time.

我只会重启蓝牙作为最后的手段。例如,如果unbonding错误仍然奇迹般地发生,你唯一的选择是重新启动蓝牙。

I would only restart the Bluetooth as a last resort. For example, if the unbonding error still miraculously occurred, your only choice would be to restart the Bluetooth.

这篇关于BLE设备绑定Android中自动删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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