语言

Menu
Sites
Language
sensor for native application

i want to use light sensor on mobile device ( tizen z3).

although there is light sensor api, i can't access light sensor on mobile.

i want to know how to use light sensor.

响应

1 回复
Nafisul Islam Kiron

Hello, first add

#include <sensor.h>

Check if your sensor is supported

int error;
bool supported;
sensor_type_e type = SENSOR_LIGHT;

error = sensor_is_supported(type, &supported);

Create an event listener

sensor_h sensor;

sensor_listener_h listener;
error = sensor_create_listener(sensor, &listener);

Then you have to register a callback for the listener

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 the sensor type, assuming there is only 1 sensor for each type
   sensor_type_e type;
   sensor_get_type(sensor, &type);
   switch (type) 
   {
      case SENSOR_LIGHT:
         // Use sensor information
   }
}

error = sensor_listener_set_event_cb(listener, 100, on_sensor_event, user_data);

Set optional keep on/off

error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);

Start listening (from you event/button click/action)

error = sensor_listener_start(listener);

Read sensor data after starting

sensor_event_s event;
error = sensor_listener_read_data(listener, &event);

When your task is done close sensor

error = sensor_listener_unset_event_cb(listener);
error = sensor_listener_stop(listener);
error = sensor_destroy_listener(listener);