unpair Bluetooth Mac - Android Bluetooth

Android examples for Bluetooth:Bluetooth Bond

Description

unpair Bluetooth Mac

Demo Code


//package com.java2s;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
import java.lang.reflect.Method;
import java.util.Set;

public class Main {
    public static void unpairMac(String macToRemove) {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter
                .getDefaultAdapter();/*from ww w . j  a  v a 2s .  c  om*/
        if (bluetoothAdapter != null) {
            Set<BluetoothDevice> bondedDevices = bluetoothAdapter
                    .getBondedDevices();
            try {
                Class<?> btDeviceInstance = Class
                        .forName(BluetoothDevice.class.getCanonicalName());
                Method removeBondMethod = btDeviceInstance
                        .getMethod("removeBond");
                boolean cleared = false;
                for (BluetoothDevice bluetoothDevice : bondedDevices) {
                    String mac = bluetoothDevice.getAddress();
                    if (mac.equals(macToRemove)) {
                        removeBondMethod.invoke(bluetoothDevice);
                        Log.i("BT", "Cleared Pairing");
                        cleared = true;
                        break;
                    }
                }

                if (!cleared) {
                    Log.i("BT", "Not Paired");
                }
            } catch (Throwable th) {
                Log.e("BT", "Error pairing", th);
            }
        }
    }
}

Related Tutorials