Activity Recognition: Detecting Device Activities
This tutorial demonstrates how you can manage and monitor various system information and attached devices.
Warm-up
Become familiar with the Activity Recognition API basics by learning about:
-
Detecting Activities
Detect user activities.
Detecting Activities
To set and unset callback functions for the activity monitor and retrieve details from the received activity data:
-
To use the activity recognition-related features of the Activity Recognition API (in mobile and wearable applications), include the <activity_recognition.h> header file in your application:
#include <activity_recognition.h>
- Create a handle for activity monitoring using the activity_create() function:
activity_h handle; activity_create(&handle);
- To subscribe to notifications about specific activity state changes, invoke the activity_start_recognition() function to register a callback function.
When specific activity data is received, the example_activity_callback callback is invoked.
The following example starts the activity monitor to receive notifications when the ACTIVITY_WALK activity is detected. Any of the activity_type_e enumerators (in mobile and wearable applications) can be used in place of the ACTIVITY_WALK value.
activity_start_recognition(handle, ACTIVITY_WALK, example_activity_callback, NULL);
- When the example_activity_callback() callback is invoked, the current activity is delivered as a parameter, and you can extract the accuracy of the recognized activity:
void example_activity_callback(activity_type_e activity, const activity_data_h data, double timestamp, activity_error_e error, void *user_data) { int result; activity_accuracy_e accuracy; result = activity_get_accuracy(data, &accuracy); if (result != ACTIVITY_ERROR_NONE) { // Error handling } }
- When activity monitoring is no longer used, unset the callback function and destroy the handle:
activity_stop_recognition(handle); activity_release(handle);