Runtime information: Receiving and Monitoring Runtime Information
This tutorial demonstrates how you can obtain runtime information, such as system preferences.
Warm-up
Become familiar with the Runtime information API basics by learning about:
-
Getting Runtime Information
Get runtime information.
-
Monitoring Runtime Information Changes
Register a callback function for a specific change in the runtime information.
Getting Runtime Information
To get runtime information, which consists of key, value pairs:
-
To use the runtime information-related features of the Runtime information API (in mobile and wearable applications), include the <runtime_info.h> header file in your application:
#include <runtime_info.h>
- Check whether Bluetooth is enabled.
Use the RUNTIME_INFO_KEY_BLUETOOTH_ENABLED key to check whether Bluetooth is enabled. The type of the value is bool, and the runtime_info_get_value_bool() function is used for the information.
#include <stdbool.h> #include <runtime_info.h> void func(void) { bool value; int ret; ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, &value); if (ret != RUNTIME_INFO_ERROR_NONE) { // Error handling return; } dlog_print(DLOG_INFO, LOG_TAG, "Bluetooth: %s", value ? "Enabled" : "Disabled"); }
- Get the audio jack status.
Use the RUNTIME_INFO_KEY_AUDIO_JACK_STATUS key to get the audio jack status. The type of the value is integer, and the runtime_info_get_value_int() function is used for the information.
#include <runtime_info.h> void func(void) { int value; int ret; ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &value); if (ret != RUNTIME_INFO_ERROR_NONE) { // Error handling return; } dlog_print(DLOG_INFO, LOG_TAG, "Audio jack status: %d", value); switch (value) { case RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED: // Audio jack is not connected break; case RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE: // 3-conductor wire is connected break; case RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE: // 4-conductor wire is connected break; } }
Monitoring Runtime Information Changes
To monitor runtime information changes:
-
To use the runtime information-related features of the Runtime information API (in mobile and wearable applications), include the <runtime_info.h> header file in your application:
#include <runtime_info.h>
-
Applications can be notified about changes in the runtime information. The runtime_info_set_changed_cb() and runtime_info_unset_changed_cb() functions are used to register a callback function.
Use the RUNTIME_INFO_KEY_USB_CONNECTED key to monitor the connection state of the USB cable:
#include <stdbool.h> #include <runtime_info.h> void usb_connection_changed(runtime_info_key_e key, void *user_data) { bool value; int ret; if (key != RUNTIME_INFO_KEY_USB_CONNECTED) return; ret = runtime_info_get_value_bool(key, &value); if (ret != RUNTIME_INFO_ERROR_NONE) { // Error handling return; } dlog_print(DLOG_INFO, LOG_TAG, "USB status: %s", value ? "Connected" : "Disconnected"); } void func(void) { int ret; void *data; ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED, usb_connection_changed, data); if (ret != RUNTIME_INFO_ERROR_NONE) { // Error handling return; } ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED); if (ret != RUNTIME_INFO_ERROR_NONE) { // Error handling return; } }