• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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.

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());
    }
}
  • 66
Reply Report
      • 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.
    • 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..
      • 1
    • 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.

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());
                }
            }
        }
  • 12
Reply Report

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}")
    }
}
  • 1
Reply Report

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.

  • -6
Reply Report