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.

Examples:

Using heart rate sensors to display current heart rate

using Toybox.Sensor;
function initialize() {
    Sensor.setEnabledSensors([Sensor.SENSOR_HEARTRATE]);
    Sensor.enableSensorEvents(method(:onSensor));
}

function onSensor(sensorInfo) {
    System.println("Heart Rate: " + sensorInfo.heartRate);
}

Since:

  • 1.0.0

App Types:

  • Widget

  • App

  • Audio Content Provider App

  • Background Services

Requires Permission:

  • Sensor

Defined Under Namespace

Classes: AccelerometerData, HeartRateData, Info, SensorData, TooManySensorDataListenersException

Constant Summary

SENSOR_BIKESPEED = 0

Since:

  • 1.0.0

SENSOR_BIKECADENCE = 1

Since:

  • 1.0.0

SENSOR_BIKEPOWER = 2

Since:

  • 1.0.0

SENSOR_FOOTPOD = 3

Since:

  • 1.0.0

SENSOR_HEARTRATE = 4

Since:

  • 1.0.0

SENSOR_TEMPERATURE = 5

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

enableSensorEvents(listener) ⇒ Object

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.

Examples:

using Toybox.Sensor;
// Given an onSensor listener method is defined
Sensor.enableSensorEvents(method(:onSensor));

Parameters:

  • listener (Toybox::Lang::Method)

    A reference to a listener Method:

    • Called when sensor updates are received

    • Receives a Sensor.info object

    • Use null to specify no listener

Since:

  • 1.0.0


8518
8519
# File 'Monkeybrains.rb', line 8518

def enableSensorEvents(listener)
end

getInfoToybox::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.

Examples:

Get accelerometer data once per second

using Toybox.Sensor;
using Toybox.System;
using Toybox.Timer;
var dataTimer = new Timer.Timer();
dataTimer.start(method(:timerCallback), 1000, true); // A one-second timer
function timerCallback() {
    var sensorInfo = Sensor.getInfo();
    if (sensorInfo has :accel && sensorInfo.accel != null) {
        var accel = sensorInfo.accel;
        var xAccel = accel[0];
        var yAccel = accel[1];
        System.println("x: " + xaccel + ", y: " + yAccel);
    }
}

Returns:

See Also:

Since:

  • 1.0.0


8627
8628
# File 'Monkeybrains.rb', line 8627

def getInfo()
end

getMaxSampleRateToybox::Lang::Number

Get the maximum sample rate supported by the system.

Examples:

using Toybox.Sensor;
var maxSample = Sensor.getMaxSampleRate();

Returns:

Since:

  • 2.3.0


8837
8838
# File 'Monkeybrains.rb', line 8837

def getMaxSampleRate()
end

registerSensorDataListener(listener, options) ⇒ Object

Note:

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.

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.

Examples:

using Toybox.Sensor;
using Toybox.ActivityRecording;
// Set up a fit session to record the data
var mSession = ActivityRecording.createSession(
    {
        :name=>"PitchCounter",
        :sport=>ActivityRecording.SPORT_GENERIC,
        :sensorLogger => mLogger
    }
);
// initialize accelerometer
var options = {
    :period => 1,               // 1 second sample time
    :accelerometer => {
        :enabled => true,       // Enable the accelerometer
        :sampleRate => 25       // 25 samples
    }
    :heartBeatIntervals => {
        :enabled => true
    }
};
// Using the callback setup in Toybox.SensorHistory.SensorData
Sensor.registerSensorDataListener(method(:accel_callback), options);
mSession.start();                           // Start Activity Recording

Parameters:

Options Hash (options):

Raises:

See Also:

Since:

  • 2.3.0


8816
8817
# File 'Monkeybrains.rb', line 8816

def registerSensorDataListener(listener, options)
end

setEnabledSensors(sensors) ⇒ Toybox::Lang::Array

Enable sensors for use.

This will enable both connected ANT+ sensors and system sensors if possible.

Examples:

Enable a heart rate sensor

using Toybox.Sensor;
Sensor.setEnabledSensors([Sensor.SENSOR_HEARTRATE]);

Parameters:

  • sensors (Toybox::Lang::Array)

    The sensors to enable:

    • An Array of SENSOR_* types to enable

    • An empty array ([]) to disable all sensors

Returns:

Since:

  • 1.0.0


8501
8502
# File 'Monkeybrains.rb', line 8501

def setEnabledSensors(sensors)
end

unregisterSensorDataListenerObject

Unregister a previously registered data listener.

Examples:

// Assuming use of registerSensorDataListener() example and mSession
using Toybox.Sensor;

Sensor.unregisterSensorDataListener(); // Unregister Listener
mSession.stop();                              // Stop Activity Recording

See Also:

Since:

  • 2.3.0


8828
8829
# File 'Monkeybrains.rb', line 8828

def unregisterSensorDataListener()
end