Skip to content

Medtrum: Improve BLE connection recovery using handler thread#4582

Open
HuismanErik wants to merge 2 commits intonightscout:devfrom
HuismanErik:medtrum-ble-connection-improvement
Open

Medtrum: Improve BLE connection recovery using handler thread#4582
HuismanErik wants to merge 2 commits intonightscout:devfrom
HuismanErik:medtrum-ble-connection-improvement

Conversation

@HuismanErik
Copy link
Copy Markdown
Contributor

This PR resolves "Pump unreachable" issues where the Medtrum driver stays stuck in the CONNECTING state, often following a GATT status 133 error. While other drivers (like Dash) recover correctly on the same hardware, the Medtrum implementation can fail to trigger mCallback?.onBLEDisconnected(). This causes the QueueWorker to 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.

@MilosKozak MilosKozak force-pushed the dev branch 2 times, most recently from 0109088 to d1bad3c Compare March 11, 2026 14:17
@MilosKozak
Copy link
Copy Markdown
Contributor

any testers?

// CLosing asynchronously to avoid deadlocks or silent failures in the Android BLE callback thread during disconnect.
handler.post {
close()
isConnected = false
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting these 2 flags async can cause race conditions

@jbr7rr
Copy link
Copy Markdown
Contributor

jbr7rr commented Apr 1, 2026

Before applying this fix, did you test with scanning enabled? This setting:
https://androidaps.readthedocs.io/en/latest/CompatiblePumps/MedtrumNano.html#scan-on-connection-error

This usually takes care of hardware specific connection issues

1 similar comment
@jbr7rr
Copy link
Copy Markdown
Contributor

jbr7rr commented Apr 1, 2026

Before applying this fix, did you test with scanning enabled? This setting:
https://androidaps.readthedocs.io/en/latest/CompatiblePumps/MedtrumNano.html#scan-on-connection-error

This usually takes care of hardware specific connection issues

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 4, 2026

@HuismanErik
Copy link
Copy Markdown
Contributor Author

Addressed the race condition raised by @jbr7rr: moved the flag resets into close() itself, which is already @Synchronized, so state is always updated with the GATT cleanup.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants