Accessing data from the physical sensors of a device

The Sensor Service API allows widgets to access data provided by the physical sensors of a device. The data from a given sensor is mapped to one or more sensor channels, which the API can listen to. The available sensors depend on the device. You can use the Sensor Service to create widgets that:

  • Search for sensor channels available on a device

  • Listen for data provided by one or more sensor channels

  • Retrieve information about sensor channel properties

Sensor information involves the following concepts:

  • Sensor is a physical sensor on a device (a piece of hardware combined with a software plug-in). A single sensor can provide multiple sensor channels, such as a raw data channel and event channels, or incorporate multiple sensor readings into a single sensor channel.

  • Sensor property is a configuration value of a sensor. The property affects all sensor channels that draw data from the sensor.

  • Channel is an abstraction of a physical sensor. Data from one physical sensor can be mapped to multiple sensor channels.

  • Channel property is a configuration value of a sensor channel. The property affects all clients listening to the channel.

Accessing the API and supported methods

To create a service object for the Sensor Service API, use Service.Sensor to identify the service provider and ISensor to identify the supported interface:

var so = device.getServiceObject("Service.Sensor", "ISensor");

The ISensor interface provides the following methods:

  • FindSensorChannel()

    Use the FindSensorChannel() method to search for sensor channels available on the device.

  • RegisterForNotification()

    Use the RegisterForNotification() method to receive data from one sensor channel.

  • Cancel()

    Use the Cancel() method to stop receiving data provided by a RegisterForNotification() call.

  • GetChannelProperty()

    Use the GetChannelProperty() method to retrieve information about a sensor channel property.

Using the service in a widget

To access sensor data on a device using the Sensor Service API:

  1. Create a service object for the API using device.getServiceObject().

  2. Define the tasks you want to perform and choose the correct methods for them.

  3. Optionally, define how you want the results filtered.

  4. Define methods to process the results.

  5. Retrieve the results using the methods from step 2. Use asynchronous methods together with callback().

  6. Process the results using the methods defined in step 4.

For the complete source of a sample widget that demonstrates how to use this service, see the full example.