44

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.

Soo Wei Tan
  • 3,262
  • 2
  • 34
  • 36
Dev Perfecular
  • 1,413
  • 1
  • 11
  • 14

5 Answers5

77

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());
    }
}
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Dev Perfecular
  • 1,413
  • 1
  • 11
  • 14
  • @Ewoks, not sure what you mean – Dev Perfecular Jun 14 '13 at 18:24
  • Does this works on Jelly Bean (aka 4.1.x)? Cause I am pretty sure they add new bluetooth stack and createBond method is gone.. – Ewoks Jun 15 '13 at 12:30
  • 11
    The answer is correct but... omg... why do they keep on hiding these methods? At least, they should expose intents like for BluetoothAdapter enable/disable. – andrea.rinaldi Jun 25 '15 at 09:38
  • 2
    Google clearly just hates Bluetooth - this simple method has been hidden for 4 years now. They haven't even tried to implement the Out-of-Band pairing method which is the only one that isn't broken. – Timmmm Jul 05 '16 at 12:08
  • 1
    is there any way; without using reflection? – mumair Jul 12 '17 at 14:02
  • @DevPerfecular getting an exception `parameter type is null` in unpair device , kotlin, will please mention working code for kotlin to unpair and pair device – Riddhi Shankar Mar 26 '19 at 12:50
  • 1
    Use this solution with extreme care Google started a depreciation program for hidden api's and the createbond method is on the greylist (use is still allowed but no guarantee until for how long): https://android.googlesource.com/platform/frameworks/base/+/pie-release/config/hiddenapi-light-greylist.txt –  Feb 17 '20 at 08:43
  • 3
    As of Android 9.0, this method no longer works for user-level apps due to the method now being marked as a system API, which requires system-level permissions to be invoked through reflections. – Daniel Lee Nov 09 '21 at 08:13
14

If you are using Kotlin:

fun removeBond(device: BluetoothDevice) {
    try {
        device::class.java.getMethod("removeBond").invoke(device)
    } catch (e: Exception) {
        Log.e(TAG, "Removing bond has been failed. ${e.message}")
    }
}

Or create an extension function, in this case you can use device.removeBond()

fun BluetoothDevice.removeBond() {
    try {
        javaClass.getMethod("removeBond").invoke(this)
    } catch (e: Exception) {
        Log.e(TAG, "Removing bond has been failed. ${e.message}")
    }
}
Tom Sabel
  • 3,935
  • 33
  • 45
13

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());
                }
            }
        }
Péter Hidvégi
  • 743
  • 6
  • 21
  • Isn't **removeBond()** supposed to be a public method on BluetoothDevice? http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/bluetooth/BluetoothDevice.java – IgorGanapolsky May 01 '17 at 17:20
  • 2
    No, it's marked with `@hide` annotation, which makes it not available to public. – Christopher Nov 08 '17 at 13:42
1

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

-9

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Bhuvn
  • 1