Skip to content

DEV-1250: Verisense BLE radio no longer blocks the main thread on the MTU; fail on notify/connect errors - #130

Merged
JongChern merged 6 commits into
masterfrom
DEV-1250_verisense_ble_connect_main_thread
Oct 20, 2026
Merged

JongChern merged 6 commits into
masterfrom
DEV-1250_verisense_ble_connect_main_thread

Conversation

@JongChern

@JongChern JongChern commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

The same defect as DEV-1247 (Shimmer3BLEAndroid, #129), fixed in the other Android BLE radio, VerisenseBleAndroidRadioByteCommunication.

That class is used by:

  • ShimmerBluetoothManagerAndroid.connectVerisenseDevice → VerisenseDevice
  • the Verisense examples
  • the shimmerspeedtest example, through its subclasses Shimmer3BleAndroidRadioByteCommunication and Shimmer3RAndroidRadioByteCommunication

Problem

  1. Every connect froze the UI for ~3 s. onConnectSuccess runs on FastBle's main-thread handler and waited 3 s for an MTU result that FastBle 2.4.0 delivers on that same thread, so the wait always timed out.
  2. eventConnected() came too early. It fired before the RX notification was even requested, and connect() returned about 200 ms later. VerisenseDevice.connect() then sends readStatus() whether or not notifications are on.
  3. Notify failures were silent. onNotifyFailure was empty: a failed subscription left the device "connected", with every read timing out.

Change

Only VerisenseBleAndroidRadioByteCommunication.java changes, using the same structure as DEV-1247.

  • On the MTU result (success or failure), onMtuSet() requests connection priority, then the RX notification, then the PHY.
  • onNotifySuccess → onNotifyEnabled() calls eventConnected() and lets connect() return.
  • A connect failure, a notify failure, or a missing RX characteristic or services list calls connectionFailed(reason). That logs the reason, disconnects, calls eventDisconnected(), and connect() returns at once.
  • connect()'s signature is unchanged.

Bench results

The HIL doesn't drive this class, so I used a throwaway instrumentation probe (not in this PR). It called Shimmer3RAndroidRadioByteCommunication.connect(), wrote GET_SHIMMER_VERSION (0x3F), waited for the reply and disconnected, 5 times on each build. Phone: Redmi M2101K6G, Android 11. Device: Shimmer3R.

master this PR
connect() returns 4.42–4.72 s 1.48–1.63 s
eventConnected() 4.20–4.50 s 1.28–1.43 s
reply to the first command 5/5 (FF 25 0A) 5/5 (FF 25 0A, 37–49 ms)
PerfMonitor longMsg … BleBluetooth$MainHandler 5, one per connect 0

Not verified

  • No Verisense on the bench. The VerisenseDevice path (readStatus, production and operational config) did not run on hardware. Only the shared radio code did, through the Shimmer3R subclass.
  • Failure paths, partly. The connect-failure path did run on the bench. Two real GATT 133s during the probe went through this class's connectionFailed: eventDisconnected fired ~330 ms after connect() started, and connect() returned at once, where it used to wait 5 s. The notify-failure and missing-RX paths were not forced.
  • Possible higher GATT 133 rate. This is open, and the same question applies to DEV-1247: BLE connect no longer blocks the main thread on the MTU; fail on notify/connect errors #129, whose description has the numbers: 4 GATT 133s in ~105 connects with these changes against 0 in ~68 on master, no mechanism found, and a longer A/B soak needed.
  • Left alone (pre-existing). onDisConnected here only prints, so a link that drops after connect is still not reported to the listener.

Jira: DEV-1250

🤖 Generated with Claude Code

… fail on notify/connect errors

The same defect DEV-1247 fixed in Shimmer3BLEAndroid. onConnectSuccess
runs on FastBle's main-thread handler and waited 3 s for an MTU result that
FastBle delivers on that same thread, so every connect froze the UI for
~3 s. It then reported eventConnected before the RX notification was even
requested. Setup now continues from the MTU callback (success or failure),
and eventConnected and connect()'s return wait for the notification to be
enabled.

A failed notify subscription, a missing RX characteristic or a connect
failure now logs the reason, disconnects and calls eventDisconnected, and
connect() returns at once. Shimmer3BleAndroidRadioByteCommunication and
Shimmer3RAndroidRadioByteCommunication inherit this.

Bench probe through Shimmer3RAndroidRadioByteCommunication on a Shimmer3R:
connect() 1.5 s (was 4.4-4.7 s), first command answered 5/5, no PerfMonitor
longMsg from BleBluetooth$MainHandler (one per connect before).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

What changed in this PR

Updates Verisense BLE connection sequencing to avoid main-thread MTU blocking and report connection or notification failures.

Changes:

  • Continues setup asynchronously after MTU completion.
  • Reports connected only after RX notifications are enabled.
  • Fails fast for connection, notification, and missing-characteristic errors.
File Description
ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java Updated as part of this pull request.

Review effort: Lite

…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]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

One or more issues must be addressed before approval.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Ignore late connect success after the attempt has already failed

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:85

A late onConnectSuccess can still reach this block after onConnectFail has completed mTaskConnect and requested disconnect (FastBle can deliver a result after its timeout). This starts MTU/service/notify setup on a connection that was already reported failed; onNotifyEnabled() only suppresses the later event and does not tear down that late link. Capture the completion source for this connect attempt and gate this callback, disconnecting and returning when that attempt is already complete.

Medium severity Preserve distinct errors for missing services and RX characteristic

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:195

When getBluetoothGattServices() returns null, this branch reports RX characteristic not found, which hides the service-discovery failure from the new connection log. Keep the two failure reasons distinct so callers can diagnose a missing services list versus a missing RX characteristic.

Review effort: Lite

… late success after a failure

The same changes as DEV-1247's second review round, for the identical code here:
- onDisConnected before setup finished now calls connectionFailed(), so
  eventDisconnected fires and connect() returns at once instead of after an
  MTU/notify timeout or connect()'s 5 s wait.
- connect() keeps this attempt's own task, 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.

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]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Late callback can overwrite state for a newer connection attempt

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:85

This late-callback check only tests whether this callback's task is completed; it does not verify that the task is still the current connection attempt. If connect() is called again while an earlier FastBle callback is pending (or after its timeout), the old success can pass this check, overwrite mBleDevice/mTaskMTU, and set up the newer attempt's state. Also compare taskConnect with mTaskConnect before proceeding.

Medium severity Connection timeout leaves task incomplete and GATT link attached

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:127

When the five-second wait times out, this only prints Connect fail; it leaves taskConnect incomplete and does not disconnect the GATT link. A late MTU/notify callback can then report eventConnected() after connect() has already returned, while a missing callback leaves the link attached without a failure notification. Treat the timeout as a failed attempt and clean it up using the same attempt-owned state as the callbacks.

Medium severity Callbacks use mutable task and can complete the wrong attempt

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:154

This helper uses the mutable mTaskConnect instead of the per-callback task captured by the BLE attempt; connectionFailed() below has the same problem. A late notify or failure callback from an older attempt can therefore complete/fail the current attempt and emit the wrong listener event, defeating the per-attempt isolation added in connect(). Pass the captured task through the nested callbacks and gate it against the current attempt.

1 resolved since last review

Review effort: Lite

…etup step

The same as DEV-1247's: 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. The public startServiceS and newConnectedBLEDevice keep their
signatures and use the current attempt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:44
onConnectSuccess returns at once if a newer connect() has replaced this
attempt's task, so it can't overwrite the newer attempt's device or MTU
task. It doesn't disconnect: the newer attempt owns the same device.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Accepts late completion from a stale connection attempt

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:82

taskConnect is only checked for completion here, not for being the current attempt. Since the five-second wait can return false while leaving the task incomplete, a late success from an older timed-out attempt can pass this branch after a new connect() has replaced mTaskConnect, overwrite mBleDevice, and start setup for the stale link. Check the task identity as well before accepting the callback.

Medium severity Delayed MTU callback proceeds after connection failure

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:148

A disconnect can complete taskConnect before the MTU callback arrives, but this method has no current/completed-task guard. The delayed callback can consequently request priority and start service discovery/notification on a link that has already failed (and can race a subsequent connection). Ignore the callback unless its task is still the current, incomplete attempt.

Review effort: Lite

Copilot AI lite review requested due to automatic review settings October 20, 2026 07:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Guard stale MTU callbacks before starting notifications

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​VerisenseBleAndroidRadioByteCommunication.java:143

The per-attempt task is only checked when notification succeeds, so a late MTU callback from an attempt that already failed (or from an older attempt after a new connect() replaced mTaskConnect) still requests priority and starts notification on the stale GATT here. That can re-enable notifications on a failed/old link and leak callbacks even though onNotifyEnabled() later ignores the result. Guard onMtuSet before touching gatt with the same current-and-incomplete check used by onNotifyEnabled/connectionFailed.

Review effort: Lite

…attempt

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.

Probe through Shimmer3RAndroidRadioByteCommunication on a Shimmer3R: 5/5.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No unresolved issues were identified that would block approval.

No open issues 🎉

Review effort: Lite

@JongChern
JongChern merged commit 4e7b307 into master Oct 20, 2026
2 checks passed
@JongChern
JongChern deleted the DEV-1250_verisense_ble_connect_main_thread branch October 20, 2026 08:14
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.

2 participants