最近在调试公司智能设备时,连接蓝牙成功后,又立即断开连接。经过查阅资料,发现是以下原因:
因为之前设备需要配对,且APP连接过设备并成功配对,所以系统自动保存了这个已配对设备,但是后面智能设备改为不需要配对,当再次连接的时候,就会报状态码=22的错误。
解决办法:
1、手动在系统蓝牙设置里找到已配对的这个设备,解除配对。
2、如果系统蓝牙里没有显示已配对设备,在连接设备的时候,使用反射解除配对。
public static void unpairDevice(BluetoothDevice device) {
try {
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
m.setAccessible(true);
m.invoke(device, (Object[]) null);
} catch (Exception e) {
Log.e("ble", e.toString());
}
}