使用 Android 12 设备进行测试时,每当我的蓝牙设备断开连接时,无论我是以编程方式断开连接还是设备超出范围,我都会收到状态 0。根据我对以前 Android 版本的理解,状态 0 是通过编程方式断开连接,状态 8 是设备超出范围。
return object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
val name = gatt.device.name
Log.i("onConnectionStateChange", name + "\t" + status + "\t" + newState)
when (newState) {
BluetoothGatt.STATE_CONNECTED -> {
gatt.discoverServices()
}
BluetoothGatt.STATE_CONNECTING -> {
}
BluetoothGatt.STATE_DISCONNECTED -> {
}
BluetoothGatt.STATE_DISCONNECTING -> {
// status 0 (Programmatically disconnected)
if (status == BluetoothGatt.GATT_SUCCESS) {
// Always going in here
...
}
// Deivce went out of range
else if(status == 8){
// Never in here
...
}
}
}
}
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
super.onServicesDiscovered(gatt, status)
}
}
有没有人遇到同样的问题,并弄清楚如何让状态正确显示,或者找到另一种方法来确定设备是否超出范围或以编程方式断开连接?
我确认你对 Android 12 的说法:在 BLE 断开连接时,传入的状态参数的值onConnectionStateChange()
是 0 (即BluetoothGatt.GATT_SUCCESS
)断开连接是通过编程方式完成还是因为设备超出范围或以电气方式关闭。
与之前的 Android 版本相比,这是一个缺点,之前的 Android 版本会通过状态 8(0x08 = GATT CONN TIMEOUT)
当设备超出范围(或被电气关闭)时,状态 0 当连接以编程方式成功关闭时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)