Mobile native Wearable native

Sensor: Using Sensors and Managing Sensor Events

This tutorial demonstrates how you can manage sensors, retrieve information from and about them, and set intervals for sensor events.

Warm-up

Become familiar with the Sensor API basics by learning about:

Retrieving a Sensor

To declare the variables and get a sensor handle:

  1. To use the Sensor API (in mobile and wearable applications), include the following header file:
    #include <sensor.h>
    
  2. Check whether a sensor is supported:
    int error;
    bool supported;
    sensor_type_e type = SENSOR_ACCELEROMETER;
    
    error = sensor_is_supported(type, &supported);
    
  3. Get a handle of the default sensor or a sensor list:
    int error;
    sensor_type_e type = SENSOR_ACCELEROMETER;
    sensor_h sensor;
    
    error = sensor_get_default_sensor(type, &sensor);
    
    int error;
    sensor_type_e type = SENSOR_ALL;
    sensor_h *list;
    
    error = sensor_get_default_sensor(type, &list);
    
    // API caller must explicitly free this list after use
    // free(list);
    

Registering a Sensor Event

To set and unset callback functions for sensor events:

  1. To use the Sensor API (in mobile and wearable applications), include the following header file:
    #include <sensor.h>
    
  2. Create an event listener:
    sensor_h sensor;
    
    sensor_listener_h listener;
    error = sensor_create_listener (sensor, &listener);
  3. Register a callback:
    void on_sensor_event(sensor_h sensor, sensor_event_s *event, void *user_data)
    {
       // Select a specific sensor with a sensor handle
       // This example uses sensor type, assuming there is only 1 sensor for each type
       sensor_type_e type;
       sensor_get_type(sensor, &type);
       switch (type) 
       {
          case SENSOR_ACCELEROMETER:
             // Use sensor information
       }
    }
    
    error = sensor_listener_set_event_cb(listener, 100, on_sensor_event, user_data);
    

    The following example shows the use of sensor_event_s:

    // In sensor.h
    typedef struct                   
    {                                
       int accuracy;                
       unsigned long long timestamp;
       int value_count;             
       float values[MAX_VALUE_SIZE];
    }
    sensor_event_s;
  4. Reset the interval (in milliseconds) (optional):

    In case of the proximity sensor, the setting interval has no effect. The proximity event occurs when the value changes.

    error = sensor_listener_set_interval(listener, 100);
    
  5. Set the optional sensor option (in mobile and wearable applications):
    error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);
    
  6. Start a sensor listener:
    error = sensor_listener_start(listener);
    
  7. Read sensor data (optional).

    In order to read sensor data, call the sensor_listener_start() function:

    sensor_event_s event;
    error = sensor_listener_read_data(listener, &event);
    
  8. Unset the callback:
    error = sensor_listener_unset_event_cb(listener);
    
  9. Stop the listener:
    error = sensor_listener_stop(listener);
    
  10. Destroy the listener:
    error = sensor_destroy_listener(listener);
    
  11. Read sensor details (optional):
    char *name;
    char *vendor;
    sensor_type_e type;
    float min_range;
    float max_range;
    float resolution;
    int min_interval;
    
    error = sensor_get_name(listener, &name);
    error = sensor_get_vendor(listener, &vendor);
    error = sensor_get_type(listener, &type);
    error = sensor_get_min_range(listener, &min_range);
    error = sensor_get_max_range(listener, &max_range);
    error = sensor_get_resolution(listener, &resolution);
    error = sensor_get_min_interval(listener, &min_interval);
    

Registering the Accuracy Changed Callback

To register the accuracy changed callback:

  1. To use the Sensor API, include the following header file:
    #include <sensor.h>
    
  2. Create an event listener:
    sensor_h sensor;
    
    sensor_listener_h listener;
    error = sensor_create_listener (sensor, &listener);
    
  3. Register the accuracy changed callback:
    error = sensor_listener_set_accuracy_cb (listener, sensor_accuracy_changed_cb, user_data);
    void sensor_accuracy_changed_cb(sensor_h sensor, unsigned long long timestamp, 
                                    sensor_data_accuracy_e accuracy, void *data)
    {
       // Use the timestamp or accuracy from the sensor
    }
Go to top