Dev*_*lar 34 android bluetooth arduino
该项目是使用我的Android手机连接我的arduino设备.但是我如何解除配对的配对.我看到配对列表似乎存储在bluetoothadapter可以随时检索的位置.
PS:1,我知道长按配对设备将取消配对.
但问题是如何以编程方式实现这一目标?
2,我检查了bluetoothdevice和bluetoothAdapter类,没有实现这个的功能.
谢谢.
Dev*_*lar 63
这段代码适合我.
private void pairDevice(BluetoothDevice device) {
try {
if (D)
Log.d(TAG, "Start Pairing...");
waitingForBonding = true;
Method m = device.getClass()
.getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
if (D)
Log.d(TAG, "Pairing finished.");
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
private void unpairDevice(BluetoothDevice device) {
try {
Method m = device.getClass()
.getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
Pét*_*égi 12
取消配对所有设备:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
try {
Method m = device.getClass()
.getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
} catch (Exception e) {
Log.e("Removing has been failed.", e.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Kotlin:
fun removeBond(device: BluetoothDevice) {
try {
device::class.java.getMethod("removeBond").invoke(device)
} catch (e: Exception) {
Log.e(TAG, "Removing bond has been failed. ${e.message}")
}
}
Run Code Online (Sandbox Code Playgroud)
或者创建一个扩展函数,在这种情况下你可以使用 device.removeBond()
fun BluetoothDevice.removeBond() {
try {
javaClass.getMethod("removeBond").invoke(this)
} catch (e: Exception) {
Log.e(TAG, "Removing bond has been failed. ${e.message}")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48277 次 |
| 最近记录: |