Android – How to unpair or delete paired bluetooth device programmatically on android

androidarduinobluetooth

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.

Best Answer

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