diff --git a/pump/medtrum/src/main/kotlin/app/aaps/pump/medtrum/services/BLEComm.kt b/pump/medtrum/src/main/kotlin/app/aaps/pump/medtrum/services/BLEComm.kt index c7e328a53134..eefcefdbbe2e 100644 --- a/pump/medtrum/src/main/kotlin/app/aaps/pump/medtrum/services/BLEComm.kt +++ b/pump/medtrum/src/main/kotlin/app/aaps/pump/medtrum/services/BLEComm.kt @@ -187,7 +187,6 @@ class BLEComm @Inject internal constructor( } else { aapsLogger.debug(LTag.PUMPBTCOMM, "Not connected, closing gatt") close() - isConnected = false mCallback?.onBLEDisconnected() } } @@ -198,6 +197,8 @@ class BLEComm @Inject internal constructor( mBluetoothGatt?.close() SystemClock.sleep(100) mBluetoothGatt = null + isConnected = false + isConnecting = false } /** Scan callback */ @@ -387,11 +388,12 @@ class BLEComm @Inject internal constructor( // Wait a bit before retrying SystemClock.sleep(2000) } - close() - isConnected = false - isConnecting = false - mCallback?.onBLEDisconnected() - aapsLogger.debug(LTag.PUMPBTCOMM, "Device was disconnected " + gatt.device.name) //Device was disconnected + // CLosing asynchronously to avoid deadlocks or silent failures in the Android BLE callback thread during disconnect. + handler.post { + close() + mCallback?.onBLEDisconnected() + aapsLogger.debug(LTag.PUMPBTCOMM, "Device was disconnected " + gatt.device.name) //Device was disconnected + } } }