Using Proximity Sensor in Tizen Native Application


The proximity sensor measures distance of an object from the front of a phone. It is commonly used on smart devices to detect (and skip) accidental touchscreen taps when held to the ear during a call. When developing application using proximity sensor, consider the following:

  • To avoid annoyance, you can determine whether the proximity sensor is supported on the device before displaying the application features to the user.
  • You can request proximity sensor events to be delivered to the application, where you can process and display them.

In this tip document, demonstration is done on how to detect whether the proximity sensor is supported and calculate current distance value in Tizen Native Application.

Steps to do

Step 1: Adding headers for sensor library

To use the functions and data types of the sensor library, include the header file below in your application.

#include <sensor.h>

Step 2: Creating structure

Create a structure and add two labels to show whether the proximity sensor is supported and distance value by writing the code below:

typedef struct appdata {
	Evas_Object *win;
	Evas_Object *conform;
	Evas_Object *label;
	Evas_Object *isSupported; /* whether the proximity sensor is supported */
	Evas_Object *distanceValue; /* Distance value */
} appdata_s;

Step 3: Check support for proximity sensor

The check_if_supported function identifies whether the proximity sensor is supported, and displays the result in the first label component. The function is written using the code below:

static bool
check_if_supported(appdata_s *ad)
	char buf[PATH_MAX];
	bool is_supported = false;
	sensor_is_supported(SENSOR_PROXIMITY, &is_supported);

	sprintf(buf, "Proximity sensor is supported");

	else sprintf(buf, "Proximity sensor is not supported");

	elm_object_text_set(ad->isSupported, buf);

	return is_supported;

In the function above, the sensor_is_supported function requests the support information. In this function, SENSOR_PROXIMITY is passed as the first parameter. It returns the proximity support information in the second parameter.

Step 4: Adding UI component

The my_box_pack function adds a UI component to a box container. It is written by using the code below:

static void
my_box_pack(Evas_Object *box, Evas_Object *child,
            double h_weight, double v_weight, double h_align, double v_align)
    /* Tell the child packed into the box to be able to expand */
    evas_object_size_hint_weight_set(child, h_weight, v_weight);
    /* Fill the expanded area (above) as opposed to centering in it */
    evas_object_size_hint_align_set(child, h_align, v_align);
    /* Set the child as the box content and show it */
    elm_object_content_set(box, child);

    /* Put the child into the box */
    elm_box_pack_end(box, child);
    /* Show the box */

Step 5: Creating structure with handle and listener

To request the sensor events, a structure containing sensor handle and event listener is required. The structure can be created writing the code below:

typedef struct _sensor_info {
          sensor_h sensor; /* Sensor handle */
          sensor_listener_h sensor_listener; /* Sensor listener */
} sensorinfo_s;

Step 6: Requesting sensor events

Step 6.1: Starting the listener

The start_proximity_sensor function starts the proximity sensor and specifies the event callback function. The function can be written as below:

static void
start_proximity_sensor(appdata_s *ad)
    sensor_error_e err = SENSOR_ERROR_NONE;
    sensor_get_default_sensor(SENSOR_PROXIMITY, &sensor_info.sensor);
    err = sensor_create_listener(sensor_info.sensor, &sensor_info.sensor_listener);
    sensor_listener_set_event_cb(sensor_info.sensor_listener, 100, _new_sensor_value, ad);

In the above function, the sensor_get_default_sensor function is used to get a specific sensor handle. SENSOR_PROXIMITY is passed as the first parameter and returns proximity sensor handle to the second parameter.

The sensor_create_listener function is used to create an event listener. Passing a sensor handle to the first parameter returns a listener object to the second parameter.

The sensor_listener_set_event_cb function registers a callback function to be invoked when a sensor event occurs.

Finally, the sensor_listener_start function starts the listener.

Step 6.2: Getting sensor data

The _new_sensor_value function is an event callback for the proximity sensor, and it outputs sensor value to the screen. The sensor data is passed to the second parameter. The distance data is saved in values[0].

The callback function can be written as below:

static void
_new_sensor_value(sensor_h sensor, sensor_event_s *sensor_data, void *user_data)
    if (sensor_data->value_count < 1)
    char buf[PATH_MAX];
    appdata_s *ad = (appdata_s*)user_data;

    sprintf(buf, "Distance: %0.1f", sensor_data->values[0]);
    elm_object_text_set(ad->distanceValue, buf);

Step 7: Invoking the event listener

After checking if the device provides support for the sensor call the start_proximity_sensor function from the end of create_base_gui function.

bool status = check_if_supported(ad);

Running the sample application

Now, the attached sample application is capable of showing the distance between an object and the device. Build and run the sample application and get desired result on the screen of the device.

Figure: Sample application shows the differences in distance value between device and object



첨부 파일: 
SDK Version Since: 
2.4 mobile/2.3.1 wearable