How to unpair bluetooth device using android 2.1 sdk

前端 未结 4 1119
日久生厌
日久生厌 2020-11-29 23:50

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 f

4条回答
  •  情歌与酒
    2020-11-30 00:53

    Another way:

    public void clear(View v) {
        Set 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);
        }
    }
    

提交回复
热议问题