public class SensorSession
- Object
- SensorSession
A live connection to one health sensor, decoding its notifications into health samples.
Obtained from
HealthSensors.connect(HealthSensor,HealthSensorProfile,SensorSessionOptions).
All listener callbacks arrive on the EDT.
Derived values
Speed and cadence sensors transmit cumulative counters rather than
rates, so this class differences consecutive notifications – handling
the counter and event-timer wraps that would otherwise produce spikes
of tens of thousands of rpm. See CumulativeCounterTracker.
Constructors
protected SensorSession(String sensorId, HealthSensorProfile profile, SensorSessionOptions options) | Ports and HealthSensors construct sessions. |
Methods
Inherited methods
Constructor details
SensorSession
protected SensorSession(String sensorId, HealthSensorProfile profile, SensorSessionOptions options)HealthSensors construct sessions.Method details
getSensorId
public final String getSensorId()HealthSensor.getId().getProfile
public final HealthSensorProfile getProfile()getOptions
public final SensorSessionOptions getOptions()The options this session was created with.
A copy, so that reconfiguring what this returns cannot change a running session – the same reason the session took a snapshot of what it was given.
getState
public final SensorSessionState getState()addListener
public final void addListener(SensorSampleListener listener)removeListener
public final void removeListener(SensorSampleListener listener)getLatest
public final HealthSample getLatest(HealthDataType type)The most recent sample of type, or null when none has arrived
or the last one is older than
SensorSessionOptions.getStaleSampleMillis().
Returning null rather than a stale value is deliberate: a UI bound to this shows a dash when the strap falls off, instead of continuing to display the wearer’s last heart rate as though it were current.
getBatteryPercent
public final Integer getBatteryPercent()null when it does
not report one.getBodySensorLocation
public final int getBodySensorLocation()BodySensorLocation
constant, or -1 when unreported.resetEnergyExpended
public AsyncResource<Boolean> resetEnergyExpended()Resets a heart-rate strap’s accumulated energy-expended counter, by
writing 0x01 to the Heart Rate Control Point (0x2A39).
Fails with HealthError.NOT_SUPPORTED on other profiles and on
straps that do not expose the control point.
requestStoredRecords
public AsyncResource<Integer> requestStoredRecords(GlucoseRecordFilter filter)Asks a glucose meter to replay stored records, delivering each one through the normal sample listeners, and resolving with how many were retrieved.
Not implemented in this release: this always fails with
HealthError.NOT_SUPPORTED, on the glucose profile as well as any
other. Replay runs over the Record Access Control Point, a
stateful request/response protocol on a second characteristic with
its own operators, filters and abort semantics, and shipping it
untested against real meters would be worse than saying plainly
that it is absent. GlucoseRecordFilter and the rest of the
vocabulary are here so the call site does not have to change when
it lands.
Live glucose notifications are unaffected and work today.
stop
public void stop()forgetFromManager
protected final void forgetFromManager()Drops this session from HealthSensors.getActiveSessions().
Without it a stopped or failed session stayed in the registry for the manager’s lifetime, so the list documented as “connected or reconnecting” filled up with dead sessions and kept their peripherals and listeners alive with them.
onMeasurement
protected final void onMeasurement(byte[] value, long receivedAtMillis)Decodes one raw characteristic value and emits the resulting samples. Called by the transport when a notification arrives.
A malformed payload emits nothing and reports
HealthError.INVALID_DATA rather than throwing: one misbehaving
device must not take down the app.
flushPendingWrites
protected final void flushPendingWrites()setBatteryPercent
protected final void setBatteryPercent(Integer percent)setBodySensorLocation
protected final void setBodySensorLocation(int location)resetCounters
protected final void resetCounters()hasStreamed
protected final boolean hasStreamed()Moves the session to a new state and notifies listeners. True once this session has actually streamed.
A failed initial connect also publishes DISCONNECTED, and reconnecting from that resurrects a session the caller was already told had failed.
setState
protected final boolean setState(SensorSessionState newState)Moves to newState, reporting whether the move happened.
A refusal is not merely a no-op for the caller: it means the session went terminal underneath, and whatever the caller was about to do next – connect a peripheral, arm a timer – would be done on behalf of a session that has already been torn down.
fireSample
protected final void fireSample(HealthSample sample)fireError
protected final void fireError(HealthException error)toString
public String toString()