Skip to content

DEV-1427: ShimmerService works without a BluetoothManager throughout - #151

Open
MAzalya wants to merge 1 commit into
masterfrom
DEV-1427_service_bt_manager_guards
Open

MAzalya wants to merge 1 commit into
masterfrom
DEV-1427_service_bt_manager_guards

Conversation

@MAzalya

@MAzalya MAzalya commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #150 (DEV-1402), whose review asked for it.

ShimmerService.btManager is null while Bluetooth was off when the service was created (onCreate logs "Bluetooth not Enabled"). #150 guarded the calls ShimmerCapture reaches. The other 49 methods that use btManager still threw a NullPointerException, as did the four library fragments that dereference getBluetoothManager(). Apps using ShimmerService from source or from the published shimmerandroidinstrumentdriver package could hit them.

Rule: no BluetoothManager means no devices

Every method that uses the manager first returns what ShimmerBluetoothManagerAndroid itself returns for a device it doesn't know, read from each manager method:

Methods Without a manager
Commands: start/stopStreaming, start/stopLogging, write*, setAll*, toggle*LED*, configureShimmer(s), … do nothing
getSamplingRate, getAccelRange, getGSRRange, get5VReg, getpmux, getBattLimitWarning, getPacketReceptionRate -1
getEnabledSensors, sensorConflictCheckandCorrection, getShimmerVersion 0
getShimmerState DISCONNECTED
isSensing, isDocked, isLowPowerMagEnabled, isUsingLogAndStreamFW, DevicesConnected, DeviceIsLogging, DeviceIsStreaming false
getFWVersion ""
getListofEnabledSensors null
getListofEnabledSensorSignals empty list

Two methods are handled by hand:

  • handleMsgStateChange() skips the manager in its CONNECTED, SDLOGGING and DISCONNECTED cases.
  • bluetoothAddressComparator() is a string comparison, so it now compares the two addresses itself.

The rule is written down on the btManager field.

Why not a null-object manager: ShimmerBluetoothManagerAndroid's constructor throws when Bluetooth is off, and other apps rely on that.

getBluetoothManager()

It keeps returning null when there's no manager; that's now documented: "callers must check for null". Its callers in the library:

  • PlotFragment (both copies): read the manager once and treat null as "no sensor list", which the code already handles.
  • SignalsToPlotFragment (both copies): use the service's null-safe getShimmer(). They also no longer throw for a device the service doesn't know (device != null && device.isStreaming()).

shimmerServiceExample's MainActivity calls it in six places, all for a device the service already has. Since getShimmer() returns null without a manager, a device existing means a manager exists, so MainActivity is unchanged.

Tests

AAPI_00008 gains four tests:

  • every newly guarded command runs without throwing;
  • every query returns the unknown-device value from the table;
  • the address comparison still works;
  • CONNECTED, SDLOGGING and DISCONNECTED state messages are handled.

Against master's ShimmerService all four fail; with this change all 7 pass, and so do AAPI_00002–00007. assembleDebug builds every example app, and the library's release build passes. Paths that show a Toast (connectShimmer, onStop) are left out: Toast.makeText() returns null on the stub android.jar.

Notes

  • Apps using the published package get this at the next Release Android API run.
  • Android-only: no other host API needs it.
  • No behaviour change when Bluetooth is on: every guard is if (btManager == null).

🤖 Generated with Claude Code

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

1 participant