unpair Mac address - Android java.net

Android examples for java.net:Mac Address

Description

unpair Mac address

Demo Code

import java.lang.reflect.Method;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;

public class Main {

  public static void unpairMac(String macToRemove) {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter != null) {
      Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
      try {// ww w .  j  av  a  2  s.c  om
        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