10

In Android 2.1, to unpair a Bluetooth device you can go to Bluetooth settings, long-click on a device and select Unpair to unpair that device. I want to be able to do this from my application. I can retrieve a list of paired/bonded devices using BluetoothAdapter.getBondedDevices(), but I can't find how to unpair. I've explored the BluetoothChat sample, and I've searched the sdk but still can't find an API that allows this.

How can I unpair a Bluetooth device?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
user417738
  • 101
  • 1
  • 1
  • 3

4 Answers4

11

Here's how you unpair/remove a bonded device call this method where macAddress is the string of the mac address of the device..i.e. "00:02:00:A3:03:05"

IBluetooth ib =getIBluetooth();
ib.removeBond(macAddress);

To get the IBluetooth Object you need to go through a couple of steps

  1. create a package in your project called android.bluetooth
  2. create two files, IBluetooth.aidl and IBluetoothCallback.aidl
  3. create method in your files called getBluetooth()

    private IBluetooth getIBluetooth() {
    IBluetooth ibt = null;
    
    try {
    
        Class c2 = Class.forName("android.os.ServiceManager");
    
        Method m2 = c2.getDeclaredMethod("getService",String.class);
        IBinder b = (IBinder) m2.invoke(null, "bluetooth");
    
        Class c3 = Class.forName("android.bluetooth.IBluetooth");
    
        Class[] s2 = c3.getDeclaredClasses();
    
        Class c = s2[0];
        Method m = c.getDeclaredMethod("asInterface",IBinder.class);
        m.setAccessible(true);
        ibt = (IBluetooth) m.invoke(null, b);
    
    
    } catch (Exception e) {
        Log.e("flowlab", "Erroraco!!! " + e.getMessage());
    }
    
    return ibt;
    }
    

    /************ IBluetooth.aidl ************/

    package android.bluetooth;
    
    import android.bluetooth.IBluetoothCallback;
    import android.os.ParcelUuid;
    
    /**
      * System private API for talking with the Bluetooth service.
      *
      * {@hide}
      */
     interface IBluetooth
     {
       boolean isEnabled();
       int getBluetoothState();
       boolean enable();
       boolean disable(boolean persistSetting);
    
       String getAddress();
       String getName();
       boolean setName(in String name);
    
       int getScanMode();
       boolean setScanMode(int mode, int duration);
    
       int getDiscoverableTimeout();
       boolean setDiscoverableTimeout(int timeout);
    
       boolean startDiscovery();
       boolean cancelDiscovery();
       boolean isDiscovering();
    
       boolean createBond(in String address);
       boolean cancelBondProcess(in String address);
       boolean removeBond(in String address);
       String[] listBonds();
       int getBondState(in String address);
    
       String getRemoteName(in String address);
       int getRemoteClass(in String address);
       ParcelUuid[] getRemoteUuids(in String address);
       boolean fetchRemoteUuids(in String address, in ParcelUuid uuid, in IBluetoothCallback callback);
       int getRemoteServiceChannel(in String address, in ParcelUuid uuid);
    
       boolean setPin(in String address, in byte[] pin);
       boolean setPasskey(in String address, int passkey);
       boolean setPairingConfirmation(in String address, boolean confirm);
       boolean cancelPairingUserInput(in String address);
    
       boolean setTrust(in String address, in boolean value);
       boolean getTrustState(in String address);
    
       int addRfcommServiceRecord(in String serviceName, in ParcelUuid uuid, int channel, IBinder b);
       void removeServiceRecord(int handle);
    }
    

/************ IBluetoothCallback.aidl ************/

    package android.bluetooth;

    /**
     * System private API for Bluetooth service callbacks.
     *
     * {@hide}
     */
    interface IBluetoothCallback
    {
        void onRfcommChannelFound(int channel);
    }
JPM
  • 9,077
  • 13
  • 78
  • 137
  • would you please share this package in one zip file? – Majid Golshadi Jan 14 '14 at 11:24
  • Sorry for ignorance but I do not understand point 3) "create method in your files called getBluetooth()" ? Where is that ? Why do I get a "cannot resolve symbol IBluetooth" – Sebastien FERRAND Nov 14 '17 at 08:12
  • Lol I wrote this 6 years ago...I don't remember the finer points anymore. Most likely your IBlueTooth.aidl is not in the right place or not imported in the build script. – JPM Nov 14 '17 at 16:01
3

Another way:

public void clear(View v) {
    Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
    try {
        Class<?> btDeviceInstance =  Class.forName(BluetoothDevice.class.getCanonicalName());
        Method removeBondMethod = btDeviceInstance.getMethod("removeBond");
        String currentMac = getCurrentMAC();
        boolean cleared = false;
                for (BluetoothDevice bluetoothDevice : bondedDevices) {
            String mac = bluetoothDevice.getAddress();
            if(mac.equals(currentMac)) {
                removeBondMethod.invoke(bluetoothDevice);
                Log.i(TAG,"Cleared Pairing");
                cleared = true;
                break;
            }
        }

                if(!cleared) {
            Log.i(TAG,"Not Paired");
                }
    } catch (Throwable th) {
        Log.e(TAG, "Error pairing", th);
    }
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
d3n13d1
  • 200
  • 12
  • 1
    Thanks!!! This worked great for me! Where is the "removeBond(BluetoothDevice device)" method defined? Why do we have to invoke reflection to call this? – ossys Mar 12 '13 at 16:55
  • Hey d3n13d1, I tried this but thats not working on API level 24 and above, can you please suggest any alternative way, Thanks – umesh Jan 28 '19 at 10:17
  • I've only used it w/ apk's built with version 16 and 17 but it works on my pixel. – d3n13d1 Feb 22 '19 at 22:33
1

you can send the user to bluetooth setting, by this you can unpair devices which are paired Intent intentOpenBluetoothSettings = new Intent(); intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); startActivity(intentOpenBluetoothSettings);`

Mohd Qasim
  • 896
  • 9
  • 20
  • Please explain your code so other users can understand its functionality. Thanks! – Ignacio Ara May 14 '18 at 09:39
  • @IgnacioAra, when ever end user click on "scan for device" button at that time we are sending him to bluetooth setting intent. he can paired device from there and come back to the app. – Mohd Qasim May 14 '18 at 12:39
0

It is possible to unpair a device manually throug droid java.

You can invoke a hidden method to remove the bond.

Guntram
  • 961
  • 14
  • 19
  • Might want to ask this question or search this sight more, but the suggestion I have above is the way I found to programmatically unpair. To manually unpair it you use the Bluettooth program already in the phone. – JPM Aug 24 '11 at 14:52