user91579631 user91579631 - 2 months ago 20
Android Question

Delete all paired bluetooth devices on Android

I would like to delete paired bluetooth low energy devices with names that start with "ABC" on an Android phone programatically.

I am using Android studio.

Answer

To unpair all devices use this code

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                try {
                    if(device.getName().contains("abc")){
                    Method m = device.getClass()
                            .getMethod("removeBond", (Class[]) null);
                    m.invoke(device, (Object[]) null);
                    }
                } catch (Exception e) {
                    Log.e("fail", e.getMessage());
                }
            }
        }