Skip to content

DEV-1402: ShimmerCapture no longer crashes at launch when Bluetooth is off - #150

Merged
jyong15 merged 3 commits into
masterfrom
DEV-1401_bluetooth_off_crash
Nov 3, 2026
Merged

jyong15 merged 3 commits into
masterfrom
DEV-1401_bluetooth_off_crash

Conversation

@MAzalya

@MAzalya MAzalya commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Google Play refused to send ShimmerCapture 1.3.4 for review. Its pre-launch report flagged a crash on its 16 KB page-size test device. The crash isn't about page size: the APK has no native libraries. It's a Java crash that happens on any device with Bluetooth off:

ShimmerService: ERROR! java.lang.Exception: Bluetooth not Enabled
FATAL EXCEPTION: main
java.lang.NullPointerException: … ShimmerBluetoothManagerAndroid.getListOfConnectedDevices() on a null object reference
    at com.shimmerresearch.android.shimmerService.ShimmerService.getListOfConnectedDevices(ShimmerService.java:1313)
    at com.shimmerresearch.shimmercapture.MainActivity.onPrepareOptionsMenu(MainActivity.java:297)

ShimmerService.onCreate() cannot create its BluetoothManager while Bluetooth is off, so it leaves it null. About 1.5 s later the options menu calls through it. The prompt that used to ask for Bluetooth was commented out of MainActivity.onCreate() in March 2025. It ran before BLUETOOTH_CONNECT was granted, which throws on Android 12+.

Change

ShimmerService (library): a missing BluetoothManager now means "no devices":

  • getListOfConnectedDevices() returns an empty list, and getShimmer() returns null.
  • disconnectAllDevices(), onDestroy() and onStop() skip the manager.
  • ensureBluetoothManager() (public) creates the manager only if there is none, and keeps an existing one. The connectShimmer() overloads call it first.
  • It logs Bluetooth Manager created; before, only a failure was logged.

MainActivity (ShimmerCapture):

  • After starting the service, it asks to switch Bluetooth on if it's off. It asks only once BLUETOOTH_CONNECT is granted, so the request can't throw.
  • On Allow, it calls ensureBluetoothManager(), so a manager the service already has (Bluetooth switched off after launch) is kept, not replaced. On Deny, it shows a message once; the old handler re-opened the prompt in a loop.
  • Connect to Shimmer device asks for Bluetooth before opening the device list.
  • onRequestPermissionsResult() ran startServiceandBTManager() only when BLUETOOTH_CONNECT was refused (!=). It now runs it on any answer. onResume() still bound the service on a first launch, but it skipped BleManager.init() and, now, the Bluetooth prompt.

build.gradle: versionCode 37. Play keeps 36 from the refused draft. versionName stays 1.3.4.

Tested (Android 16 emulator, debug builds)

Case Before (master) After
Bluetooth off, permissions pre-granted (Play's lab) Crash at launch, same stack as Play's report No crash. Asks to turn Bluetooth on
…then Deny, then Disconnect All Devices, Enable Write To CSV (crashed before this) No crash. Connect asks for Bluetooth again
…then Allow — Bluetooth on; Bluetooth Manager created; Connect opens the device list and scans
Bluetooth off, fresh install, permissions granted by hand — Bluetooth prompt follows the permission prompts; manager created on Allow
Bluetooth on, fresh install, permissions granted by hand Service bound by onResume Service started and bound; no Bluetooth prompt
Bluetooth on at launch, then off, Connect → Allow — Bluetooth back on; the existing manager is kept (no second one created)

New unit test AAPI_00008: a service with no manager returns an empty device list and no device, and disconnectAllDevices() still clears its state. All three fail with the NPE on master's ShimmerService. Debug and release builds pass, release including lint-vital, and so do AAPI_00002–00008. Not covered: connecting to and streaming from a real Shimmer.

Notes

  • ShimmerService is library code, so the other example apps that use it get the guard too. Apps using the published shimmerandroidinstrumentdriver package get it at the next Release Android API run. It's Android-only, so no other host API needs it.
  • After merge: delete the ShimmerCapture_v1.3.4 release and tag (versionCode 36), discard the Play draft, and run Release ShimmerCapture again.

🤖 Generated with Claude Code

MAzalya and others added 2 commits November 3, 2026 13:47
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 <[email protected]>
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 <[email protected]>
@MAzalya MAzalya changed the title DEV-1401: ShimmerCapture no longer crashes at launch when Bluetooth is off DEV-1402: ShimmerCapture no longer crashes at launch when Bluetooth is off Nov 3, 2026
@jyong15
jyong15 self-requested a review November 3, 2026 06:27

@jyong15 jyong15 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 8bfacee. The fix works for the crash Play reported, and running the service start on any permission answer fixes the old != check. Comments inline. The first one (Allow replacing an existing BluetoothManager) is worth fixing before merge; the rest can follow.

Ticket: the title says DEV-1402, but the branch is DEV-1401_bluetooth_off_crash and both commits are prefixed DEV-1401:. DEV-1401 is the target-SDK change already merged in #149. Jira's development panel and anything that reads the ticket from the branch or commits will attach this work to DEV-1401. Renaming the branch would close the PR again, as with #148, so consider rewording the commit subjects to DEV-1402 before merge.

I built the debug APK (versionCode 37, targetSdk 36) and installed it on a Galaxy Z Fold7 running Android 16. I haven't yet tested it there with Bluetooth off.

🤖 Generated with Claude Code

//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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can replace a BluetoothManager the service already has. createNewBluetoothManager() always builds a new one, so here it overwrites btManager whenever one exists.

How it happens: Bluetooth is on at launch, so the service creates a manager. The user then turns Bluetooth off and taps Connect. requestBluetoothEnableIfOff() shows the prompt, and Allow builds a second manager. Anything that kept the first, e.g. a fragment holding getBluetoothManager()'s result, is left with an orphaned manager whose devices no longer match the service's.

ensureBluetoothManager() in this PR does the right thing, creating a manager only when there is none. Suggest making it public and calling it here:

if (mService != null) {
    mService.ensureBluetoothManager();
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 247271a as you suggested: ensureBluetoothManager() is public, its javadoc says to call it rather than createNewBluetoothManager() once Bluetooth comes on, and Allow calls it. Tested your scenario on the Android 16 emulator: Bluetooth on at launch (the manager is made in onCreate), then off, then Connect → Allow. Bluetooth comes back on and no second manager is created (Bluetooth Manager created count 0; the old call logged it every time). Connect then opens the device list. Bluetooth off at launch → Allow still creates exactly one.

public List<ShimmerDevice> getListOfConnectedDevices() {
return btManager.getListOfConnectedDevices();
// No BluetoothManager (Bluetooth was off when the service started) means no devices.
return btManager == null ? new ArrayList<ShimmerDevice>() : btManager.getListOfConnectedDevices();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The guard covers only some of btManager's uses. This PR guards about six of the ~60 btManager. calls in ShimmerService: the ones the Play crash and the tested menus reached. Still unguarded:

  • the public onStop() (L307), which calls btManager.disconnectAllDevices() just as onDestroy() did;
  • getBluetoothManager() (L1328), which returns null to callers that dereference it straight away: SignalsToPlotFragment (both copies), both PlotFragments, and MainActivity L598/923/975;
  • every other btManager. call: startStreamingAllDevices(), setAllEnabledSensors(), the write*/get* methods.

ShimmerService is library code, and the PR notes say the other apps using it get the guard too. But an app that reaches any of these with Bluetooth off still gets the same NullPointerException. Guarding it once would cover all of them: route every method through one accessor (ensureBluetoothManager(), returning early or throwing a clear exception when it's false), or use a null-object manager. If that's too much for this release, a follow-up issue would keep it from being lost.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed on the gap. In 247271a I guarded onStop() like onDestroy(). I left the rest for a follow-up ticket, for this reason. Within ShimmerCapture, every other btManager/getBluetoothManager() call you list runs only for a connected or selected device: configure, the sensor/config lists, plot and signals-to-plot while streaming. A device can't connect until a manager exists, and the manager is only ever missing when Bluetooth was off at start, until Allow creates it. So the app can't reach them with null. Other apps on ShimmerService can, so I'll raise a follow-up to route every use through one accessor, with a clear failure instead of an NPE, and link it here.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: the PR names the Bluetooth request code (REQUEST_ENABLE_BT) but leaves 110 bare here and in requestPermissions() (L190). If one changes without the other, the service is never started from the permission result. A REQUEST_PERMISSIONS = 110 beside REQUEST_CONNECT_SHIMMER would keep them together.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 247271a: REQUEST_PERMISSIONS = 110 beside REQUEST_ENABLE_BT and REQUEST_CONNECT_SHIMMER, used in both requestPermissions() and onRequestPermissionsResult().

* not Enabled"). Creates it if Bluetooth has been switched on since.
* @return whether there is a BluetoothManager to use
*/
private boolean ensureBluetoothManager() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The library already has JVM unit tests (AAPI_00002 to 00007, on the stub android.jar). An AAPI_00008 that leaves btManager null and calls getListOfConnectedDevices(), getShimmer() and disconnectAllDevices() would pin this fix. Without one, a re-added unguarded call would be caught only by the next Play pre-launch report.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in 247271a: AAPI_00008_ServiceWithoutBluetoothManagerTest. It builds a ShimmerService that hasn't been through onCreate(), so it has no manager, which is the Bluetooth-off state. It checks getListOfConnectedDevices() is empty, getShimmer() is null, and disconnectAllDevices() still clears mMultiShimmer. Against master's ShimmerService all three fail with the NPE (the first at Play's exact call); with the fix they pass, and so do AAPI_00002–00007. onStop() and connectShimmer() aren't in it: both reach Toast.makeText(…).show(), which is null on the stub android.jar.

@jyong15

jyong15 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Tested on a real device

Debug build of 8bfacee (versionCode 37, targetSdk 36) on a Samsung Galaxy Z Fold7 (SM-F966B), Android 16 / API 36, upgraded in place over the #149 build, with permissions already granted. Checked against logcat throughout. The app stayed in one process from launch to streaming, and the crash buffer stayed empty.

Step Result Log
Launch with Bluetooth off No crash. The system prompt "Shimmer Capture is asking to turn on Bluetooth" is shown ShimmerService: ERROR! java.lang.Exception: Bluetooth not Enabled, then Shimmer Service Bound
Deny One "Bluetooth is off…" message, and the prompt doesn't reopen Toast from onActivityResult
Connect to Shimmer device Asks for Bluetooth again
Allow Bluetooth turns on, and the manager is created BluetoothAdapter: onBluetoothOn, then ShimmerService: Bluetooth Manager created about 0.2 s later
Connect and stream a Shimmer3 Streams and plots, with packet reception rate 100%

This covers the case the PR description listed as not covered: connecting to and streaming from a real Shimmer, in this case through the BluetoothManager created after Allow.

Not exercised: Bluetooth on at launch, then switched off and back on through the prompt (the first inline comment in my review), and a fresh install with permissions granted by hand.

🤖 Generated with Claude Code

- 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 <[email protected]>
@MAzalya

MAzalya commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

On the ticket: agreed, the branch and the first two commit subjects (6401c40, 8bfacee) say DEV-1401. The new commit is DEV-1402. Rewording the first two needs a force-push, which my session isn't allowed to do, so I've asked the author to run it.

@jyong15 jyong15 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good and tested

@jyong15
jyong15 merged commit 4c26dca into master Nov 3, 2026
1 check passed
@jyong15
jyong15 deleted the DEV-1401_bluetooth_off_crash branch November 3, 2026 07:29
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