By Dev Perfecular


2012-03-07 19:54:18 8 Comments

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.

5 comments

@Tom Sabel 2019-09-02 14:26:07

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}")
    }
}

@Dev Perfecular 2012-06-21 22:45:24

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

@Dev Perfecular 2013-06-14 18:24:46

@Ewoks, not sure what you mean

@Ewoks 2013-06-15 12:30:25

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

@andrea.rinaldi 2015-06-25 09:38:03

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.

@Timmmm 2016-07-05 12:08:52

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.

@mumair 2017-07-12 14:02:19

is there any way; without using reflection?

@Riddhi Shankar 2019-03-26 12:50:26

@DevPerfecular getting an exception parameter type is null in unpair device , kotlin, will please mention working code for kotlin to unpair and pair device

@remonh87 2020-02-17 08:43:25

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): android.googlesource.com/platform/frameworks/base/+/pie-rele‌​ase/…

@GNK 2020-10-07 02:25:38

this solution is not working

@Péter Hidvégi 2015-09-03 13:34:16

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

@IgorGanapolsky 2017-05-01 17:20:03

Isn't removeBond() supposed to be a public method on BluetoothDevice? androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/andr‌​oid/…

@Christopher 2017-11-08 13:42:08

No, it's marked with @hide annotation, which makes it not available to public.

@Bhuvn 2012-06-21 16:39:42

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.

@chandan kumar 2012-04-15 09:30:54

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

@IgorGanapolsky 2017-05-01 17:20:37

Is it a publicly-exposed api?

Related Questions

Sponsored Content

111 Answered Questions

52 Answered Questions

[SOLVED] Is there a unique Android device ID?

38 Answered Questions

[SOLVED] How to lazy load images in ListView in Android

40 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

52 Answered Questions

4 Answered Questions

[SOLVED] How to unpair bluetooth device using android 2.1 sdk

  • 2010-08-11 21:24:59
  • user417738
  • 16956 View
  • 10 Score
  • 4 Answer
  • Tags:   android bluetooth

4 Answered Questions

[SOLVED] Programmatically connect to paired Bluetooth device

  • 2011-03-02 17:38:28
  • rmc47
  • 84872 View
  • 55 Score
  • 4 Answer
  • Tags:   android bluetooth

10 Answered Questions

[SOLVED] How to programmatically pair a bluetooth device on Android

  • 2011-02-14 07:54:42
  • FireFly
  • 78141 View
  • 17 Score
  • 10 Answer
  • Tags:   android bluetooth

Sponsored Content