You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DEV-1247: BLE connect no longer blocks the main thread on the MTU; fail on notify/connect errors - #129
Fixes two defects in Shimmer3BLEAndroid, the BLE path for Shimmer3/3R. The Android HIL (#128) found both.
Problem
Every BLE connect blocked the UI thread for ~3.1 s.onConnectSuccess runs on FastBle's main-thread handler. It called setMtu(251) and then waited up to 3 s on mTaskMTU. FastBle 2.4.0 delivers onMtuChanged on that same main thread, so the wait always timed out. The MTU exchange itself finishes in about 25 ms. Logcat showed PerfMonitor longMsg ... wall≈3130ms h=com.clj.fastble.bluetooth.BleBluetooth$MainHandler on every connect, and MTU Changed was printed after <mac> Connected.
Notify and connect failures were silent.onNotifyFailure and onConnectFail were empty. If the RX subscription failed, the driver still called initialize() and timed out on commands with nothing in the log. If the GATT connect failed, the state never changed.
Change
Only Shimmer3BLEAndroid.java changes.
onConnectSuccess sends the MTU request and returns. On the MTU result (success or failure), onMtuSet() requests connection priority, then the RX notification, then the PHY.
onNotifySuccess → onNotifyEnabled() does what the end of onConnectSuccess used to do: the CONNECTED message, the toast, the IO and processing threads, and initialize(). The order is MTU → notify → initialize, and nothing waits on the main thread.
A connect failure, a notify failure, or a missing RX characteristic or services list calls connectionFailed(reason). That logs the reason, disconnects, sets CONNECTION_FAILED and lets connect() return.
It sets CONNECTION_FAILED rather than DISCONNECTED because a first connect starts at DISCONNECTED, and the adapter only sends a state message when the state changes.
trySetResult on mTaskMTU and mTaskConnect guards against a callback arriving twice, e.g. FastBle's 5 s timeout followed by a late result.
midStreamSet: fails with "2 timestamps did not increase". This is the known Shimmer3R firmware issue DEV-1245, which fails the same way on master; there were no other failures.
reconnectLoop: 3 of 5 runs passed, i.e. 20 of 22 connects. See the next section.
Not verified / open
Possible higher rate of GATT 133 connect failures. Open, needs a longer A/B soak. All of these were on the Shimmer3R, the same day.
one ~85 s stretch where the unit took no connection (4 attempts)
Each 133 came ~300 ms after Connecting, before FastBle reached onConnectSuccess, i.e. before any code this PR changes runs.
Fisher's test gives p ≈ 0.09, so this is suggestive, not proven.
I found no mechanism:
the disconnect-to-connect gap (~2.3 s), connection interval (12) and PHY (2M) are unchanged;
holding the link 4 s before disconnecting didn't prevent it (7 of 8), and an immediate disconnect went 12 of 12 in another run, so session length isn't the trigger.
What did change is timing: priority, PHY and the first commands now go out ~0.5 s after link-up instead of ~3.5 s.
Before this change, a 133 left the state at DISCONNECTED and the app waited out its timeout with nothing logged. Now it reports CONNECTION_FAILED within ~330 ms, with the GATT status in the log.
Shimmer3 not tested. The bench Shimmer3 was hung (DEV-1246, it stops answering commands). Its logcat still shows the new sequence on a Shimmer3: MTU Changed: 160 25 ms after configureMTU, then Notify Success, then Connected, with no longMsg.
Failure paths. Only the connect-failure path (the GATT 133s above) ran on hardware. The notify-failure and missing-RX paths did not.
… notify/connect errors
onConnectSuccess runs on FastBle's main-thread handler and waited 3 s for
an MTU result that FastBle delivers on that same thread, so the wait always
timed out and every connect froze the UI for ~3.1 s. Setup now continues
from the MTU callback (success or failure) and initialize() waits for the
RX notification to be enabled: MTU, then notify, then initialize.
A failed connect, a failed notify subscription or a missing RX
characteristic now logs the reason, disconnects, sets CONNECTION_FAILED
and lets connect() return, instead of leaving the driver timing out on
commands with nothing in the log.
HIL (-Transport ble, Shimmer3R): connect ~3 s faster, no PerfMonitor
longMsg from BleBluetooth$MainHandler.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…MTU task; no "lost" report for a failed connect
- connectionFailed() disconnects the BleDevice the failing callback was
given, not mBleDevice, which is unset or stale on the onConnectFail path.
- The MTU callback completes a task local to its attempt, so a late MTU
result after a timeout can't complete a later attempt's task.
- onDisConnected only reports DISCONNECTED and "connection was lost" for a
connection that reached Connected. A failed setup is already reported as
CONNECTION_FAILED, and the toasts contradicted each other.
Shimmer3R BLE HIL: streamRest and reconnectLoop (5/5) pass, no main-thread
longMsg.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r-attempt MTU task
The same two fixes as DEV-1247's review, for the identical code here:
- connectionFailed() disconnects the BleDevice the failing callback was
given, not mBleDevice, which is unset or stale on the onConnectFail path.
- The MTU callback completes a task local to its attempt, so a late MTU
result after a timeout can't complete a later attempt's task.
Probe through Shimmer3RAndroidRadioByteCommunication on a Shimmer3R: 5/5
connects at ~1.5 s, first command answered every time.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
When getBluetoothGattServices() returns null, this reports “RX characteristic not found,” which is a different failure and makes the new connect-failure diagnostic misleading. Report the missing services list separately, and reserve the RX message for a non-null list that lacks the characteristic.
… late success after a failure
- onDisConnected before setup finished now calls connectionFailed(), so
connect() returns and CONNECTION_FAILED is set straight away instead of
after an MTU/notify timeout or connect()'s 10 s wait.
- connect() keeps this attempt's own task. onDisConnected, onConnectSuccess
and the wait use it, so a late callback from an earlier attempt can't
complete or fail a newer one.
- A late onConnectSuccess for an attempt that has already failed is
disconnected instead of set up.
- A missing services list is reported as such, not as a missing RX
characteristic.
- Comments on the per-attempt tasks spelled out.
Shimmer3R BLE HIL: streamRest and reconnectLoop (5/5) pass, no main-thread
longMsg.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This completes the mutable mTaskConnect, which may already belong to a later attempt. If an attempt disconnects after onConnectSuccess but before notification completes, its late MTU/notify callback can reach here after a reconnect starts and mark the new attempt Connected, start IO against the old GATT, and initialize it. Carry the captured attempt task/generation through onMtuSet, startServiceS, and the notify callbacks, and ignore callbacks that are no longer current.
…etup step
onMtuSet, startServiceS, newConnectedBLEDevice and their callbacks now carry
the task of the connect attempt they belong to. onNotifyEnabled and
connectionFailed ignore an attempt that is no longer current, so a late
notify or service result from an earlier attempt can't mark a newer one
Connected, fail it, or disconnect it. The public startServiceS and
newConnectedBLEDevice keep their signatures and use the current attempt.
Shimmer3R BLE HIL: streamRest and reconnectLoop (5/5) pass, no main-thread
longMsg.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
onConnectSuccess returns at once if a newer connect() has replaced this
attempt's task, so it can't overwrite the newer attempt's device, buffer or
MTU task. It doesn't disconnect: the newer attempt owns the same device.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The late-success guard checks only whether this attempt is completed, not whether it is still the current attempt. If a newer connect() has replaced mTaskConnect before this callback arrives, this stale success still overwrites the shared mBuffer, mBleDevice, and mTaskMTU; the newer connection can then use the wrong buffer/device. Reject a success whose task is no longer mTaskConnect before changing shared state.
Ignore active or stale disconnect callbacks as connection failures
This treats every pre-Connected disconnect as a connection failure, including an active disconnect requested by the caller (isActiveDisConnected == true), and it does not ignore a disconnect callback from an older attempt. That can overwrite an intentional DISCONNECTED state with CONNECTION_FAILED, or emit failure for a later reconnect. Return for active or non-current callbacks before applying the setup-failure path.
Guard late MTU callbacks after connection attempt failure
A late MTU callback can arrive after onDisConnected or another failure has already completed this attempt. onMtuSet() does not re-check the attempt before calling requestConnectionPriority() and starting service/notify setup, so it can operate on a disconnected/stale GATT and register a notification after failure. Guard this continuation with the same current-task/completed check used by onNotifyEnabled() and disconnect/return when it is stale.
The MTU callback is attempt-local, but this method does not check whether taskConnect is still the current, incomplete attempt. After a reconnect or an earlier failure, a late MTU result can still request priority/PHY and enable notifications on the stale GATT; only the later onNotifyEnabled call is ignored. Guard this method before starting service setup so an obsolete attempt cannot continue setup.
…lure; skip a late MTU result
- onDisConnected before setup finished: an active disconnect (the caller's
own disconnect()) ends the attempt quietly instead of setting
CONNECTION_FAILED over the DISCONNECTED it already set. Only a dropped
link is reported as a failed connect.
- onMtuSet returns at once if its attempt has failed or been replaced while
the MTU was pending, so it doesn't request priority or notifications on a
dead or stale link.
Shimmer3R BLE HIL reconnectLoop 5/5, no main-thread longMsg.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This callback is not filtered by attempt identity once the old task has reached Connected. If disconnect() is followed quickly by a new connect() on the same instance, the old GATT's delayed onDisConnected enters this branch and emits DISCONNECTED/"Device connection was lost" while the newer attempt is active. Return immediately when taskConnect != mTaskConnect, before inspecting the task result.
onDisConnected returns at once if a newer connect() has replaced this
attempt, so an old link going down after disconnect() and a quick reconnect
can't report DISCONNECTED or "connection was lost" over the new attempt.
Shimmer3R BLE HIL reconnectLoop: cycles 1-4 connect and report their
disconnects as before. Cycle 5 hit the separate GATT 133 at link setup that
is open on the PR.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The attempt token now guards notify success/failure, but this same notification callback still writes every characteristic update into the shared mBuffer. If a delayed callback from a failed or older attempt arrives after a reconnect, those stale bytes can be consumed as responses for the current connection. Ignore updates unless this task is still current and either setup is pending or it completed as Connected.
How attempts were classified: from logcat, each connect attempt was classed by the first GATT connection-state callback that followed it.
Arm
Connect attempts
Connected
GATT 133 (HCI 0x3E)
No link within FastBle's 10 s
before
111
100
4
7
after
110
105
2
3
No regression. GATT 133s and no-link attempts happen on the old code too, in clusters that hit whichever arm happened to be running. One-sided Fisher p for "after is worse": 0.89 for 133s, 0.97 for any connect failure. The difference seen earlier (5/110 vs 0/68) came from comparing different sessions.
One unexplained episode, recorded but not attributed: in round 4's "after" block, for about 3.5 minutes, the unit accepted connections but didn't answer a single command.
The link side was fine: link setup completed and notifications were enabled every time.
It recovered by itself.
It matches the symptom described for a Shimmer3 in DEV-1246.
Nothing this PR changes runs after the link is up, so it isn't evidence either way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes two defects in
Shimmer3BLEAndroid, the BLE path for Shimmer3/3R. The Android HIL (#128) found both.Problem
onConnectSuccessruns on FastBle's main-thread handler. It calledsetMtu(251)and then waited up to 3 s onmTaskMTU. FastBle 2.4.0 deliversonMtuChangedon that same main thread, so the wait always timed out. The MTU exchange itself finishes in about 25 ms. Logcat showedPerfMonitor longMsg ... wall≈3130ms h=com.clj.fastble.bluetooth.BleBluetooth$MainHandleron every connect, andMTU Changedwas printed after<mac> Connected.onNotifyFailureandonConnectFailwere empty. If the RX subscription failed, the driver still calledinitialize()and timed out on commands with nothing in the log. If the GATT connect failed, the state never changed.Change
Only
Shimmer3BLEAndroid.javachanges.onConnectSuccesssends the MTU request and returns. On the MTU result (success or failure),onMtuSet()requests connection priority, then the RX notification, then the PHY.onNotifySuccess→onNotifyEnabled()does what the end ofonConnectSuccessused to do: the CONNECTED message, the toast, the IO and processing threads, andinitialize(). The order is MTU → notify → initialize, and nothing waits on the main thread.connectionFailed(reason). That logs the reason, disconnects, setsCONNECTION_FAILEDand letsconnect()return.CONNECTION_FAILEDrather thanDISCONNECTEDbecause a first connect starts atDISCONNECTED, and the adapter only sends a state message when the state changes.trySetResultonmTaskMTUandmTaskConnectguards against a callback arriving twice, e.g. FastBle's 5 s timeout followed by a late result.Bench results
Android HIL,
-Transport ble. Phone: Redmi M2101K6G, Android 11. Driver 0.11.9_beta. Shimmer3R on LogAndStream v1.1.21.PerfMonitor longMsg … BleBluetooth$MainHandlerNot verified / open
Possible higher rate of GATT 133 connect failures. Open, needs a longer A/B soak. All of these were on the Shimmer3R, the same day.
Connecting, before FastBle reachedonConnectSuccess, i.e. before any code this PR changes runs.DISCONNECTEDand the app waited out its timeout with nothing logged. Now it reportsCONNECTION_FAILEDwithin ~330 ms, with the GATT status in the log.Shimmer3 not tested. The bench Shimmer3 was hung (DEV-1246, it stops answering commands). Its logcat still shows the new sequence on a Shimmer3:
MTU Changed: 16025 ms afterconfigureMTU, thenNotify Success, thenConnected, with no longMsg.Failure paths. Only the connect-failure path (the GATT 133s above) ran on hardware. The notify-failure and missing-RX paths did not.
Related
VerisenseBleAndroidRadioByteCommunication, had the same defect. It is fixed separately under DEV-1250 (DEV-1250: Verisense BLE radio no longer blocks the main thread on the MTU; fail on notify/connect errors #130).Jira: DEV-1247
🤖 Generated with Claude Code