Wearable Web

Sensor

Tizen enables you to access the sensor data of a wearable device using the Sensor API.

The main features of the Sensor API include:

  • Activating and deactivating notifications for device sensor changes

    You can activate the sensor notifications by registering a sensor data change listener with the start() and setChangeListener() methods of the Sensor interface.

    When notifications are no longer needed, you can unregister the listener by using the unsetChangeListener() method of the Sensor interface.

  • Retrieving current sensor data from the device

    You can retrieve the current device sensor data using a sensor-specific method, such as getMagneticSensorData() for the magnetic sensor.

The following table lists the supported sensor types.

Table: Sensor types
Type Description
LIGHT Provides the light sensor data, such as the ambient light level.
MAGNETIC Provides the magnetic sensor data, such as the ambient magnetic field of the X, Y, and Z axis.
PRESSURE Provides the pressure sensor data.
PROXIMITY Provides the proximity sensor data, such as the proximity state.
ULTRAVIOLET Provides the ultraviolet sensor data, such as the ultraviolet index.
Note
The LIGHT, MAGNETIC, PRESSURE, PROXIMITY, and ULTRAVIOLET sensor types are deprecated since Tizen 2.3. Use the Tizen public Sensor API (tizen.sensorservice since Tizen 2.3) instead.

Managing a Device Sensor

Learning how to start a device sensor and register a listener for getting updated sensor data is a basic sensor management skill:

  1. To start a device sensor, use the start() method:
    function onStartSuccessCB(resultSet)
    {
       console.log("Start Success");
    }
    
    var magneticSensor = webapis.sensorService.getDefaultSensor("MAGNETIC");
    magneticSensor.start(onStartSuccessCB);
    
  2. To register a listener for the device sensor:
    1. Define the listener callback (action to be taken when the sensor data changes). In the following example, the changed values are recorded in the console log:
      function onchangedCB(results) 
      {
         console.log("Magnetic X: " + results.x);
         console.log("Magnetic Y: " + results.y);
         console.log("Magnetic Z: " + results.z);
      }
      
    2. Use the setChangeListener() method to register the listener:
      var magneticSensor = webapis.sensorService.getDefaultSensor("MAGNETIC");
      magneticSensor.setChangeListener(onchangedCB);
      

Getting the Device Sensor Data

Learning how to get the current sensor data is a basic sensor management skill:

  1. To start a device sensor, use the start() method:
    function onStartSuccessCB(resultSet)
    {
       console.log("Start Success");
    }
    
    var magneticSensor = webapis.sensorService.getDefaultSensor("MAGNETIC");
    magneticSensor.start(onStartSuccessCB);
    
  2. Retrieve the current sensor data:
    1. Define a callback for handling the retrieved data. In the following example, the current values are recorded in the console log:
      function onsuccessCB(results) 
      {
         console.log("Magnetic X: " + results.x);
         console.log("Magnetic Y: " + results.y);
         console.log("Magnetic Z: " + results.z);
      }
      
    2. Use the getMagneticSensorData() method to retrieve the data:
      magneticSensor.getMagneticSensorData(onsuccessCB);
      
Go to top