From 6401c40964bde3d42a9317bd4f62179f893c6328 Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Tue, 3 Nov 2026 13:47:05 +0800 Subject: [PATCH 1/3] DEV-1401: ShimmerCapture no longer crashes when Bluetooth is off Google Play's pre-launch test refused 1.3.4: on a device with Bluetooth off, ShimmerService.onCreate() could not create its BluetoothManager ("Bluetooth not Enabled") and left it null, and the options menu then called getListOfConnectedDevices() on it. NullPointerException at launch. ShimmerService: a missing BluetoothManager means no devices. getListOfConnectedDevices() returns an empty list, getShimmer() null, disconnectAllDevices() and onDestroy() skip it, and the connectShimmer() methods first create it if Bluetooth has been switched on since. It logs "Bluetooth Manager created" when it does. MainActivity: after starting the service it asks to switch Bluetooth on if it is off; on Allow it calls createNewBluetoothManager(), which nothing called before, and on Deny it says so once instead of asking again in a loop. Connect asks for Bluetooth before opening the device list. The request needs BLUETOOTH_CONNECT on Android 12+, so it is made only once that is granted: the reason the old prompt in onCreate() was commented out. onRequestPermissionsResult() now runs startServiceandBTManager() on any answer; it ran it only when BLUETOOTH_CONNECT was refused, so a first launch skipped it (onResume still bound the service) and with it BleManager.init(). Co-Authored-By: Claude Opus 5.5 --- .../shimmerService/ShimmerService.java | 30 ++++++++++-- .../shimmercapture/MainActivity.java | 49 ++++++++++++++----- 2 files changed, 63 insertions(+), 16 deletions(-) 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 d8cb1d90..95e08c53 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 @@ -187,11 +187,27 @@ public void setECGtoHRSignal(String ecgtoHR){ public void onDestroy() { Toast.makeText(this, "Shimmer Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); - btManager.disconnectAllDevices(); + if (btManager != null) { + btManager.disconnectAllDevices(); + } + } + + /** + * btManager is null when Bluetooth was off as the service was created (onCreate logs "Bluetooth + * not Enabled"). Creates it if Bluetooth has been switched on since. + * @return whether there is a BluetoothManager to use + */ + private boolean ensureBluetoothManager() { + if (btManager == null) { + createNewBluetoothManager(); + } + return btManager != null; } public void disconnectAllDevices(){ - btManager.disconnectAllDevices(); + if (btManager != null) { + btManager.disconnectAllDevices(); + } mMultiShimmer.clear(); mLogShimmer.clear(); } @@ -255,14 +271,17 @@ public boolean isECGtoHREnabled(){ } public void connectShimmer(final String bluetoothAddress,Context context){ + if (!ensureBluetoothManager()) return; btManager.connectShimmerThroughBTAddress(bluetoothAddress,"",context); } public void connectShimmer(final String bluetoothAddress,final String deviceName, Context context){ + if (!ensureBluetoothManager()) return; btManager.connectShimmerThroughBTAddress(bluetoothAddress,deviceName,context); } public void connectShimmer(final String bluetoothAddress,final String deviceName, ShimmerBluetoothManagerAndroid.BT_TYPE preferredBtType, Context context){ + if (!ensureBluetoothManager()) return; boolean isVerisense = false; if (deviceName!=null){ if (deviceName.contains(VerisenseDevice.VERISENSE_PREFIX)) { @@ -281,6 +300,7 @@ public void connectShimmer(final String bluetoothAddress,final String deviceName } public void connectShimmer(final String bluetoothAddress){ + if (!ensureBluetoothManager()) return; btManager.connectShimmerThroughBTAddress(bluetoothAddress); } @@ -1201,7 +1221,7 @@ public int getShimmerVersion (String bluetoothAddress){ } public ShimmerDevice getShimmer(String bluetoothAddress){ - return btManager.getShimmer(bluetoothAddress); + return btManager == null ? null : btManager.getShimmer(bluetoothAddress); } public void test(){ @@ -1310,7 +1330,8 @@ public ShimmerBluetoothManagerAndroid getBluetoothManager() { } public List getListOfConnectedDevices() { - return btManager.getListOfConnectedDevices(); + // No BluetoothManager (Bluetooth was off when the service started) means no devices. + return btManager == null ? new ArrayList() : btManager.getListOfConnectedDevices(); } public void configureShimmer(ShimmerDevice device) { @@ -1330,6 +1351,7 @@ public void createNewBluetoothManager() { try { btManager = new ShimmerBluetoothManagerAndroid(this, mHandler); + Log.d(TAG, "Bluetooth Manager created"); } catch (Exception e) { Log.e(TAG, "ERROR! " + e); Toast.makeText(this, "Error! Could not create Bluetooth Manager!", Toast.LENGTH_LONG).show(); diff --git a/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java b/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java index 4e091746..cba6d514 100644 --- a/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java +++ b/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java @@ -37,6 +37,7 @@ import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import androidx.appcompat.app.AppCompatActivity; +import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Gravity; @@ -100,6 +101,7 @@ public class MainActivity extends AppCompatActivity implements ConnectedShimmers final static String LOG_TAG = "Shimmer"; final static String SERVICE_TAG = "ShimmerService"; + final static int REQUEST_ENABLE_BT = 1; final static int REQUEST_CONNECT_SHIMMER = 2; public static APP_RELEASE_TYPE appReleaseType = APP_RELEASE_TYPE.PUBLIC; public enum APP_RELEASE_TYPE{ @@ -274,8 +276,10 @@ public void onRequestPermissionsResult( @NonNull String[] permissions, @NonNull int[] grantResults ){ - int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT); - if (permissionCheck != PackageManager.PERMISSION_GRANTED) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + // Start the service whatever the answer, as requestPermissions() does when they are already + // granted. This used to start it only when BLUETOOTH_CONNECT was refused. + if (requestCode == 110) { startServiceandBTManager(); } } @@ -287,6 +291,27 @@ protected void startServiceandBTManager(){ getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE); Log.d(LOG_TAG, "Shimmer Service started"); Toast.makeText(this, "Shimmer Service started", Toast.LENGTH_SHORT).show(); + requestBluetoothEnableIfOff(); + } + + /** + * Asks the user to switch Bluetooth on if it is off. The service then has no BluetoothManager + * (it logs "Bluetooth not Enabled"); onActivityResult creates one once Bluetooth is on. + * @return true if Bluetooth is off, whether or not it could be asked for + */ + private boolean requestBluetoothEnableIfOff() { + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if (adapter == null || adapter.isEnabled()) { + return false; + } + // From Android 12 the enable request needs BLUETOOTH_CONNECT, or it throws. + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S + || ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { + startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT); + } else { + Toast.makeText(this, "Bluetooth is off. Turn it on to connect to a device.", Toast.LENGTH_LONG).show(); + } + return true; } @Override @@ -351,6 +376,9 @@ public boolean onOptionsItemSelected(MenuItem item) { final Toast otherTaskOngoingToast = Toast.makeText(this, "Please wait until current task is finished", Toast.LENGTH_LONG); int id = item.getItemId(); if(id == R.id.connect_device){ + if (requestBluetoothEnableIfOff()) { + return true; //Nothing to connect to until Bluetooth is on + } Intent pairedDevicesIntent = new Intent(getApplicationContext(), ShimmerBluetoothDialog.class); startActivityForResult(pairedDevicesIntent, REQUEST_CONNECT_SHIMMER); return true; @@ -720,18 +748,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { */ } } - if (requestCode == 1) { //The system Bluetooth enable dialog has returned a result + if (requestCode == REQUEST_ENABLE_BT) { //The system Bluetooth enable dialog has returned a result if (resultCode == RESULT_OK) { - Intent intent = new Intent(this, ShimmerService.class); - startService(intent); - getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - Log.d(LOG_TAG, "Shimmer Service started"); - Toast.makeText(this, "Shimmer Service started", Toast.LENGTH_SHORT).show(); + //The service is already running, but without a BluetoothManager: Bluetooth was off + //when it was created. If it is not bound yet, connecting creates one instead. + if (mService != null) { + mService.createNewBluetoothManager(); + } } else if (resultCode == RESULT_CANCELED) { - Toast.makeText(this, "Please enable Bluetooth to proceed.", Toast.LENGTH_LONG).show(); - int REQUEST_ENABLE_BT = 1; - Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); + Toast.makeText(this, "Bluetooth is off. Turn it on to connect to a device.", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Unknown Error! Your device may not support Bluetooth!", Toast.LENGTH_LONG).show(); } From 8bfaceee0c8e2bae50e0341fa056548aeb7a145f Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Tue, 3 Nov 2026 13:47:05 +0800 Subject: [PATCH 2/3] DEV-1401: ShimmerCapture 1.3.4, versionCode 37 Play keeps versionCode 36 from the refused 1.3.4 draft. versionName stays 1.3.4: no 1.3.4 has reached users. Co-Authored-By: Claude Opus 5.5 --- .../shimmerServiceExample/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ShimmerAndroidInstrumentDriver/shimmerServiceExample/build.gradle b/ShimmerAndroidInstrumentDriver/shimmerServiceExample/build.gradle index 6634fde0..ddf660a7 100644 --- a/ShimmerAndroidInstrumentDriver/shimmerServiceExample/build.gradle +++ b/ShimmerAndroidInstrumentDriver/shimmerServiceExample/build.gradle @@ -8,7 +8,7 @@ android { applicationId "com.shimmersensing.shimmerconnect" minSdkVersion 21 targetSdkVersion 36 - versionCode 36 + versionCode 37 versionName "1.3.4" multiDexEnabled true From 247271ad546f18da5a5146fd826cfd8dd73db43c Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Tue, 3 Nov 2026 14:58:35 +0800 Subject: [PATCH 3/3] DEV-1402: Address the #150 review - Allow on the Bluetooth prompt calls ensureBluetoothManager(), now public, instead of createNewBluetoothManager(). It creates a manager only when there is none, so one the service already had (Bluetooth switched off after launch) is kept rather than replaced, and nothing holding it from getBluetoothManager() is orphaned. - ShimmerService.onStop() skips a missing manager, as onDestroy() does. - MainActivity names the permission request code, REQUEST_PERMISSIONS. - AAPI_00008 pins the fix: a service with no manager returns an empty device list and no device, and disconnectAllDevices() still clears its state. All three fail with NullPointerException on master's ShimmerService. Co-Authored-By: Claude Opus 5.5 --- .../shimmerService/ShimmerService.java | 10 +++- ...08_ServiceWithoutBluetoothManagerTest.java | 57 +++++++++++++++++++ .../shimmercapture/MainActivity.java | 13 +++-- 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/android/shimmerService/AAPI_00008_ServiceWithoutBluetoothManagerTest.java 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 95e08c53..5fda5af9 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 @@ -194,10 +194,12 @@ public void onDestroy() { /** * btManager is null when Bluetooth was off as the service was created (onCreate logs "Bluetooth - * not Enabled"). Creates it if Bluetooth has been switched on since. + * not Enabled"). Creates it if Bluetooth has been switched on since; an existing one is kept, so + * whatever holds it from getBluetoothManager() still matches the service. Call this rather than + * createNewBluetoothManager() once Bluetooth comes on. * @return whether there is a BluetoothManager to use */ - private boolean ensureBluetoothManager() { + public boolean ensureBluetoothManager() { if (btManager == null) { createNewBluetoothManager(); } @@ -308,7 +310,9 @@ public void onStop(){ Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); - btManager.disconnectAllDevices(); + if (btManager != null) { + btManager.disconnectAllDevices(); + } } /** 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 new file mode 100644 index 00000000..3eaccd3c --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/android/shimmerService/AAPI_00008_ServiceWithoutBluetoothManagerTest.java @@ -0,0 +1,57 @@ +package com.shimmerresearch.android.shimmerService; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.shimmerresearch.driver.ShimmerDevice; + +/** + * DEV-1402: with Bluetooth off, ShimmerService.onCreate() cannot create its + * BluetoothManager ("Bluetooth not Enabled") and leaves btManager null. The + * ShimmerCapture options menu then called getListOfConnectedDevices() on it, and + * 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. + *

+ * No hardware: a service that has not been through onCreate() has no + * BluetoothManager, which is the state Bluetooth being off leaves it in. + */ +public class AAPI_00008_ServiceWithoutBluetoothManagerTest { + + private static final String ADDRESS = "00:06:66:00:00:01"; + + private ShimmerService service; + + @Before + public void createServiceWithoutBluetoothManager() { + service = new ShimmerService(); + assertNull("precondition: no BluetoothManager", service.getBluetoothManager()); + } + + @Test + public void listOfConnectedDevicesIsEmpty() { + List devices = service.getListOfConnectedDevices(); + assertNotNull(devices); + assertTrue(devices.isEmpty()); + } + + @Test + public void getShimmerFindsNoDevice() { + assertNull(service.getShimmer(ADDRESS)); + } + + @Test + public void disconnectAllDevicesClearsTheServiceState() { + service.mMultiShimmer.put(ADDRESS, new Object()); + service.disconnectAllDevices(); + assertTrue(service.mMultiShimmer.isEmpty()); + } +} diff --git a/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java b/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java index cba6d514..0ba4d5a8 100644 --- a/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java +++ b/ShimmerAndroidInstrumentDriver/shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java @@ -103,6 +103,7 @@ public class MainActivity extends AppCompatActivity implements ConnectedShimmers final static String SERVICE_TAG = "ShimmerService"; final static int REQUEST_ENABLE_BT = 1; final static int REQUEST_CONNECT_SHIMMER = 2; + final static int REQUEST_PERMISSIONS = 110; public static APP_RELEASE_TYPE appReleaseType = APP_RELEASE_TYPE.PUBLIC; public enum APP_RELEASE_TYPE{ INTERNAL, @@ -187,7 +188,7 @@ public void requestPermissions(){ } if (!permissionGranted) { // Should we show an explanation? - ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT,Manifest.permission.BLUETOOTH_SCAN,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION}, 110); + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT,Manifest.permission.BLUETOOTH_SCAN,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSIONS); } else { startServiceandBTManager(); @@ -279,7 +280,7 @@ public void onRequestPermissionsResult( super.onRequestPermissionsResult(requestCode, permissions, grantResults); // Start the service whatever the answer, as requestPermissions() does when they are already // granted. This used to start it only when BLUETOOTH_CONNECT was refused. - if (requestCode == 110) { + if (requestCode == REQUEST_PERMISSIONS) { startServiceandBTManager(); } } @@ -750,10 +751,12 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } if (requestCode == REQUEST_ENABLE_BT) { //The system Bluetooth enable dialog has returned a result if (resultCode == RESULT_OK) { - //The service is already running, but without a BluetoothManager: Bluetooth was off - //when it was created. If it is not bound yet, connecting creates one instead. + //If Bluetooth was off when the service was created, it has no BluetoothManager yet. + //ensureBluetoothManager() keeps one it already has (Bluetooth was only switched off + //later), so nothing holding it is orphaned. If the service is not bound yet, + //connecting creates the manager instead. if (mService != null) { - mService.createNewBluetoothManager(); + mService.ensureBluetoothManager(); } } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Bluetooth is off. Turn it on to connect to a device.", Toast.LENGTH_LONG).show();