Mobile Web Wearable Web

Sensor: Getting Sensor Data from the Various Device Sensors

This tutorial demonstrates how you can use device sensors.

The Sensor API is optional for both Tizen mobile and wearable profiles, which means that it may not be supported in all mobile and wearable devices. The Sensor API is supported on all Tizen Emulators.

Warm-up

Become familiar with the Sensor API basics by learning about:

Task in Mobile Applications

In the Sensor Ball task, we will walk through simulating ball behavior to create a game.

Managing Sensors

Learning how to start, read and stop a sensor is a basic sensor management skill:

  1. Check that the sensor is supported by the device using the getCapability() method of the SystemInfo interface (in mobile and wearable applications) for the proper capability related to the sensor:

    var proximityCapability = tizen.systeminfo.getCapability("http://tizen.org/feature/sensor.proximity");
    
    if (proximityCapability === true)
    {
       /* The device supports the proximity sensor */
       var proximitySensor = tizen.sensorservice.getDefaultSensor("PROXIMITY");
    }
    
  2. To get all available sensor types, use the getAvailableSensors() method:

    var sensors = tizen.sensorservice.getAvailableSensors();
    console.log("Available sensor: " + sensors.toString());
    
  3. Obtain the Sensor object (in mobile and wearable applications) using the getDefaultSensor() method of the SensorService interface (in mobile and wearable applications). Enable the sensor using the start() method:

    var proximitySensor = tizen.sensorservice.getDefaultSensor("PROXIMITY");
    
    function onsuccessCB()
    {
       console.log("The proximity sensor started successfully.");
    }
    
    proximitySensor.start(onsuccessCB);
  4. To get data from the sensor, use the appropriate method of the sensor object. For example, for the LightSensor (in mobile and wearable applications), use the getLightSensorData() method:

    var lightSensor = tizen.sensorservice.getDefaultSensor("LIGHT");
    
    function onGetSuccessCB(sensorData)
    {
       console.log("light level: " + sensorData.lightLevel);
    }
    
    function onsuccessCB()
    {
       console.log("sensor started");
       lightSensor.getLightSensorData(onGetSuccessCB);
       lightSensor.stop();
    }
    
    lightSensor.start(onsuccessCB);
    
  5. To disable the sensor when it is no longer needed, use the stop() method of the Sensor interface:

    proximitySensor.stop(); 

Receiving Notifications on Sensor Data Changes

Learning how to register a change event handler for sensor data enables your application to react to changes without the need to check current values constantly.

  1. Define an event handler for sensor data changes by implementing the SensorDataSuccessCallback interface (in mobile and wearable applications):
    function onchangedCB(sensorData)
    {
       console.log("value of pressure is : " + sensorData.pressure);
    }
    
  2. Register a change listener to be called when the sensor data changes.

    To register a change listener, use the setChangeListener() method of the Sensor interface (in mobile and wearable applications).

    Whenever the sensor readout changes, the registered event handler is called and a SensorData object (in mobile and wearable applications) is passed to the listener.

    var proximitySensor = tizen.sensorservice.getDefaultSensor("PROXIMITY");
    
    function onsuccessCB()
    {
       console.log("proximity sensor start");
    }
    
    function onchangedCB(sensorData)
    {
       console.log("proximity distance : " + sensorData.proximityState);
    }
    
    proximitySensor.setChangeListener(onchangedCB);
    proximitySensor.start(onsuccessCB);
  3. To stop receiving notifications on sensor data changes, use the unsetChangeListener() method of the Sensor interface.
    proximitySensor.unsetChangeListener();
Go to top