From edb26273b4377057c9eb8098b673afd8c29320e5 Mon Sep 17 00:00:00 2001 From: JongChern Date: Tue, 20 Oct 2026 14:16:38 +0800 Subject: [PATCH 1/6] DEV-1250 Verisense BLE radio: don't block the main thread on the MTU; 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 --- ...senseBleAndroidRadioByteCommunication.java | 93 +++++++++++++------ 1 file changed, 63 insertions(+), 30 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java index 33a89603..874db229 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java @@ -72,47 +72,32 @@ public void onStartConnect() { @Override public void onConnectFail(BleDevice bleDevice, BleException exception) { - if (mByteCommunicationListener != null) { - mByteCommunicationListener.eventDisconnected(); - } + connectionFailed("Connect Failure: " + exception); } @Override - public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) { + public void onConnectSuccess(final BleDevice bleDevice, final BluetoothGatt gatt, int status) { + mBleDevice = bleDevice; mTaskMTU= new TaskCompletionSource<>(); + // FastBle runs this and the MTU callback on the main thread, so waiting here for the + // MTU would block the UI and always time out. Setup continues from the callback instead. BleManager.getInstance().setMtu(bleDevice, 251, new BleMtuChangedCallback() { @Override public void onSetMTUFailure(BleException exception) { - System.out.println("MTU Failure"); - + System.out.println("MTU Failure: " + exception); + if (mTaskMTU.trySetResult("MTU Failure")) { + onMtuSet(bleDevice, gatt); + } } @Override public void onMtuChanged(int mtu) { System.out.println("MTU Changed: " + mtu); - mTaskMTU.setResult("MTU Changed: " + mtu); + if (mTaskMTU.trySetResult("MTU Changed: " + mtu)) { + onMtuSet(bleDevice, gatt); + } } }); - - try { - boolean result = mTaskMTU.getTask().waitForCompletion(3, TimeUnit.SECONDS); - } catch (InterruptedException e) { - e.printStackTrace(); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); - } - - if (mByteCommunicationListener != null) { - mByteCommunicationListener.eventConnected(); - } - mBleDevice = bleDevice; - startServiceS(bleDevice); - System.out.println(bleDevice.getMac() + " Connected"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED); - } - mTaskConnect.setResult("Connected"); } @Override @@ -133,13 +118,56 @@ public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, Bl } } + /** + * Continues connect once the MTU request has finished, whether or not it succeeded. + * Connected is reported once the RX notification is enabled, see {@link #onNotifyEnabled(BleDevice)} + */ + private void onMtuSet(BleDevice bleDevice, BluetoothGatt gatt) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); + } + + startServiceS(bleDevice); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED); + } + } + + /** + * Finishes connect once the RX notification is enabled, as no reply from the device can arrive before then + */ + private void onNotifyEnabled(BleDevice bleDevice) { + if (!mTaskConnect.trySetResult("Connected")) { + return; + } + System.out.println(bleDevice.getMac() + " Connected"); + if (mByteCommunicationListener != null) { + mByteCommunicationListener.eventConnected(); + } + } + + /** + * The connection attempt failed: drop the link and tell the listener + */ + private void connectionFailed(String reason) { + System.out.println(mMac + " " + reason); + if (!mTaskConnect.trySetResult(reason)) { + return; + } + BleManager.getInstance().disconnect(mBleDevice); + if (mByteCommunicationListener != null) { + mByteCommunicationListener.eventDisconnected(); + } + } + /** * Get services and start notify for characteristics changed * @param bleDevice BLE device */ public void startServiceS(BleDevice bleDevice){ List services = BleManager.getInstance().getBluetoothGattServices(bleDevice); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + boolean isRxFound = false; + if (services != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { for (BluetoothGattService service:services) { @@ -151,6 +179,7 @@ public void startServiceS(BleDevice bleDevice){ CharSequence text = "TXID!"; } else if (characteristic.getUuid().compareTo(rxid)==0){ + isRxFound = true; newConnectedBLEDevice(bleDevice,characteristic); CharSequence text = "RxID!"; } @@ -160,6 +189,9 @@ else if (characteristic.getUuid().compareTo(rxid)==0){ } + if (!isRxFound) { + connectionFailed("RX characteristic not found"); + } } /** @@ -181,12 +213,13 @@ public void newConnectedBLEDevice(final BleDevice bleDevice, final BluetoothGatt @Override public void onNotifySuccess() { - + System.out.println("Notify Success"); + onNotifyEnabled(bleDevice); } @Override public void onNotifyFailure(BleException exception) { - + connectionFailed("Notify Failure: " + exception); } @Override From 3b00853e6d6eca8bf1952a1a60dabfd1f140061b Mon Sep 17 00:00:00 2001 From: JongChern Date: Tue, 20 Oct 2026 15:20:15 +0800 Subject: [PATCH 2/6] DEV-1250 Review fixes from #129: disconnect the callback's device; per-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 --- ...isenseBleAndroidRadioByteCommunication.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java index 874db229..8468be4f 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java @@ -72,20 +72,22 @@ public void onStartConnect() { @Override public void onConnectFail(BleDevice bleDevice, BleException exception) { - connectionFailed("Connect Failure: " + exception); + connectionFailed(bleDevice, "Connect Failure: " + exception); } @Override public void onConnectSuccess(final BleDevice bleDevice, final BluetoothGatt gatt, int status) { mBleDevice = bleDevice; - mTaskMTU= new TaskCompletionSource<>(); + // This attempt's own task, so a late MTU result can't complete a later attempt's + final TaskCompletionSource taskMTU = new TaskCompletionSource<>(); + mTaskMTU = taskMTU; // FastBle runs this and the MTU callback on the main thread, so waiting here for the // MTU would block the UI and always time out. Setup continues from the callback instead. BleManager.getInstance().setMtu(bleDevice, 251, new BleMtuChangedCallback() { @Override public void onSetMTUFailure(BleException exception) { System.out.println("MTU Failure: " + exception); - if (mTaskMTU.trySetResult("MTU Failure")) { + if (taskMTU.trySetResult("MTU Failure")) { onMtuSet(bleDevice, gatt); } } @@ -93,7 +95,7 @@ public void onSetMTUFailure(BleException exception) { @Override public void onMtuChanged(int mtu) { System.out.println("MTU Changed: " + mtu); - if (mTaskMTU.trySetResult("MTU Changed: " + mtu)) { + if (taskMTU.trySetResult("MTU Changed: " + mtu)) { onMtuSet(bleDevice, gatt); } } @@ -149,12 +151,12 @@ private void onNotifyEnabled(BleDevice bleDevice) { /** * The connection attempt failed: drop the link and tell the listener */ - private void connectionFailed(String reason) { + private void connectionFailed(BleDevice bleDevice, String reason) { System.out.println(mMac + " " + reason); if (!mTaskConnect.trySetResult(reason)) { return; } - BleManager.getInstance().disconnect(mBleDevice); + BleManager.getInstance().disconnect(bleDevice); if (mByteCommunicationListener != null) { mByteCommunicationListener.eventDisconnected(); } @@ -190,7 +192,7 @@ else if (characteristic.getUuid().compareTo(rxid)==0){ } if (!isRxFound) { - connectionFailed("RX characteristic not found"); + connectionFailed(bleDevice, "RX characteristic not found"); } } @@ -219,7 +221,7 @@ public void onNotifySuccess() { @Override public void onNotifyFailure(BleException exception) { - connectionFailed("Notify Failure: " + exception); + connectionFailed(bleDevice, "Notify Failure: " + exception); } @Override From a8b8aad012ae373c7f95a085e3072297899505ba Mon Sep 17 00:00:00 2001 From: JongChern Date: Tue, 20 Oct 2026 15:30:09 +0800 Subject: [PATCH 3/6] DEV-1250 Review fixes: fail a setup-time disconnect at once; ignore a 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 --- ...senseBleAndroidRadioByteCommunication.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java index 8468be4f..f6282531 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java @@ -63,7 +63,9 @@ public VerisenseBleAndroidRadioByteCommunication(String mac) { */ @Override public void connect() { - mTaskConnect = new TaskCompletionSource<>(); + // Task for this attempt only, so a late callback from an earlier attempt can't complete or fail this one + final TaskCompletionSource taskConnect = new TaskCompletionSource<>(); + mTaskConnect = taskConnect; BleManager.getInstance().connect(mMac, new BleGattCallback() { @Override public void onStartConnect() { @@ -77,8 +79,13 @@ public void onConnectFail(BleDevice bleDevice, BleException exception) { @Override public void onConnectSuccess(final BleDevice bleDevice, final BluetoothGatt gatt, int status) { + if (taskConnect.getTask().isCompleted()) { + // This attempt has already failed, so drop a late success instead of setting it up + BleManager.getInstance().disconnect(bleDevice); + return; + } mBleDevice = bleDevice; - // This attempt's own task, so a late MTU result can't complete a later attempt's + // MTU task for this attempt only, so a late MTU result can't complete the MTU task of a later attempt final TaskCompletionSource taskMTU = new TaskCompletionSource<>(); mTaskMTU = taskMTU; // FastBle runs this and the MTU callback on the main thread, so waiting here for the @@ -104,13 +111,17 @@ public void onMtuChanged(int mtu) { @Override public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) { + // Dropped before setup finished: fail the connect now rather than wait for the MTU or notify timeout + if (!taskConnect.getTask().isCompleted() && taskConnect == mTaskConnect) { + connectionFailed(bleDevice, "Disconnected during connect"); + } System.out.println(); } }); try { - boolean result = mTaskConnect.getTask().waitForCompletion(5, TimeUnit.SECONDS); + boolean result = taskConnect.getTask().waitForCompletion(5, TimeUnit.SECONDS); Thread.sleep(200); if (!result) { System.out.println("Connect fail"); @@ -168,8 +179,12 @@ private void connectionFailed(BleDevice bleDevice, String reason) { */ public void startServiceS(BleDevice bleDevice){ List services = BleManager.getInstance().getBluetoothGattServices(bleDevice); + if (services == null) { + connectionFailed(bleDevice, "GATT services not available"); + return; + } boolean isRxFound = false; - if (services != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { for (BluetoothGattService service:services) { From 7a9f1743dfa624181c51f0217889b5a0617c93d0 Mon Sep 17 00:00:00 2001 From: JongChern Date: Tue, 20 Oct 2026 15:44:30 +0800 Subject: [PATCH 4/6] DEV-1250 Review fix: carry the connect attempt's task through every setup 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 --- ...senseBleAndroidRadioByteCommunication.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java index f6282531..6cb4f09e 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java @@ -74,7 +74,7 @@ public void onStartConnect() { @Override public void onConnectFail(BleDevice bleDevice, BleException exception) { - connectionFailed(bleDevice, "Connect Failure: " + exception); + connectionFailed(taskConnect, bleDevice, "Connect Failure: " + exception); } @Override @@ -95,7 +95,7 @@ public void onConnectSuccess(final BleDevice bleDevice, final BluetoothGatt gatt public void onSetMTUFailure(BleException exception) { System.out.println("MTU Failure: " + exception); if (taskMTU.trySetResult("MTU Failure")) { - onMtuSet(bleDevice, gatt); + onMtuSet(taskConnect, bleDevice, gatt); } } @@ -103,7 +103,7 @@ public void onSetMTUFailure(BleException exception) { public void onMtuChanged(int mtu) { System.out.println("MTU Changed: " + mtu); if (taskMTU.trySetResult("MTU Changed: " + mtu)) { - onMtuSet(bleDevice, gatt); + onMtuSet(taskConnect, bleDevice, gatt); } } }); @@ -112,8 +112,8 @@ public void onMtuChanged(int mtu) { @Override public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) { // Dropped before setup finished: fail the connect now rather than wait for the MTU or notify timeout - if (!taskConnect.getTask().isCompleted() && taskConnect == mTaskConnect) { - connectionFailed(bleDevice, "Disconnected during connect"); + if (!taskConnect.getTask().isCompleted()) { + connectionFailed(taskConnect, bleDevice, "Disconnected during connect"); } System.out.println(); } @@ -133,14 +133,15 @@ public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, Bl /** * Continues connect once the MTU request has finished, whether or not it succeeded. - * Connected is reported once the RX notification is enabled, see {@link #onNotifyEnabled(BleDevice)} + * Connected is reported once the RX notification is enabled, see {@link #onNotifyEnabled(TaskCompletionSource, BleDevice)}. + * Each step carries its connect attempt's task, so results from an attempt that is no longer current are ignored. */ - private void onMtuSet(BleDevice bleDevice, BluetoothGatt gatt) { + private void onMtuSet(TaskCompletionSource taskConnect, BleDevice bleDevice, BluetoothGatt gatt) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); } - startServiceS(bleDevice); + startServiceS(taskConnect, bleDevice); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED); } @@ -149,8 +150,8 @@ private void onMtuSet(BleDevice bleDevice, BluetoothGatt gatt) { /** * Finishes connect once the RX notification is enabled, as no reply from the device can arrive before then */ - private void onNotifyEnabled(BleDevice bleDevice) { - if (!mTaskConnect.trySetResult("Connected")) { + private void onNotifyEnabled(TaskCompletionSource taskConnect, BleDevice bleDevice) { + if (taskConnect != mTaskConnect || !taskConnect.trySetResult("Connected")) { return; } System.out.println(bleDevice.getMac() + " Connected"); @@ -162,9 +163,9 @@ private void onNotifyEnabled(BleDevice bleDevice) { /** * The connection attempt failed: drop the link and tell the listener */ - private void connectionFailed(BleDevice bleDevice, String reason) { + private void connectionFailed(TaskCompletionSource taskConnect, BleDevice bleDevice, String reason) { System.out.println(mMac + " " + reason); - if (!mTaskConnect.trySetResult(reason)) { + if (taskConnect != mTaskConnect || !taskConnect.trySetResult(reason)) { return; } BleManager.getInstance().disconnect(bleDevice); @@ -178,9 +179,13 @@ private void connectionFailed(BleDevice bleDevice, String reason) { * @param bleDevice BLE device */ public void startServiceS(BleDevice bleDevice){ + startServiceS(mTaskConnect, bleDevice); + } + + private void startServiceS(TaskCompletionSource taskConnect, BleDevice bleDevice){ List services = BleManager.getInstance().getBluetoothGattServices(bleDevice); if (services == null) { - connectionFailed(bleDevice, "GATT services not available"); + connectionFailed(taskConnect, bleDevice, "GATT services not available"); return; } boolean isRxFound = false; @@ -197,7 +202,7 @@ public void startServiceS(BleDevice bleDevice){ } else if (characteristic.getUuid().compareTo(rxid)==0){ isRxFound = true; - newConnectedBLEDevice(bleDevice,characteristic); + newConnectedBLEDevice(taskConnect, bleDevice,characteristic); CharSequence text = "RxID!"; } } @@ -207,7 +212,7 @@ else if (characteristic.getUuid().compareTo(rxid)==0){ } if (!isRxFound) { - connectionFailed(bleDevice, "RX characteristic not found"); + connectionFailed(taskConnect, bleDevice, "RX characteristic not found"); } } @@ -217,6 +222,10 @@ else if (characteristic.getUuid().compareTo(rxid)==0){ * @param characteristic */ public void newConnectedBLEDevice(final BleDevice bleDevice, final BluetoothGattCharacteristic characteristic) { + newConnectedBLEDevice(mTaskConnect, bleDevice, characteristic); + } + + private void newConnectedBLEDevice(final TaskCompletionSource taskConnect, final BleDevice bleDevice, final BluetoothGattCharacteristic characteristic) { int count = 1; @@ -231,12 +240,12 @@ public void newConnectedBLEDevice(final BleDevice bleDevice, final BluetoothGatt @Override public void onNotifySuccess() { System.out.println("Notify Success"); - onNotifyEnabled(bleDevice); + onNotifyEnabled(taskConnect, bleDevice); } @Override public void onNotifyFailure(BleException exception) { - connectionFailed(bleDevice, "Notify Failure: " + exception); + connectionFailed(taskConnect, bleDevice, "Notify Failure: " + exception); } @Override From aef0d74dda02aa1eb61314148caca26b36430c54 Mon Sep 17 00:00:00 2001 From: JongChern Date: Tue, 20 Oct 2026 15:45:56 +0800 Subject: [PATCH 5/6] DEV-1250 Review fix: ignore a connect success from a superseded attempt 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 --- .../VerisenseBleAndroidRadioByteCommunication.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java index 6cb4f09e..e9c8a9c1 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java @@ -79,6 +79,10 @@ public void onConnectFail(BleDevice bleDevice, BleException exception) { @Override public void onConnectSuccess(final BleDevice bleDevice, final BluetoothGatt gatt, int status) { + if (taskConnect != mTaskConnect) { + // A newer connect() owns this device now, so leave its link alone + return; + } if (taskConnect.getTask().isCompleted()) { // This attempt has already failed, so drop a late success instead of setting it up BleManager.getInstance().disconnect(bleDevice); From 1327eb3d2073061d92c0d6275e298aff59a7fb1f Mon Sep 17 00:00:00 2001 From: JongChern Date: Tue, 20 Oct 2026 15:53:49 +0800 Subject: [PATCH 6/6] DEV-1250 Review fix: skip a late MTU result for a failed or replaced 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 --- .../VerisenseBleAndroidRadioByteCommunication.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java index e9c8a9c1..22cf44fc 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/VerisenseBleAndroidRadioByteCommunication.java @@ -141,6 +141,10 @@ public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, Bl * Each step carries its connect attempt's task, so results from an attempt that is no longer current are ignored. */ private void onMtuSet(TaskCompletionSource taskConnect, BleDevice bleDevice, BluetoothGatt gatt) { + if (taskConnect != mTaskConnect || taskConnect.getTask().isCompleted()) { + // The attempt failed or a newer one replaced it while the MTU was pending + return; + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); }