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 plugin). 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.
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:
Use the FindSensorChannel()
method to search for
sensor channels available on the device.
Use the RegisterForNotification()
method to receive
data from one sensor channel.
Use the Cancel()
method to stop receiving data
provided by a RegisterForNotification()
call.
Use the GetChannelProperty()
method to retrieve
information about a sensor channel property.
To access sensor data on a device using the Sensor Service API:
Create a service
object for the API using device.getServiceObject()
.
Define the tasks you want to perform and choose the correct methods for them.
Optionally, define how you want the results filtered.
Define methods to process the results.
Retrieve the
results using the methods from step 2. Use asynchronous methods together with callback()
.
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.