Medtrum: Improve BLE connection recovery using handler thread#4582
Medtrum: Improve BLE connection recovery using handler thread#4582HuismanErik wants to merge 2 commits intonightscout:devfrom
Conversation
0109088 to
d1bad3c
Compare
|
any testers? |
| // CLosing asynchronously to avoid deadlocks or silent failures in the Android BLE callback thread during disconnect. | ||
| handler.post { | ||
| close() | ||
| isConnected = false |
There was a problem hiding this comment.
Setting these 2 flags async can cause race conditions
|
Before applying this fix, did you test with scanning enabled? This setting: This usually takes care of hardware specific connection issues |
1 similar comment
|
Before applying this fix, did you test with scanning enabled? This setting: This usually takes care of hardware specific connection issues |
|
|
Addressed the race condition raised by @jbr7rr: moved the flag resets into close() itself, which is already Regarding "scan on connection error": I tested this but it didn't help. The issue seems to be related to a stall on the Binder thread on some specific devices, which prevents onBLEDisconnected() from ever firing — scanning more aggressively didn't fix that. |



This PR resolves "Pump unreachable" issues where the Medtrum driver stays stuck in the
CONNECTINGstate, often following a GATT status 133 error. While other drivers (like Dash) recover correctly on the same hardware, the Medtrum implementation can fail to triggermCallback?.onBLEDisconnected(). This causes theQueueWorkerto stall and skip new connection attempts.The root cause is performing a
close()and state updates directly on the Bluetooth callback (Binder) thread, which can lead to deadlocks or silent connection stalls. Wrapping the GATT cleanup and state updates in a Handler.post() ensures these operations are executed asynchronously. This allows the onConnectionStateChange thread to finalize its lifecycle without being blocked.In my testing, this change eliminated the "Pump unreachable" connection issues on my Xiaomi device. Additional testing by other Medtrum users would be appreciated to confirm correct behavior across different Android devices.