Module: Toybox.Sensor
Overview
The Sensor module provides access to sensor data.
Sensor allows Apps to register for updates to the current sensor data. It also enables apps to control the ANT+ sensors supported natively by the device, which are described by the provided SENSOR_* constants.
Classes Under Namespace
Classes: AccelerometerData, GyroscopeData, HeartRateData, Info, MagnetometerData, SensorData, SensorInfo, SensorInfoIterator, TooManySensorDataListenersException
Constant Summary
-
RemoteSensorType
-
OnboardSensorType
-
SensorTechnology
Name | Value | Since | Description | See Also |
---|---|---|---|---|
SENSOR_BIKESPEED | 0 | API Level 1.0.0 |
||
SENSOR_BIKECADENCE | 1 | API Level 1.0.0 |
||
SENSOR_BIKEPOWER | 2 | API Level 1.0.0 |
||
SENSOR_FOOTPOD | 3 | API Level 1.0.0 |
||
SENSOR_HEARTRATE | 4 | API Level 1.0.0 |
||
SENSOR_TEMPERATURE | 5 | API Level 1.0.0 |
Name | Value | Since | Description | See Also |
---|---|---|---|---|
SENSOR_PULSE_OXIMETRY | 6 | API Level 3.2.0 |
||
SENSOR_ONBOARD_PULSE_OXIMETRY | 7 | API Level 3.2.0 |
||
SENSOR_ONBOARD_HEARTRATE | 8 | API Level 3.2.0 |
The sensor technology
Describes the technology used to communicate with the sensor.
Name | Value | Since | Description | See Also |
---|---|---|---|---|
SENSOR_TECHNOLOGY_ANT | 0 | API Level 3.2.0 |
ANT Sensor |
|
SENSOR_TECHNOLOGY_BLE | 1 | API Level 3.2.0 |
Bluetooth Low Energy Sensor |
|
SENSOR_TECHNOLOGY_ONBOARD | 2 | API Level 3.2.0 |
Onboard Sensor |
Typedef Summary collapse
Instance Method Summary collapse
-
disableSensorType(sensorType as Sensor.SensorType) as Lang.Boolean
Disable the given sensor type for use.
-
enableSensorEvents(listener as Null or Lang.Method(info as Sensor.Info) as Void) as Void
Request sensor events from enabled sensors.
-
enableSensorType(sensorType as Sensor.SensorType) as Lang.Boolean
Enable the given sensor type for use.
-
getInfo() as Sensor.Info
Get the current Sensor Sensor.Info.
-
getMaxSampleRate() as Lang.Number
Get the maximum sample rate supported by the system.
-
getMaxSampleRateForSensorType(sensorDataType as Lang.Symbol) as Lang.Number
Get the maximum sample rate supported for given sensor data type.
-
getRegisteredSensors(sensorType as Sensor.SensorType or Null) as Sensor.SensorInfoIterator
Retrieve the sensors that are currently registered.
-
registerSensorDataListener(listener as Lang.Method(data as Sensor.SensorData) as Void, options as { :period as Lang.Number, :accelerometer as { :enabled as Lang.Boolean, :sampleRate as Lang.Number, :includePower as Lang.Boolean, :includePitch as Lang.Boolean, :includeRoll as Lang.Boolean }, :gyroscope as { :enabled as Lang.Boolean, :sampleRate as Lang.Number }, :magnetometer as { :enabled as Lang.Boolean, :sampleRate as Lang.Number }, :heartBeatIntervals as { :enabled as Lang.Boolean } }) as Void
Register a callback to fetch high-frequency data from various sensors.
-
setEnabledSensors(sensors as Lang.Array<Sensor.SensorType>) as Lang.Array<Sensor.SensorType>
Enable sensors for use.
-
unregisterSensorDataListener() as Void
Unregister a previously registered data listener.
Instance Method Details
disableSensorType(sensorType as Sensor.SensorType) as Lang.Boolean
Disable the given sensor type for use.
Unlike the existing setEnabledSensors() function, this will not enable/disable other sensor types.
Will cause an app crash if called from a data field app
enableSensorEvents(listener as Null or Lang.Method(info as Sensor.Info) as Void) as Void
Request sensor events from enabled sensors.
Sensor events are retrieved from any enabled sensors at a rate of 1 Hz. The data retrieved from enabled sensors is passed to the listener Method provided as a parameter to this method.
Will cause an app crash if called from a data field app
enableSensorType(sensorType as Sensor.SensorType) as Lang.Boolean
Enable the given sensor type for use.
Unlike the existing setEnabledSensors() function, this will not enable/disable other sensor types.
Will cause an app crash if called from a data field app
Multitasking: Sensor states can not be changed while in inacitve mode and sensor enabled during active mode will be disabled when app becomes inactive, and re-enabled automatically when is active again. These state changes are denoted by calls to AppBase.onActive() and AppBase.onInactive().
getInfo() as Sensor.Info
Get the current Sensor Sensor.Info.
This is useful for retrieving the current sensor info either on demand or periodically within a Timer.
Will cause an app crash if called from a data field app
getMaxSampleRate() as Lang.Number
Get the maximum sample rate supported by the system.
Will cause an app crash if called from a data field app
This function can produce different results after the app transitions to the active state after being inactive. These state changes are denoted by calls to AppBase.onActive() and AppBase.onInactive().
getMaxSampleRateForSensorType(sensorDataType as Lang.Symbol) as Lang.Number
Get the maximum sample rate supported for given sensor data type.
Will cause an app crash if called from a data field app
This function can produce different results after the app transitions to the active state after being inactive. These state changes are denoted by calls to AppBase.onActive() and AppBase.onInactive().
getRegisteredSensors(sensorType as Sensor.SensorType or Null) as Sensor.SensorInfoIterator
Retrieve the sensors that are currently registered.
This function returns an iterator for the sensors that are considered to be `registered` if pairing information has been provided for it in Sensor settings.
registerSensorDataListener(listener as Lang.Method(data as Sensor.SensorData) as Void, options as { :period as Lang.Number, :accelerometer as { :enabled as Lang.Boolean, :sampleRate as Lang.Number, :includePower as Lang.Boolean, :includePitch as Lang.Boolean, :includeRoll as Lang.Boolean }, :gyroscope as { :enabled as Lang.Boolean, :sampleRate as Lang.Number }, :magnetometer as { :enabled as Lang.Boolean, :sampleRate as Lang.Number }, :heartBeatIntervals as { :enabled as Lang.Boolean } }) as Void
Register a callback to fetch high-frequency data from various sensors.
The callback will get invoked each time a new set of sensor data over the length of time specified in the period option is available.
Only one data request is allowed to be registered at a time. Subsequent calls to this function for the same sensor type will override previously registered requests.
Will cause an app crash if called from a data field app
setEnabledSensors(sensors as Lang.Array<Sensor.SensorType>) as Lang.Array<Sensor.SensorType>
Enable sensors for use.
This will enable both connected ANT+ sensors and system sensors if possible.
Will cause an app crash if called from a data field app
Multitasking: Sensor states can not be changed while in inacitve mode and sensor enabled during active mode will be disabled when app becomes inactive, and re-enabled automatically when is active again. These state changes are denoted by calls to AppBase.onActive() and AppBase.onInactive().
unregisterSensorDataListener() as Void
Unregister a previously registered data listener.
Will cause an app crash if called from a data field app