From aff9c825bd6dd2bd146241ad922436d4ce3070e8 Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Fri, 6 Nov 2026 13:29:20 +0800 Subject: [PATCH] DEV-1427: ShimmerService works without a BluetoothManager throughout btManager is null while Bluetooth was off when the service was created. DEV-1402 guarded the calls ShimmerCapture reaches; the other 49 methods that use btManager still threw NullPointerException, as did the four library fragments that dereference getBluetoothManager(). Rule: no BluetoothManager means no devices. Every method that uses it first returns what ShimmerBluetoothManagerAndroid returns for a device it does not know: commands do nothing, and queries return -1, 0, false, "", null, DISCONNECTED or an empty list, method by method. handleMsgStateChange() skips the manager for its CONNECTED, SDLOGGING and DISCONNECTED cases, and bluetoothAddressComparator() compares the addresses itself. A null-object manager was not an option: the manager's constructor throws when Bluetooth is off, and other apps rely on that. getBluetoothManager() keeps returning null, now documented. The PlotFragment and SignalsToPlotFragment copies check for it, and SignalsToPlotFragment also no longer throws for a device the service does not know. AAPI_00008 covers every guarded method; its four new tests fail on master's ShimmerService. Co-Authored-By: Claude Opus 5.5 --- .../android/guiUtilities/PlotFragment.java | 7 +- .../guiUtilities/SignalsToPlotFragment.java | 7 +- .../supportfragments/PlotFragment.java | 7 +- .../SignalsToPlotFragment.java | 7 +- .../shimmerService/ShimmerService.java | 70 ++++++++++++- ...08_ServiceWithoutBluetoothManagerTest.java | 97 ++++++++++++++++++- 6 files changed, 177 insertions(+), 18 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/PlotFragment.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/PlotFragment.java index e6d291cb..55ceda77 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/PlotFragment.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/PlotFragment.java @@ -27,6 +27,7 @@ import com.androidplot.xy.XYStepMode; import com.shimmerresearch.android.Shimmer; import com.shimmerresearch.android.shimmerService.ShimmerService; +import com.shimmerresearch.android.manager.ShimmerBluetoothManagerAndroid; import com.shimmerresearch.androidinstrumentdriver.R; import com.shimmerresearch.bluetooth.ShimmerBluetooth; import com.shimmerresearch.driver.CallbackObject; @@ -222,9 +223,11 @@ public void handleMessage(Message msg) { deviceState="Streaming"; textViewDeviceName.setText(mBluetoothAddress); textViewDeviceState.setText(deviceState); - List sensorList = shimmerService.getBluetoothManager().getListofEnabledSensors(mBluetoothAddress); + // No BluetoothManager (Bluetooth was off when the service started) means no sensor list + ShimmerBluetoothManagerAndroid btManager = shimmerService.getBluetoothManager(); + List sensorList = btManager == null ? null : btManager.getListofEnabledSensors(mBluetoothAddress); if(sensorList!=null){ - if(shimmerService.getBluetoothManager().getShimmerVersion(mBluetoothAddress)== ShimmerVerDetails.HW_ID.SHIMMER_3){ + if(btManager.getShimmerVersion(mBluetoothAddress)== ShimmerVerDetails.HW_ID.SHIMMER_3){ sensorList.remove("ECG"); sensorList.remove("EMG"); if(sensorList.contains("EXG1")){ diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/SignalsToPlotFragment.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/SignalsToPlotFragment.java index 60532332..fcf1a387 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/SignalsToPlotFragment.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/SignalsToPlotFragment.java @@ -14,7 +14,6 @@ import androidx.annotation.Nullable; import com.androidplot.xy.XYPlot; -import com.shimmerresearch.android.manager.ShimmerBluetoothManagerAndroid; import com.shimmerresearch.android.shimmerService.ShimmerService; import com.shimmerresearch.androidinstrumentdriver.R; import com.shimmerresearch.driver.ShimmerDevice; @@ -90,10 +89,10 @@ public void buildSignalsToPlotList(Context context, final ShimmerService service this.context = context; shimmerService = service; dynamicPlot = plot; - ShimmerBluetoothManagerAndroid btManager = shimmerService.getBluetoothManager(); - final ShimmerDevice device = btManager.getShimmer(bluetoothAddress); + // Null when the service has no BluetoothManager (Bluetooth off) or does not know the device + final ShimmerDevice device = shimmerService.getShimmer(bluetoothAddress); - if(device.isStreaming()) { + if(device != null && device.isStreaming()) { device.getListofEnabledChannelSignalsandFormats(); channelsMap = device.getMapOfEnabledChannelsForStreaming(); diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/PlotFragment.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/PlotFragment.java index b26f12e2..d52d2ca1 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/PlotFragment.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/PlotFragment.java @@ -30,6 +30,7 @@ import com.androidplot.xy.XYStepMode; import com.shimmerresearch.android.Shimmer; import com.shimmerresearch.android.shimmerService.ShimmerService; +import com.shimmerresearch.android.manager.ShimmerBluetoothManagerAndroid; import com.shimmerresearch.androidinstrumentdriver.R; import com.shimmerresearch.bluetooth.ShimmerBluetooth; import com.shimmerresearch.driver.CallbackObject; @@ -264,9 +265,11 @@ public void handleMessage(Message msg) { deviceState="Streaming"; textViewDeviceName.setText(mBluetoothAddress); textViewDeviceState.setText(deviceState); - List sensorList = shimmerService.getBluetoothManager().getListofEnabledSensors(mBluetoothAddress); + // No BluetoothManager (Bluetooth was off when the service started) means no sensor list + ShimmerBluetoothManagerAndroid btManager = shimmerService.getBluetoothManager(); + List sensorList = btManager == null ? null : btManager.getListofEnabledSensors(mBluetoothAddress); if(sensorList!=null){ - if(shimmerService.getBluetoothManager().getShimmerVersion(mBluetoothAddress)== ShimmerVerDetails.HW_ID.SHIMMER_3){ + if(btManager.getShimmerVersion(mBluetoothAddress)== ShimmerVerDetails.HW_ID.SHIMMER_3){ sensorList.remove("ECG"); sensorList.remove("EMG"); if(sensorList.contains("EXG1")){ diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/SignalsToPlotFragment.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/SignalsToPlotFragment.java index cc3da4b4..f2bc6fb9 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/SignalsToPlotFragment.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/guiUtilities/supportfragments/SignalsToPlotFragment.java @@ -15,7 +15,6 @@ import android.widget.ListView; import com.androidplot.xy.XYPlot; -import com.shimmerresearch.android.manager.ShimmerBluetoothManagerAndroid; import com.shimmerresearch.android.shimmerService.ShimmerService; import com.shimmerresearch.androidinstrumentdriver.R; import com.shimmerresearch.driver.ShimmerDevice; @@ -84,10 +83,10 @@ public void buildSignalsToPlotList(Context context, final ShimmerService service this.context = context; shimmerService = service; dynamicPlot = plot; - ShimmerBluetoothManagerAndroid btManager = shimmerService.getBluetoothManager(); - final ShimmerDevice device = btManager.getShimmer(bluetoothAddress); + // Null when the service has no BluetoothManager (Bluetooth off) or does not know the device + final ShimmerDevice device = shimmerService.getShimmer(bluetoothAddress); - if(device.isStreaming()) { + if(device != null && device.isStreaming()) { device.getListofEnabledChannelSignalsandFormats(); channelsMap = device.getMapOfEnabledChannelsForStreaming(); diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/shimmerService/ShimmerService.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/shimmerService/ShimmerService.java index 5fda5af9..8dbd4c44 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/shimmerService/ShimmerService.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/shimmerService/ShimmerService.java @@ -118,6 +118,13 @@ public class ShimmerService extends Service { private String mECGtoHRSignalName = Configuration.Shimmer3.ObjectClusterSensorName.ECG_LA_RA_24BIT; public PlotManagerAndroid mPlotManager; + /** + * Null while Bluetooth was off when the service was created (onCreate logs "Bluetooth not + * Enabled"), until ensureBluetoothManager() creates it. With no BluetoothManager there are no + * devices, so every method that uses it first returns what the BluetoothManager returns for a + * device it does not know: commands do nothing, and queries return -1, 0, false, "", null, + * DISCONNECTED or an empty list, as that method would. + */ protected ShimmerBluetoothManagerAndroid btManager; /** For logging to file */ @@ -321,6 +328,7 @@ public void onStop(){ */ @Deprecated public void toggleAllLEDS(){ + if (btManager == null) return; btManager.toggleAllLEDS(); } @@ -331,12 +339,14 @@ public void toggleAllLEDS(){ */ @Deprecated public void toggleLED(String bluetoothAddress) { + if (btManager == null) return; btManager.toggleLED(bluetoothAddress); } //TODO: Remove This when done testing public void clickToggle() { + if (btManager == null) return; btManager.toggleAllLEDS(); } @@ -540,7 +550,7 @@ public void handleMsgStateChange(Message msg) { } switch (state) { case CONNECTED: - ShimmerDevice shimmerDevice = btManager.getShimmerDeviceBtConnectedFromMac(macAddress); + ShimmerDevice shimmerDevice = btManager == null ? null : btManager.getShimmerDeviceBtConnectedFromMac(macAddress); mMultiShimmer.remove(macAddress); if (mMultiShimmer.get(macAddress)==null) { mMultiShimmer.put(macAddress,shimmerDevice); } intent.putExtra("ShimmerBluetoothAddress", macAddress); @@ -567,7 +577,7 @@ public void handleMsgStateChange(Message msg) { sendBroadcast(intent); break; case SDLOGGING: - shimmerDevice = btManager.getShimmerDeviceBtConnectedFromMac(macAddress); + shimmerDevice = btManager == null ? null : btManager.getShimmerDeviceBtConnectedFromMac(macAddress); mMultiShimmer.remove(macAddress); if (mMultiShimmer.get(macAddress)==null) { mMultiShimmer.put(macAddress,shimmerDevice); } intent.putExtra("ShimmerBluetoothAddress", macAddress ); @@ -576,7 +586,9 @@ public void handleMsgStateChange(Message msg) { sendBroadcast(intent); break; case DISCONNECTED: - btManager.removeShimmerDeviceBtConnected(macAddress); + if (btManager != null) { + btManager.removeShimmerDeviceBtConnected(macAddress); + } intent.putExtra("ShimmerBluetoothAddress", macAddress ); intent.putExtra("ShimmerDeviceName", shimmerName ); intent.putExtra("ShimmerState",BT_STATE.DISCONNECTED); @@ -598,10 +610,12 @@ public void handleNotificationMsg(Message msg) { } public void stopStreamingAllDevices() { + if (btManager == null) return; btManager.stopStreamingAllDevices(); } public void startStreamingAllDevices() { + if (btManager == null) return; btManager.startStreamingAllDevices(); } @@ -690,6 +704,7 @@ public void setAllSampingRate(double samplingRate) { */ @Deprecated public void setAllAccelRange(int accelRange) { + if (btManager == null) return; //TODO: Test this. btManager.setAllAccelRange(accelRange); } @@ -700,6 +715,7 @@ public void setAllAccelRange(int accelRange) { */ @Deprecated public void setAllGSRRange(int gsrRange) { + if (btManager == null) return; // TODO: Test this. btManager.setAllGSRRange(gsrRange); } @@ -710,6 +726,7 @@ public void setAllGSRRange(int gsrRange) { */ @Deprecated public void setAllEnabledSensors(int enabledSensors) { + if (btManager == null) return; //TODO: Test this. btManager.setAllEnabledSensors(enabledSensors); } @@ -745,6 +762,7 @@ public void setEnabledSensors(long enabledSensors,String bluetoothAddress) { */ @Deprecated public void writePMux(String bluetoothAddress,int setBit) { + if (btManager == null) return; btManager.writePMux(bluetoothAddress, setBit); } @@ -755,6 +773,7 @@ public void writePMux(String bluetoothAddress,int setBit) { */ @Deprecated public void write5VReg(String bluetoothAddress,int setBit) { + if (btManager == null) return; btManager.write5VReg(bluetoothAddress, setBit); } @@ -766,6 +785,7 @@ public void write5VReg(String bluetoothAddress,int setBit) { */ @Deprecated public List getListofEnabledSensorSignals(String bluetoothAddress) { + if (btManager == null) return new ArrayList(); List listofSensors = new ArrayList(); listofSensors = btManager.getListofEnabledSensorSignals(bluetoothAddress); return listofSensors; @@ -778,6 +798,7 @@ public List getListofEnabledSensorSignals(String bluetoothAddress) { */ @Deprecated public long getEnabledSensors(String bluetoothAddress) { + if (btManager == null) return 0; long enabledSensors = btManager.getEnabledSensors(bluetoothAddress); return enabledSensors; } @@ -829,6 +850,7 @@ public void writeSamplingRate(String bluetoothAddress,double samplingRate) { */ @Deprecated public void writeAccelRange(String bluetoothAddress,int accelRange) { + if (btManager == null) return; btManager.writeAccelRange(bluetoothAddress, accelRange); } @@ -839,6 +861,7 @@ public void writeAccelRange(String bluetoothAddress,int accelRange) { */ @Deprecated public void writeGyroRange(String bluetoothAddress,int range) { + if (btManager == null) return; btManager.writeGyroRange(bluetoothAddress, range); } @@ -849,6 +872,7 @@ public void writeGyroRange(String bluetoothAddress,int range) { */ @Deprecated public void writePressureResolution(String bluetoothAddress,int resolution) { + if (btManager == null) return; btManager.writePressureResolution(bluetoothAddress, resolution); } @@ -859,6 +883,7 @@ public void writePressureResolution(String bluetoothAddress,int resolution) { */ @Deprecated public void writeMagRange(String bluetoothAddress,int range) { + if (btManager == null) return; btManager.writeMagRange(bluetoothAddress, range); } @@ -869,6 +894,7 @@ public void writeMagRange(String bluetoothAddress,int range) { */ @Deprecated public void writeGSRRange(String bluetoothAddress,int gsrRange) { + if (btManager == null) return; btManager.writeGSRRange(bluetoothAddress, gsrRange); } @@ -879,6 +905,7 @@ public void writeGSRRange(String bluetoothAddress,int gsrRange) { */ @Deprecated public double getSamplingRate(String bluetoothAddress) { + if (btManager == null) return -1; double SRate = -1; SRate = btManager.getSamplingRate(bluetoothAddress); return SRate; @@ -891,6 +918,7 @@ public double getSamplingRate(String bluetoothAddress) { */ @Deprecated public int getAccelRange(String bluetoothAddress) { + if (btManager == null) return -1; return btManager.getAccelRange(bluetoothAddress); } @@ -901,6 +929,7 @@ public int getAccelRange(String bluetoothAddress) { */ @Deprecated public BT_STATE getShimmerState(String bluetoothAddress){ + if (btManager == null) return BT_STATE.DISCONNECTED; BT_STATE status=BT_STATE.DISCONNECTED; status = btManager.getShimmerState(bluetoothAddress); return status; @@ -913,6 +942,7 @@ public BT_STATE getShimmerState(String bluetoothAddress){ */ @Deprecated public int getGSRRange(String bluetoothAddress) { + if (btManager == null) return -1; return btManager.getGSRRange(bluetoothAddress); } @@ -923,6 +953,7 @@ public int getGSRRange(String bluetoothAddress) { */ @Deprecated public int get5VReg(String bluetoothAddress) { + if (btManager == null) return -1; int fiveVReg=-1; fiveVReg = btManager.get5VReg(bluetoothAddress); return fiveVReg; @@ -935,6 +966,7 @@ public int get5VReg(String bluetoothAddress) { */ @Deprecated public boolean isLowPowerMagEnabled(String bluetoothAddress) { + if (btManager == null) return false; boolean enabled=false; enabled = btManager.isLowPowerMagEnabled(bluetoothAddress); return enabled; @@ -947,6 +979,7 @@ public boolean isLowPowerMagEnabled(String bluetoothAddress) { */ @Deprecated public int getpmux(String bluetoothAddress) { + if (btManager == null) return -1; int pmux=-1; pmux = btManager.getpmux(bluetoothAddress); return pmux; @@ -980,6 +1013,7 @@ public void startStreaming(String bluetoothAddress) { */ public void startStreaming(String bluetoothAddress) throws Exception{ + if (btManager == null) return; btManager.startStreaming(bluetoothAddress); } @@ -989,6 +1023,7 @@ public void startStreaming(String bluetoothAddress) throws Exception{ */ @Deprecated public void startLogging(String bluetoothAddress) { + if (btManager == null) return; btManager.startLogging(bluetoothAddress); } @@ -998,14 +1033,17 @@ public void startLogging(String bluetoothAddress) { */ @Deprecated public void stopLogging(String bluetoothAddress) { + if (btManager == null) return; btManager.stopLogging(bluetoothAddress); } public void startLogAndStreaming(String bluetoothAddress) { + if (btManager == null) return; btManager.startLoggingAndStreaming(bluetoothAddress); } public void stopLogAndStreaming(String bluetoothAddress) { + if (btManager == null) return; btManager.stopLoggingAndStreaming(bluetoothAddress); } @@ -1018,6 +1056,7 @@ public void stopLogAndStreaming(String bluetoothAddress) { */ @Deprecated public long sensorConflictCheckandCorrection(String bluetoothAddress, long enabledSensors, int sensorToCheck) { + if (btManager == null) return 0; long newSensorBitmap = 0; newSensorBitmap = btManager.sensorConflictCheckandCorrection(bluetoothAddress, enabledSensors, sensorToCheck); return newSensorBitmap; @@ -1030,6 +1069,7 @@ public long sensorConflictCheckandCorrection(String bluetoothAddress, long enabl */ @Deprecated public List getListofEnabledSensors(String bluetoothAddress) { + if (btManager == null) return null; List listofSensors = null; listofSensors = btManager.getListofEnabledSensors(bluetoothAddress); return listofSensors; @@ -1043,6 +1083,8 @@ public List getListofEnabledSensors(String bluetoothAddress) { */ @Deprecated public boolean bluetoothAddressComparator(String bluetoothAddress, String address){ + // A comparison of two addresses with the colons removed, which needs no device. + if (btManager == null) return bluetoothAddress.replace(":","").equals(address.replace(":","")); return btManager.bluetoothAddressComparator(bluetoothAddress, address); } @@ -1072,6 +1114,7 @@ public void stopStreaming(String bluetoothAddress) { */ public void stopStreaming(String bluetoothAddress) throws Exception{ + if (btManager == null) return; btManager.stopStreaming(bluetoothAddress); } @@ -1081,6 +1124,7 @@ public void stopStreaming(String bluetoothAddress) throws Exception{ */ @Deprecated public void setBlinkLEDCMD(String bluetoothAddress) { + if (btManager == null) return; btManager.setBlinkLEDCMD(bluetoothAddress); } @@ -1091,6 +1135,7 @@ public void setBlinkLEDCMD(String bluetoothAddress) { */ @Deprecated public void enableLowPowerMag(String bluetoothAddress,boolean enable) { + if (btManager == null) return; btManager.enableLowPowerMag(bluetoothAddress, enable); } @@ -1101,6 +1146,7 @@ public void enableLowPowerMag(String bluetoothAddress,boolean enable) { */ @Deprecated public void setBattLimitWarning(String bluetoothAddress, double limit) { + if (btManager == null) return; btManager.setBattLimitWarning(bluetoothAddress, limit); } @@ -1111,6 +1157,7 @@ public void setBattLimitWarning(String bluetoothAddress, double limit) { */ @Deprecated public double getBattLimitWarning(String bluetoothAddress) { + if (btManager == null) return -1; return btManager.getBattLimitWarning(bluetoothAddress); } @@ -1121,6 +1168,7 @@ public double getBattLimitWarning(String bluetoothAddress) { */ @Deprecated public double getPacketReceptionRate(String bluetoothAddress) { + if (btManager == null) return -1; double rate = btManager.getPacketReceptionRate(bluetoothAddress); return rate; } @@ -1163,6 +1211,7 @@ public void enableGraphingHandler(boolean setting){ */ @Deprecated public boolean DevicesConnected(String bluetoothAddress){ + if (btManager == null) return false; return btManager.DevicesConnected(bluetoothAddress); } @@ -1173,6 +1222,7 @@ public boolean DevicesConnected(String bluetoothAddress){ */ @Deprecated public boolean DeviceIsLogging(String bluetoothAddress){ + if (btManager == null) return false; return btManager.DeviceIsLogging(bluetoothAddress); } @@ -1183,6 +1233,7 @@ public boolean DeviceIsLogging(String bluetoothAddress){ */ @Deprecated public boolean DeviceIsStreaming(String bluetoothAddress){ + if (btManager == null) return false; return btManager.DeviceIsStreaming(bluetoothAddress); } @@ -1211,6 +1262,7 @@ public void closeAndRemoveFile(String bluetoothAddress){ */ @Deprecated public String getFWVersion (String bluetoothAddress){ + if (btManager == null) return ""; return btManager.getFWVersion(bluetoothAddress); } @@ -1221,6 +1273,7 @@ public String getFWVersion (String bluetoothAddress){ */ @Deprecated public int getShimmerVersion (String bluetoothAddress){ + if (btManager == null) return 0; return btManager.getShimmerVersion(bluetoothAddress); } @@ -1276,6 +1329,7 @@ public boolean isEXGUsingEMG16Configuration(String bluetoothAddress){ */ @Deprecated public void writeEXGSetting(String bluetoothAddress,int setting) { + if (btManager == null) return; btManager.writeEXGSetting(bluetoothAddress, setting); } @@ -1297,6 +1351,7 @@ private String fromMilisecToDate(long miliseconds){ */ @Deprecated public boolean isUsingLogAndStreamFW(String bluetoothAddress){ + if (btManager == null) return false; return btManager.isUsingLogAndStreamFW(bluetoothAddress); } @@ -1306,6 +1361,7 @@ public boolean isUsingLogAndStreamFW(String bluetoothAddress){ */ @Deprecated public void readStatusLogAndStream(String bluetoothAddress){ + if (btManager == null) return; btManager.readStatusLogAndStream(bluetoothAddress); } @@ -1316,6 +1372,7 @@ public void readStatusLogAndStream(String bluetoothAddress){ */ @Deprecated public boolean isSensing(String bluetoothAddress){ + if (btManager == null) return false; return btManager.isSensing(bluetoothAddress); } @@ -1326,9 +1383,14 @@ public boolean isSensing(String bluetoothAddress){ */ @Deprecated public boolean isDocked(String bluetoothAddress){ + if (btManager == null) return false; return btManager.isDocked(bluetoothAddress); } + /** + * @return the BluetoothManager, or null if Bluetooth was off when the service was created and it + * has not been switched on since (see ensureBluetoothManager()). Callers must check for null. + */ public ShimmerBluetoothManagerAndroid getBluetoothManager() { return btManager; } @@ -1339,10 +1401,12 @@ public List getListOfConnectedDevices() { } public void configureShimmer(ShimmerDevice device) { + if (btManager == null) return; btManager.configureShimmer(device); } public void configureShimmers(List listOfShimmers) { + if (btManager == null) return; btManager.configureShimmers(listOfShimmers); } diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/android/shimmerService/AAPI_00008_ServiceWithoutBluetoothManagerTest.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/android/shimmerService/AAPI_00008_ServiceWithoutBluetoothManagerTest.java index 3eaccd3c..47008ed8 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/android/shimmerService/AAPI_00008_ServiceWithoutBluetoothManagerTest.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/android/shimmerService/AAPI_00008_ServiceWithoutBluetoothManagerTest.java @@ -1,14 +1,21 @@ package com.shimmerresearch.android.shimmerService; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.List; +import android.os.Message; + import org.junit.Before; import org.junit.Test; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; +import com.shimmerresearch.driver.CallbackObject; import com.shimmerresearch.driver.ShimmerDevice; /** @@ -18,11 +25,16 @@ * the app crashed at launch. Google Play's pre-launch test refused the 1.3.4 * release for it. *

- * A missing BluetoothManager now means no devices: these calls return empty or - * null, or do nothing, instead of throwing. + * DEV-1427: the same was true of every other method that uses btManager. A + * missing BluetoothManager now means no devices throughout: each method returns + * what ShimmerBluetoothManagerAndroid returns for a device it does not know. + * Commands do nothing, and queries return -1, 0, false, "", null, DISCONNECTED + * or an empty list. *

* No hardware: a service that has not been through onCreate() has no - * BluetoothManager, which is the state Bluetooth being off leaves it in. + * BluetoothManager, which is the state Bluetooth being off leaves it in. Paths + * that show a Toast are left out: Toast.makeText() returns null on the stub + * android.jar these tests run against. */ public class AAPI_00008_ServiceWithoutBluetoothManagerTest { @@ -54,4 +66,83 @@ public void disconnectAllDevicesClearsTheServiceState() { service.disconnectAllDevices(); assertTrue(service.mMultiShimmer.isEmpty()); } + + /** Each does what the BluetoothManager does for a device it does not know: nothing. */ + @Test + @SuppressWarnings("deprecation") + public void commandsDoNothing() throws Exception { + service.toggleAllLEDS(); + service.toggleLED(ADDRESS); + service.clickToggle(); + service.stopStreamingAllDevices(); + service.startStreamingAllDevices(); + service.setAllAccelRange(0); + service.setAllGSRRange(0); + service.setAllEnabledSensors(0); + service.writePMux(ADDRESS, 0); + service.write5VReg(ADDRESS, 0); + service.writeAccelRange(ADDRESS, 0); + service.writeGyroRange(ADDRESS, 0); + service.writePressureResolution(ADDRESS, 0); + service.writeMagRange(ADDRESS, 0); + service.writeGSRRange(ADDRESS, 0); + service.startStreaming(ADDRESS); + service.stopStreaming(ADDRESS); + service.startLogging(ADDRESS); + service.stopLogging(ADDRESS); + service.startLogAndStreaming(ADDRESS); + service.stopLogAndStreaming(ADDRESS); + service.setBlinkLEDCMD(ADDRESS); + service.enableLowPowerMag(ADDRESS, true); + service.setBattLimitWarning(ADDRESS, 3.3); + service.writeEXGSetting(ADDRESS, 0); + service.readStatusLogAndStream(ADDRESS); + service.configureShimmer(null); + service.configureShimmers(new ArrayList()); + } + + /** Each returns the value the BluetoothManager returns for a device it does not know. */ + @Test + @SuppressWarnings("deprecation") + public void queriesReturnTheUnknownDeviceValues() { + assertTrue(service.getListofEnabledSensorSignals(ADDRESS).isEmpty()); + assertEquals(0, service.getEnabledSensors(ADDRESS)); + assertEquals(-1, service.getSamplingRate(ADDRESS), 0); + assertEquals(-1, service.getAccelRange(ADDRESS)); + assertEquals(BT_STATE.DISCONNECTED, service.getShimmerState(ADDRESS)); + assertEquals(-1, service.getGSRRange(ADDRESS)); + assertEquals(-1, service.get5VReg(ADDRESS)); + assertFalse(service.isLowPowerMagEnabled(ADDRESS)); + assertEquals(-1, service.getpmux(ADDRESS)); + assertEquals(0, service.sensorConflictCheckandCorrection(ADDRESS, 0xFF, 0)); + assertNull(service.getListofEnabledSensors(ADDRESS)); + assertEquals(-1, service.getBattLimitWarning(ADDRESS), 0); + assertEquals(-1, service.getPacketReceptionRate(ADDRESS), 0); + assertFalse(service.DevicesConnected(ADDRESS)); + assertFalse(service.DeviceIsLogging(ADDRESS)); + assertFalse(service.DeviceIsStreaming(ADDRESS)); + assertEquals("", service.getFWVersion(ADDRESS)); + assertEquals(0, service.getShimmerVersion(ADDRESS)); + assertFalse(service.isUsingLogAndStreamFW(ADDRESS)); + assertFalse(service.isSensing(ADDRESS)); + assertFalse(service.isDocked(ADDRESS)); + } + + /** A comparison of two strings, so it needs no device and still answers. */ + @Test + @SuppressWarnings("deprecation") + public void addressComparatorStillCompares() { + assertTrue(service.bluetoothAddressComparator(ADDRESS, "000666000001")); + assertFalse(service.bluetoothAddressComparator(ADDRESS, "00:06:66:00:00:02")); + } + + /** State messages look the device up in, or remove it from, the BluetoothManager. */ + @Test + public void stateMessagesAreHandled() { + for (BT_STATE state : new BT_STATE[] {BT_STATE.CONNECTED, BT_STATE.SDLOGGING, BT_STATE.DISCONNECTED}) { + Message msg = new Message(); + msg.obj = new CallbackObject(0, state, ADDRESS, ""); + service.handleMsgStateChange(msg); + } + } }