删除 Android 上所有已配对的蓝牙设备
Delete all paired bluetooth devices on Android
我想以编程方式删除 Android phone 上名称以 "ABC" 开头的配对蓝牙低功耗设备。
我正在使用 Android 工作室。
要取消所有设备的配对,请使用此代码
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
try {
if(device.getName().contains("abc")){
Method m = device.getClass()
.getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
}
} catch (Exception e) {
Log.e("fail", e.getMessage());
}
}
}
如果您具体了解 BLE(低功耗蓝牙),
要获得所有绑定的设备,您可以编写一个方法。
public List<BluetoothDevice> getConnectedDevices() {
BluetoothManager btManager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE);
return btManager.getConnectedDevices(BluetoothProfile.GATT);
}
此 return 在 GATT 配置文件中连接的 BLE 设备列表。
获取名称确认这是否是您要断开连接的设备:
List<BluetoothDevice> btdevices = getConnectedDevices();
for(int i=0;i<btdevices.size();i++)
{
//match your device here
Log.d("saurav"," BLE Name:"+btdevices.get(i).getName());
}
要断开连接,您只需调用断开连接方法即可。您需要断开与 gatt 实例的连接(您用于连接 BLE 设备的同一个 gatt 实例)。
public void disconnect() {
if (gatt == null) {
return;
}
gatt.disconnect();
}
这将断开您的 BLE 设备。我有 teste tshi 亲自为我工作。
我想以编程方式删除 Android phone 上名称以 "ABC" 开头的配对蓝牙低功耗设备。
我正在使用 Android 工作室。
要取消所有设备的配对,请使用此代码
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
try {
if(device.getName().contains("abc")){
Method m = device.getClass()
.getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
}
} catch (Exception e) {
Log.e("fail", e.getMessage());
}
}
}
如果您具体了解 BLE(低功耗蓝牙), 要获得所有绑定的设备,您可以编写一个方法。
public List<BluetoothDevice> getConnectedDevices() {
BluetoothManager btManager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE);
return btManager.getConnectedDevices(BluetoothProfile.GATT);
}
此 return 在 GATT 配置文件中连接的 BLE 设备列表。 获取名称确认这是否是您要断开连接的设备:
List<BluetoothDevice> btdevices = getConnectedDevices();
for(int i=0;i<btdevices.size();i++)
{
//match your device here
Log.d("saurav"," BLE Name:"+btdevices.get(i).getName());
}
要断开连接,您只需调用断开连接方法即可。您需要断开与 gatt 实例的连接(您用于连接 BLE 设备的同一个 gatt 实例)。
public void disconnect() {
if (gatt == null) {
return;
}
gatt.disconnect();
}
这将断开您的 BLE 设备。我有 teste tshi 亲自为我工作。