Mobile native Wearable native

Gesture Recognition: Detecting Device Gestures

This tutorial demonstrates how you can detect gestures performed with the device.

Warm-up

Become familiar with the Gesture Recognition API basics by learning about:

Detecting Gestures

To set and unset callback functions for user gestures:

  1. To use the gesture recognition-related features of the Gesture Recognition API (in mobile and wearable applications), include the <gesture_recognition.h> header file in your application:

    #include <gesture_recognition.h>
    
  2. Create a handle for gesture detection using the gesture_create() function:
    gesture_h handle;
    gesture_create(&handle);
  3. To subscribe to notifications about gesture events, invoke the gesture_start_recognition() function to register a callback function and start gesture detection.

    When the registered gesture is detected, the gesture_cb callback is invoked.

    gesture_start_recognition(handle, GESTURE_PICK_UP, GESTURE_OPTION_DEFAULT, gesture_cb, NULL);

    The above example starts gesture detection to receive notifications when the GESTURE_PICK_UP gesture is detected. The application can use any of the gesture_type_e enumerators (in mobile and wearable applications) in place of the GESTURE_PICK_UP value. Note that not all gestures are supported by all devices. In such cases, the function returns the GESTURE_ERROR_NOT_SUPPORTED value.

    If the default gesture option is designated, the system tries to reduce power consumption to detect the gesture. For example, the gesture is not necessarily detected while the display is switched off. Using the GESTURE_OPTION_ALWAYS_ON option prevents such power-saving behaviors. For more information about the gesture options, see the gesture_option_e enumeration (in mobile and wearable applications).

  4. When the gesture_cb() callback function is invoked, you can retrieve the event data using the gesture_get_event() function:

    void gesture_cb(gesture_type_e gesture, const gesture_data_h data, double timestamp, gesture_error_e error, void *user_data)
    {			
       gesture_event_e event;
       if (gesture == GESTURE_PICK_UP) 
       {
          gesture_get_event(data, &event);
    
          if (event == GESTURE_EVENT_DETECTED) 
          {
             // Handle the event
          }
       }
    }

    If the application registered multiple gestures to a single callback function, the input parameter gesture can be used to distinguish the gesture received.

    Some gestures can return different types of events. For example, GESTURE_SHAKE can return GESTURE_SHAKE_DETECTED or GESTURE_SHAKE_FINISHED.

    In case of GESTURE_TILT, the gesture_get_tilt() function can be used to extract the tilting angles.

    int x, y;
    if (gesture == GESTURE_TILT) 
    {
       gesture_get_tilt(data, &x, &y);
    }
  5. When gesture detection is no longer used, unset the callback functions with the gesture_stop_recognition() function and destroy the handle with the gesture_release() function:

    gesture_stop_recognition(handle);
    gesture_release(handle);
Go to top