How to unpair or delete paired bluetooth device pr

2019-01-16 05:52发布

The project is to use my android phone to connect with my arduino devices. but how can I unpair the paired ones. I see it seems the paired list is stored where bluetoothadapter could retrieve anytime.

PS: 1st, I know long press paired device will unpair it.
but the question here is how can I make this happen programmatically?

2nd, I have checked bluetoothdevice and bluetoothAdapter class, there is no function to implement this.

thanks.

4条回答
混吃等死
2楼-- · 2019-01-16 06:29

This code works for me.

private void pairDevice(BluetoothDevice device) {
    try {
        if (D)
            Log.d(TAG, "Start Pairing...");

        waitingForBonding = true;

        Method m = device.getClass()
            .getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);

        if (D)
            Log.d(TAG, "Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

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());
    }
}
查看更多
Melony?
3楼-- · 2019-01-16 06:36

in BluetoothService class there is method removebond() to unpair , paired devices. Finally this method call rmovebondnative().

查看更多
三岁会撩人
4楼-- · 2019-01-16 06:54

unpair all devices:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                try {
                    Method m = device.getClass()
                            .getMethod("removeBond", (Class[]) null);
                    m.invoke(device, (Object[]) null);
                } catch (Exception e) {
                    Log.e("Removing has been failed.", e.getMessage());
                }
            }
        }
查看更多
三岁会撩人
5楼-- · 2019-01-16 06:54

If you want to delete the pair bluetooth device for this first of all you have to unpair all the device and than click on serch option you will find all device has removed from the list.

查看更多
登录 后发表回答