BLE Device Bonding Remove Automatically in Android

前端 未结 2 1894
一整个雨季
一整个雨季 2020-12-09 04:48

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

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

Sometimes Android OS unpaired our BT device in a wei

相关标签:
2条回答
  • 2020-12-09 05:23

    We had the same issue and we've figured out that "connectGatt" has new "transport" argument (which defines transport protocol of connection), starting from SDK v 23. So if you want to pair your devices you should use "TRANSPORT_BREDR", if you want to only connect to peripheral without bonding - use "TRANSPORT_LE" Documentation: https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int)

    0 讨论(0)
  • 2020-12-09 05:39

    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.

    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.

    The Solution

    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.

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



    Why waiting for the error and then solving it by restarting the Bluetooth is a bad idea...

    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. 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.

    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.

    0 讨论(0)
提交回复
热议问题