Class 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.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSensorSession(String sensorId, HealthSensorProfile profile, SensorSessionOptions options) Ports andHealthSensorsconstruct sessions. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddListener(SensorSampleListener listener) Registers a listener for measurements and state changes.protected final voidfireError(HealthException error) Delivers an error to listeners on the EDT.protected final voidfireSample(HealthSample sample) Delivers a sample to listeners on the EDT.protected final voidWrites anything still buffered.protected final voidDrops this session fromHealthSensors.getActiveSessions().final IntegerThe device's battery level as a percentage, ornullwhen it does not report one.final intWhere a heart-rate sensor is worn, as aBodySensorLocationconstant, or-1when unreported.final HealthSamplegetLatest(HealthDataType type) The most recent sample oftype, ornullwhen none has arrived or the last one is older thanSensorSessionOptions.getStaleSampleMillis().final SensorSessionOptionsThe options this session was created with.final HealthSensorProfileThe profile this session is speaking.final StringThe stable identifier of the connected device -- seeHealthSensor.getId().final SensorSessionStategetState()The current lifecycle state.protected final voidonMeasurement(byte[] value, long receivedAtMillis) Decodes one raw characteristic value and emits the resulting samples.final voidremoveListener(SensorSampleListener listener) Removes a previously registered listener.Asks a glucose meter to replay stored records, delivering each one through the normal sample listeners, and resolving with how many were retrieved.protected final voidForgets the cumulative-counter baselines, so the next notification establishes a new one.Resets a heart-rate strap's accumulated energy-expended counter, by writing0x01to the Heart Rate Control Point (0x2A39).protected final voidsetBatteryPercent(Integer percent) Records the device's battery level.protected final voidsetBodySensorLocation(int location) Records where a heart-rate sensor is worn.protected final voidsetState(SensorSessionState newState) Moves the session to a new state and notifies listeners.voidstop()Disconnects and stops delivering measurements.toString()Returns a string representation of the object.
-
Constructor Details
-
SensorSession
Ports andHealthSensorsconstruct sessions.
-
-
Method Details
-
getSensorId
The stable identifier of the connected device -- seeHealthSensor.getId(). -
getProfile
The profile this session is speaking. -
getOptions
The options this session was created with. -
getState
The current lifecycle state. -
addListener
Registers a listener for measurements and state changes. -
removeListener
Removes a previously registered listener. -
getLatest
The most recent sample of
type, ornullwhen none has arrived or the last one is older thanSensorSessionOptions.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
The device's battery level as a percentage, ornullwhen it does not report one. -
getBodySensorLocation
public final int getBodySensorLocation()Where a heart-rate sensor is worn, as aBodySensorLocationconstant, or-1when unreported. -
resetEnergyExpended
Resets a heart-rate strap's accumulated energy-expended counter, by writing
0x01to the Heart Rate Control Point (0x2A39).Fails with
HealthError.NOT_SUPPORTEDon other profiles and on straps that do not expose the control point. -
requestStoredRecords
Asks a glucose meter to replay stored records, delivering each one through the normal sample listeners, and resolving with how many were retrieved.
Fails with
HealthError.NOT_SUPPORTEDon other profiles. -
stop
public void stop()Disconnects and stops delivering measurements. Idempotent. -
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_DATArather than throwing: one misbehaving device must not take down the app. -
flushPendingWrites
protected final void flushPendingWrites()Writes anything still buffered. Called when the session stops so a short ride does not lose its last partial batch. -
setBatteryPercent
Records the device's battery level. Called by the transport. -
setBodySensorLocation
protected final void setBodySensorLocation(int location) Records where a heart-rate sensor is worn. Called by the transport. -
resetCounters
protected final void resetCounters()Forgets the cumulative-counter baselines, so the next notification establishes a new one. Called by the transport on reconnect: a sensor that power-cycled restarts its counters from zero. -
setState
Moves the session to a new state and notifies listeners. -
fireSample
Delivers a sample to listeners on the EDT. -
fireError
Delivers an error to listeners on the EDT. -
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
-