From b7cce2b6bd2cea22390866e9efd1fbf29769e790 Mon Sep 17 00:00:00 2001 From: JongChern Date: Fri, 23 Oct 2026 21:04:59 +0800 Subject: [PATCH] DEV-1275 Over BLE too, report a Shimmer ready only once its connect sequence has ended The Android HIL's connectReady found a Shimmer3R over BLE reported ready 2.7 s before its connect sequence ended, with driver 0.11.11_beta. #132 ports the driver's fix to Shimmer, the Classic class; Shimmer3BLEAndroid had the same defect, in two places: - it sent the app a CONNECTED state message as soon as the BLE notifications were on, before initialize() queued the connect sequence; - like Shimmer, it overrides the status handler, and a status reply part way through the sequence moved the state on: to SDLOGGING for a logging unit, which also sets mIsInitialised, and to CONNECTED once connected. When the notifications come on, the state is now CONNECTING. While CONNECTING, a status reply leaves the state alone. The sequence's own end, isReadyForStreaming(), reports the unit ready: SDLOGGING for a unit the status showed logging, since the callback adapter only moves CONNECTING to CONNECTED. AAPI_00006 hands a status reply to the driver's parser while CONNECTING. Without the fix the idle case reports Ready and the logging case SD Logging before the sequence ends; with it both wait for the end. Co-Authored-By: Claude Opus 5.5 --- .../Shimmer3BLEAndroid.java | 20 ++- ...eConnectReadyAfterConnectSequenceTest.java | 167 ++++++++++++++++++ 2 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00006_BleConnectReadyAfterConnectSequenceTest.java diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/Shimmer3BLEAndroid.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/Shimmer3BLEAndroid.java index 0b6f2e14..9e3b6338 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/Shimmer3BLEAndroid.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/androidradiodriver/Shimmer3BLEAndroid.java @@ -236,9 +236,9 @@ private void onNotifyEnabled(TaskCompletionSource taskConnect, BleDevice return; } System.out.println(bleDevice.getMac() + " Connected"); - //mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, - //new ObjectCluster("", bleDevice.getMac(), BT_STATE.CONNECTED)).sendToTarget(); - sendCallBackMsg(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, new ObjectCluster("", bleDevice.getMac(), BT_STATE.CONNECTED)); + // The link is up, but the connect sequence (initialize()) has yet to run: the unit is + // connecting, not ready. Its end, isReadyForStreaming(), reports it ready (DEV-1275). + setBluetoothRadioState(BT_STATE.CONNECTING); Bundle bundle = new Bundle(); bundle.putString(TOAST, "Device connection established"); sendMsgToHandlerList(MESSAGE_TOAST, bundle); @@ -394,6 +394,12 @@ protected void sendProgressReport(BluetoothProgressReportPerCmd bluetoothProgres @Override protected void isReadyForStreaming() { + // The end of the connect sequence. A status reply while connecting leaves the state to + // this (DEV-1275), so a unit found logging is reported SDLOGGING here; the adapter moves + // any other from CONNECTING to CONNECTED. + if (mBluetoothRadioState == BT_STATE.CONNECTING && isSDLogging()) { + setBluetoothRadioState(BT_STATE.SDLOGGING); + } mDeviceCallbackAdapter.isReadyForStreaming(); restartTimersIfNull(); } @@ -483,6 +489,14 @@ protected void startOperation(BT_STATE bt_state, int i) { @Override protected void eventLogAndStreamStatusChanged(byte currentCommand) { + // While connecting, the connect sequence's own end (inquiryDone(), then + // isReadyForStreaming()) reports the unit ready. The sequence reads the status part way + // through, and if that reply moved the state, the app would be told the unit is ready + // while the rest of the sequence is still queued (DEV-1275). SDLOGGING also sets + // mIsInitialised. + if (mBluetoothRadioState == BT_STATE.CONNECTING) { + return; + } if (currentCommand == STOP_LOGGING_ONLY_COMMAND) { //TODO need to query the Bluetooth connection here! if (mIsStreaming) { diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00006_BleConnectReadyAfterConnectSequenceTest.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00006_BleConnectReadyAfterConnectSequenceTest.java new file mode 100644 index 00000000..70cd8f90 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00006_BleConnectReadyAfterConnectSequenceTest.java @@ -0,0 +1,167 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.List; + +import org.junit.Test; + +import com.shimmerresearch.androidradiodriver.Shimmer3BLEAndroid; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; +import com.shimmerresearch.driver.ShimmerObject; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; + +/** + * DEV-1275 over BLE: Shimmer3BLEAndroid told the app the unit was ready before its connect + * sequence had run. It sent a CONNECTED state message as soon as the BLE notifications were on, + * before initialize() queued the sequence, and a status reply part way through the sequence moved + * the state to SDLOGGING for a logging unit, which also sets mIsInitialised. The Android HIL saw a + * Shimmer3R reported ready 2.7 s before the sequence ended, with driver 0.11.11_beta: the driver's + * fix is in ShimmerPC, and Shimmer3BLEAndroid overrides the status handler. + *

+ * The sequence's own end, inquiryDone() and then isReadyForStreaming(), now reports the unit + * ready, as SDLOGGING when it is logging. The message sent when the notifications come on can't be + * reached without a phone; the Android HIL's connectReady checks it. + *

+ * No hardware: the status reply is read from an in-memory byte queue by the driver's own parser, + * as the IO thread does while not streaming. + */ +public class AAPI_00006_BleConnectReadyAfterConnectSequenceTest { + + /** Not docked, sensing or logging; RTC set */ + private static final byte STATUS_IDLE = 0x04; + + /** Sensing and SD logging; RTC set */ + private static final byte STATUS_SD_LOGGING = 0x0E; + + /** A Shimmer3BLEAndroid whose BLE link is in memory, and which records every state it is set to */ + private static class QueueShimmer extends Shimmer3BLEAndroid { + final Deque rx = new ArrayDeque(); + final List states = new ArrayList(); + + QueueShimmer() { + super("E7:47:AA:7D:2F:31"); + } + + void queue(byte... bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + /** What the IO thread does with an in-stream response while not streaming, once it has read its ACK and INSTREAM_CMD_RESPONSE */ + void parseInstreamResponse() { + processInstreamResponse(false); + } + + /** The end of the connect sequence: the inquiry's reply */ + void endConnectSequence() { + inquiryDone(); + } + + @Override + public boolean setBluetoothRadioState(BT_STATE state) { + states.add(state); + return super.setBluetoothRadioState(state); + } + + @Override + protected boolean bytesAvailableToBeRead() { + return !rx.isEmpty(); + } + + @Override + protected int availableBytes() { + return rx.size(); + } + + @Override + protected byte[] readBytes(int numberofBytes) { + assertTrue("read " + numberofBytes + " bytes but only " + rx.size() + " had arrived", numberofBytes<=rx.size()); + byte[] bytes = new byte[numberofBytes]; + for(int i=0;i(), device.states); + assertEquals(BT_STATE.CONNECTING, device.getBluetoothRadioState()); + assertFalse("not reported initialised before the sequence ends", device.isInitialised()); + } + + @Test + public void statusReplyWhileConnectingLeavesTheUnitConnecting() { + QueueShimmer device = connectingShimmer3r(); + device.setIsConnected(true); + receiveStatusReply(device, STATUS_IDLE); + assertFalse("the status is still decoded", device.isSDLogging()); + assertStillConnecting(device); + + device.endConnectSequence(); + assertEquals("ready once the sequence ends", Arrays.asList(BT_STATE.CONNECTED), device.states); + assertTrue(device.isInitialised()); + } + + @Test + public void statusReplyWhileConnectingToALoggingUnitLeavesTheUnitConnecting() { + QueueShimmer device = connectingShimmer3r(); + receiveStatusReply(device, STATUS_SD_LOGGING); + assertTrue("the status is still decoded", device.isSDLogging()); + assertStillConnecting(device); + + device.endConnectSequence(); + assertEquals("ready once the sequence ends, as logging", Arrays.asList(BT_STATE.SDLOGGING), device.states); + assertTrue(device.isInitialised()); + } + + /** Once connected, a status reply still moves the state */ + @Test + public void statusReplyAfterConnectingStillChangesTheState() { + QueueShimmer device = connectingShimmer3r(); + device.endConnectSequence(); + assertEquals(BT_STATE.CONNECTED, device.getBluetoothRadioState()); + + receiveStatusReply(device, STATUS_SD_LOGGING); + assertEquals(BT_STATE.SDLOGGING, device.getBluetoothRadioState()); + + receiveStatusReply(device, STATUS_IDLE); + assertEquals(BT_STATE.CONNECTED, device.getBluetoothRadioState()); + } +}