When testing with an Android 12 device whenever my Bluetooth device disconnects I receive a status 0 regardless if I disconnect programmatically or the device went out of range. From my understanding in previous Android versions status 0 is programmatically disconnected and status 8 was for device went out of range.
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)
}
}
Has anyone face this same problem and figured out how to get the status to show correctly or found another way to determine if it was device out of range or a disconnect programmatically?