使用依赖性服务固定蓝牙Android



我需要在安装安装后取消蓝牙,我尝试了此代码:

string address;
    public void UnpairDroid()
    {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
        BluetoothDevice bluetoothDevice = bluetoothAdapter.GetRemoteDevice(address);
        var mi = bluetoothDevice.Class.GetMethod("removeBond", null);
        mi.Invoke(bluetoothDevice, null);
    }

到达该代码时,它会引发异常,因为未设置对象(字符串地址)。我需要验证蓝牙API的地址吗?

我花了几个小时来找到解决方案,但没有剩下的想法。请帮忙!谢谢。

这是一种更好的方法吗?

BluetoothDevice device;
private void unpairDevice() {
try {
    Method m = device.getClass()
        .getMethod("removeBond", (Class[]) null);
    m.invoke(device, (Object[]) null);
} catch (Exception e) {
    Log.e(TAG, e.getMessage());
}}

我没有得到该代码的工作。

可以通过bluetoothadapter.address获得本地地址,可以通过

获得远程地址
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
    List<BluetoothDevice> bondedDevices = new List<BluetoothDevice>(bluetoothAdapter.BondedDevices);
    if (bondedDevices.Count > 0)
    {
        foreach (BluetoothDevice device in bondedDevices)
        {
            System.Diagnostics.Debug.Write("Name="+device.Name+" Address="+device.Address);
            BluetoothDevice bluetoothDevice = bluetoothAdapter.GetRemoteDevice(device.Address);
            var mi = bluetoothDevice.Class.GetMethod("removeBond", null);
            mi.Invoke(bluetoothDevice, null);
        }
    }
}

最新更新