oh_sensor.h
Overview
The oh_sensor.h file declares the APIs for operating sensors, including obtaining sensor information and subscribing to and unsubscribing from sensor data.
File to include: <sensors/oh_sensor.h>
Library: libohsensor.so
System capability: SystemCapability.Sensors.Sensor
Since: 11
Related module: Sensor
Summary
Functions
| Name | Description |
|---|---|
| Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count) | Obtains information about all sensors on the device. |
| Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber) | Subscribes to sensor data. The system will report sensor data to the subscriber at the specified frequency. To subscribe to data of acceleration sensors, request the ohos.permission.ACCELEROMETER permission. To subscribe to data of gyroscope sensors, request the ohos.permission.GYROSCOPE permission. To subscribe to data of pedometer-related sensors, request the ohos.permission.ACTIVITY_MOTION permission. To subscribe to data of health-related sensors, such as heart rate sensors, request the ohos.permission.READ_HEALTH_DATA permission. Otherwise, the subscription fails. You do not need to request any permission to subscribe to data of other types of sensors. |
| Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber) | Unsubscribes from sensor data. To unsubscribe from data of acceleration sensors, request the ohos.permission.ACCELEROMETER permission. To unsubscribe from data of gyroscope sensors, request the ohos.permission.GYROSCOPE permission. To unsubscribe from data of pedometer-related sensors, request the ohos.permission.ACTIVITY_MOTION permission. To unsubscribe from data of health-related sensors, request the ohos.permission.READ_HEALTH_DATA permission. Otherwise, the unsubscription fails. You do not need to request any permission to unsubscribe from data of other types of sensors. |
Function Description
OH_Sensor_GetInfos()
Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count)
Description
Obtains information about all sensors on the device.
Since: 11
Parameters
| Name | Description |
|---|---|
| Sensor_Info **infos | Double pointer to the information about all sensors on the device. For details, see Sensor_Info. |
| uint32_t *count | Pointer to the number of sensors on the device. |
Returns
| Type | Description |
|---|---|
| Sensor_Result | SENSOR_SUCCESS if the operation is successful; an error code defined in Sensor_Result otherwise. The error code can be: - SENSOR_PARAMETER_ERROR if the parameter verification fails. For example, the parameter is invalid or the parameter type is incorrect. - SENSOR_SERVICE_EXCEPTION if the sensor service is abnormal. |
OH_Sensor_Subscribe()
Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber)
Description
Subscribes to sensor data. The system will report sensor data to the subscriber at the specified frequency. To subscribe to data of acceleration sensors, request the ohos.permission.ACCELEROMETER permission. To subscribe to data of gyroscope sensors, request the ohos.permission.GYROSCOPE permission. To subscribe to data of pedometer-related sensors, request the ohos.permission.ACTIVITY_MOTION permission. To subscribe to data of health-related sensors, such as heart rate sensors, request the ohos.permission.READ_HEALTH_DATA permission. Otherwise, the subscription fails. You do not need to request any permission to subscribe to data of other types of sensors.
Required permissions: ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or ohos.permission.ACTIVITY_MOTION or ohos.permission.READ_HEALTH_DATA
Since: 11
Parameters
| Name | Description |
|---|---|
| const Sensor_SubscriptionId *id | Pointer to the sensor subscription ID. For details, see Sensor_SubscriptionId. |
| const Sensor_SubscriptionAttribute *attribute | Pointer to the subscription attribute, which is used to specify the data reporting frequency. For details, see Sensor_SubscriptionAttribute. |
| const Sensor_Subscriber *subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see Sensor_Subscriber. |
Returns
| Type | Description |
|---|---|
| Sensor_Result | SENSOR_SUCCESS if the operation is successful; an error code defined in Sensor_Result otherwise. The error code can be: - SENSOR_PERMISSION_DENIED if the permission verification fails. - SENSOR_PARAMETER_ERROR if the parameter verification fails. For example, the parameter is invalid or the parameter type is incorrect. - SENSOR_SERVICE_EXCEPTION if the sensor service is abnormal. |
OH_Sensor_Unsubscribe()
Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber)
Description
Unsubscribes from sensor data. To unsubscribe from data of acceleration sensors, request the ohos.permission.ACCELEROMETER permission. To unsubscribe from data of gyroscope sensors, request the ohos.permission.GYROSCOPE permission. To unsubscribe from data of pedometer-related sensors, request the ohos.permission.ACTIVITY_MOTION permission. To unsubscribe from data of health-related sensors, request the ohos.permission.READ_HEALTH_DATA permission. Otherwise, the unsubscription fails. You do not need to request any permission to unsubscribe from data of other types of sensors.
Required permissions: ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or ohos.permission.ACTIVITY_MOTION or ohos.permission.READ_HEALTH_DATA
Since: 11
Parameters
| Name | Description |
|---|---|
| const Sensor_SubscriptionId *id | Pointer to the sensor subscription ID. For details, see Sensor_SubscriptionId. |
| const Sensor_Subscriber *subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see Sensor_Subscriber. |
Returns
| Type | Description |
|---|---|
| Sensor_Result | SENSOR_SUCCESS if the operation is successful; an error code defined in Sensor_Result otherwise. The error code can be: - SENSOR_PERMISSION_DENIED if the permission verification fails. - SENSOR_PARAMETER_ERROR if the parameter verification fails. For example, the parameter is invalid or the parameter type is incorrect. - SENSOR_SERVICE_EXCEPTION if the sensor service is abnormal. |