【发布时间】:2022-01-20 01:13:08
【问题描述】:
在使用 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 bluetooth android-bluetooth android-12