我需要在安装完成后解开蓝牙并尝试使用此代码:
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());
}}
我还没有让代码工作..
答案 0 :(得分:0)
可以通过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);
}
}
}