当我使用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)
}
}
有人遇到过同样的问题并找到了正确显示状态或以编程方式确定设备是否超出范围或断开连接的另一种方法吗?