openharmony 鸿蒙 capi-oh-sensor-h

2026-08-25 浏览 (1)

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

NameDescription
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

NameDescription
Sensor_Info **infosDouble pointer to the information about all sensors on the device. For details, see Sensor_Info.
uint32_t *countPointer to the number of sensors on the device.

Returns

TypeDescription
Sensor_ResultSENSOR_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

NameDescription
const Sensor_SubscriptionId *idPointer to the sensor subscription ID. For details, see Sensor_SubscriptionId.
const Sensor_SubscriptionAttribute *attributePointer to the subscription attribute, which is used to specify the data reporting frequency. For details, see Sensor_SubscriptionAttribute.
const Sensor_Subscriber *subscriberPointer to the subscriber information, which is used by the callback function to report sensor data. For details, see Sensor_Subscriber.

Returns

TypeDescription
Sensor_ResultSENSOR_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

NameDescription
const Sensor_SubscriptionId *idPointer to the sensor subscription ID. For details, see Sensor_SubscriptionId.
const Sensor_Subscriber *subscriberPointer to the subscriber information, which is used by the callback function to report sensor data. For details, see Sensor_Subscriber.

Returns

TypeDescription
Sensor_ResultSENSOR_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.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-vibrator

openharmony 鸿蒙 capi-sensor-sensor-subscriptionid

openharmony 鸿蒙 capi-sensor-sensor-subscriptionattribute

openharmony 鸿蒙 capi-vibrator-h

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 capi-vibrator-vibrator-filedescription

openharmony 鸿蒙 js-apis-sensor-sys

openharmony 鸿蒙 errorcode-sensor

openharmony 鸿蒙 capi-oh-sensor-type-h

openharmony 鸿蒙 js-apis-system-sensor

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/tH3eIEfq