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
Check the sensor support and retrieve the sensor.
-
Registering a Sensor Event
Get a specific sensor using criteria such as vendor and name.
-
Registering the Accuracy Changed Callback
Create an event which is called when the accuracy is changed.
Retrieving a Sensor
To declare the variables and get a sensor handle:
- To use the Sensor API (in mobile and wearable applications), include the following header file:
#include <sensor.h>
- Check whether a sensor is supported:
int error; bool supported; sensor_type_e type = SENSOR_ACCELEROMETER; error = sensor_is_supported(type, &supported);
- 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:
- To use the Sensor API (in mobile and wearable applications), include the following header file:
#include <sensor.h>
- Create an event listener:
sensor_h sensor; sensor_listener_h listener; error = sensor_create_listener (sensor, &listener);
- 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;
- 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);
- Set the optional sensor option (in mobile and wearable applications):
error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);
- Start a sensor listener:
error = sensor_listener_start(listener);
- 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);
- Unset the callback:
error = sensor_listener_unset_event_cb(listener);
- Stop the listener:
error = sensor_listener_stop(listener);
- Destroy the listener:
error = sensor_destroy_listener(listener);
- 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:
- To use the Sensor API, include the following header file:
#include <sensor.h>
- Create an event listener:
sensor_h sensor; sensor_listener_h listener; error = sensor_create_listener (sensor, &listener);
- 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 }