1# Sensor 2 3 4## Overview 5 6The **Sensor** module provides APIs to manipulate sensors. For example, you can call APIs to obtain sensor information and subscribe to or unsubscribe from sensor data. 7 8It also provides APIs to define common sensor attributes. 9 10**Since**: 11 11 12 13## Summary 14 15 16### Files 17 18| Name| Description| 19| -------- | -------- | 20| [oh_sensor.h](oh_sensor_8h.md) | Declares the APIs for operating sensors, including obtaining sensor information and subscribing to and unsubscribing from sensor data. | 21| [oh_sensor_type.h](oh_sensor_type_8h.md) | Declares the common sensor attributes. | 22 23 24### Types 25 26| Name| Description| 27| -------- | -------- | 28| [Sensor_Type](#sensor_type) | Defines an enum that enumerates the sensor types. | 29| [Sensor_Result](#sensor_result) | Defines an enum that enumerates the sensor result codes. | 30| [Sensor_Accuracy](#sensor_accuracy) | Defines an enum that enumerates the accuracy levels of data reported by a sensor. | 31| [Sensor_Info](#sensor_info) | Defines the sensor information. | 32| [Sensor_Event](#sensor_event) | Defines the sensor data information. | 33| [Sensor_SubscriptionId](#sensor_subscriptionid) | Defines the sensor subscription ID, which uniquely identifies a sensor. | 34| [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) | Defines the sensor subscription attribute.| 35| void ([*Sensor_EventCallback](#sensor_eventcallback)) ([Sensor_Event](#sensor_event) \*event) | Defines the callback function used to report sensor data. | 36| [Sensor_Subscriber](#sensor_subscriber) | Defines the sensor subscriber information. | 37 38 39### Enums 40 41| Name| Description| 42| -------- | -------- | 43| [Sensor_Type](#sensor_type) {<br>SENSOR_TYPE_ACCELEROMETER = 1, <br>SENSOR_TYPE_GYROSCOPE = 2,<br> SENSOR_TYPE_AMBIENT_LIGHT = 5, <br>SENSOR_TYPE_MAGNETIC_FIELD = 6,<br>SENSOR_TYPE_BAROMETER = 8,<br> SENSOR_TYPE_HALL = 10, <br>SENSOR_TYPE_PROXIMITY = 12,<br> SENSOR_TYPE_ORIENTATION = 256,<br>SENSOR_TYPE_GRAVITY = 257, <br>SENSOR_TYPE_ROTATION_VECTOR = 259, ,<br>SENSOR_TYPE_PEDOMETER_DETECTION = 265,<br> SENSOR_TYPE_PEDOMETER = 266,<br>SENSOR_TYPE_HEART_RATE = 278<br>} | Enumerates the sensor types. | 44| [Sensor_Result](#sensor_result) { <br>SENSOR_SUCCESS = 0,<br> SENSOR_PERMISSION_DENIED = 201, <br>SENSOR_PARAMETER_ERROR = 401,<br> SENSOR_SERVICE_EXCEPTION = 14500101<br> } | Enumerates the sensor result codes. | 45| [Sensor_Accuracy](#sensor_accuracy) { <br>SENSOR_ACCURACY_UNRELIABLE = 0, <br>SENSOR_ACCURACY_LOW = 1,<br> SENSOR_ACCURACY_MEDIUM = 2, <br>SENSOR_ACCURACY_HIGH = 3 <br>} | Enumerates the accuracy levels of data reported by a sensor. | 46 47 48### Functions 49 50| Name| Description| 51| -------- | -------- | 52| [OH_Sensor_GetInfos](#oh_sensor_getinfos)([Sensor_Info](#sensor_info) \*\*infos, uint32_t *count) | Obtains information about all sensors on the device. | 53| [OH_Sensor_Subscribe](#oh_sensor_subscribe)(const [Sensor_SubscriptionId](#sensor_subscriptionid) \*id, const [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) \*attribute, const [Sensor_Subscriber](#sensor_subscriber) \*subscriber) | Subscribe to sensor data. The system will report sensor data to the subscriber at the specified frequency.<br>To subscribe to data of acceleration sensors, request the **ohos.permission.ACCELEROMETER** permission.<br>To subscribe to data of gyroscope sensors, request the **ohos.permission.GYROSCOPE** permission.<br>To subscribe to data of pedometer-related sensors, request the **ohos.permission.ACTIVITY_MOTION** permission.<br>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.<br>You do not need to request any permission to subscribe to data of other types of sensors.| 54| [OH_Sensor_Unsubscribe](#oh_sensor_unsubscribe)(const [Sensor_SubscriptionId](#sensor_subscriptionid) \*id, const [Sensor_Subscriber](#sensor_subscriber) \*subscriber) | Unsubscribes from sensor data.<br>To unsubscribe from data of acceleration sensors, request the **ohos.permission.ACCELEROMETER** permission.<br>To unsubscribe from data of gyroscope sensors, request the **ohos.permission.GYROSCOPE** permission.<br>To unsubscribe from data of pedometer-related sensors, request the **ohos.permission.ACTIVITY_MOTION** permission.<br>To unsubscribe from data of health-related sensors, request the **ohos.permission.READ_HEALTH_DATA** permission. Otherwise, the unsubscription fails.<br>You do not need to request any permission to unsubscribe from data of other types of sensors.| 55| [OH_Sensor_CreateInfos](#oh_sensor_createinfos)(uint32_t count) | Creates an array of [Sensor_Info](#sensor_info) instances with the given number.| 56| [OH_Sensor_DestroyInfos](#oh_sensor_destroyinfos)([Sensor_Info](#sensor_info) \*\*sensors, uint32_t count) | Destroys an array of [Sensor_Info](#sensor_info) instances and reclaims memory.| 57| [OH_SensorInfo_GetName](#oh_sensorinfo_getname)([Sensor_Info](#sensor_info) \*sensor, char \*sensorName, uint32_t \*length) | Obtains the sensor name. | 58| [OH_SensorInfo_GetVendorName](#oh_sensorinfo_getvendorname)([Sensor_Info](#sensor_info)* sensor, char \*vendorName, uint32_t \*length) | Obtains the sensor's vendor name. | 59| [OH_SensorInfo_GetType](#oh_sensorinfo_gettype)([Sensor_Info](#sensor_info)* sensor, [Sensor_Type](#sensor_type) \*sensorType) | Obtains the sensor type. | 60| [OH_SensorInfo_GetResolution](#oh_sensorinfo_getresolution)([Sensor_Info](#sensor_info)* sensor, float \*resolution) | Obtains the sensor resolution. | 61| [OH_SensorInfo_GetMinSamplingInterval](#oh_sensorinfo_getminsamplinginterval)([Sensor_Info](#sensor_info)* sensor, int64_t \*minSamplingInterval) | Obtains the minimum data reporting interval of a sensor. | 62| [OH_SensorInfo_GetMaxSamplingInterval](#oh_sensorinfo_getmaxsamplinginterval)([Sensor_Info](#sensor_info)* sensor, int64_t \*maxSamplingInterval) | Obtains the maximum data reporting interval of a sensor. | 63| [OH_SensorEvent_GetType](#oh_sensorevent_gettype)([Sensor_Event](#sensor_event)* sensorEvent, [Sensor_Type](#sensor_type) \*sensorType) | Obtains the sensor type. | 64| [OH_SensorEvent_GetTimestamp](#oh_sensorevent_gettimestamp)([Sensor_Event](#sensor_event)* sensorEvent, int64_t \*timestamp) | Obtains the timestamp of sensor data. | 65| [OH_SensorEvent_GetAccuracy](#oh_sensorevent_getaccuracy)([Sensor_Event](#sensor_event)* sensorEvent, Sensor_Accuracy *accuracy) | Obtains the accuracy of sensor data. | 66| [OH_SensorEvent_GetData](#oh_sensorevent_getdata)([Sensor_Event](#sensor_event)* sensorEvent, float \*\*data, uint32_t \*length) | Obtains sensor data.<br>The data length and content depend on the sensor type. The format of the sensor data reported is as follows:<br> - SENSOR_TYPE_ACCELEROMETER: data[0], data[1], and data[2], indicating the acceleration around the x, y, and z axes of a device, respectively, in m/s².<br>- SENSOR_TYPE_GYROSCOPE: data[0], data[1], and data[2], indicating the angular velocity of rotation around the x, y, and z axes of a device, respectively, in rad/s.<br><br>- SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux.<br> - SENSOR_TYPE_MAGNETIC_FIELD: data[0], data[1], and data[2], indicating the magnetic field strength around the x, y, and z axes of a device, respectively, in μT.<br>- SENSOR_TYPE_BAROMETER: data[0], indicating the atmospheric pressure, in hPa.<br>- SENSOR_TYPE_HALL: data[0], indicating the opening/closing state of the flip cover. The value **0** means that the flip cover is opened, and a value greater than 0 means that the flip cover is closed.<br>- SENSOR_TYPE_PROXIMITY: data[0], indicates the approaching state. The value **0** means the two objects are close to each other, and a value greater than 0 means that they are far away from each other.<br>- SENSOR_TYPE_ORIENTATION: data[0], data[1], and data[2], indicating the rotation angles of a device around the z, x, and y axes, respectively, in degree.<br>- SENSOR_TYPE_GRAVITY: data[0], data[1], and data[2], indicating the gravitational acceleration around the x, y, and z axes of a device, respectively, in m/s².<br>- SENSOR_TYPE_ROTATION_VECTOR: data[0], data[1] and data[2], indicating the rotation angles of a device around the x, y, and z axes, respectively, in degree. data[3] indicates the rotation vector.<br>- SENSOR_TYPE_PEDOMETER_DETECTION: data[0], indicating the pedometer detection status. The value **1** means that the number of detected steps changes.<br>- SENSOR_TYPE_PEDOMETER: data[0], indicating the number of steps a user has walked.<br>- SENSOR_TYPE_HEART_RATE: data[0], indicating the heart rate value.| 67| [OH_Sensor_CreateSubscriptionId](#oh_sensor_createsubscriptionid)(void) | Creates a [Sensor_SubscriptionId](#sensor_subscriptionid) instance.| 68| [OH_Sensor_DestroySubscriptionId](#oh_sensor_destroysubscriptionid)([Sensor_SubscriptionId](#sensor_subscriptionid) \*id) | Destroys a [Sensor_SubscriptionId](#sensor_subscriptionid) instance and reclaims memory.| 69| [OH_SensorSubscriptionId_GetType](#oh_sensorsubscriptionid_gettype)([Sensor_SubscriptionId](#sensor_subscriptionid) \*id, [Sensor_Type](#sensor_type) \*sensorType) | Obtains the sensor type. | 70| [OH_SensorSubscriptionId_SetType](#oh_sensorsubscriptionid_settype)([Sensor_SubscriptionId](#sensor_subscriptionid)* id, const [Sensor_Type](#sensor_type) sensorType) | Sets the sensor type. | 71| [OH_Sensor_CreateSubscriptionAttribute](#oh_sensor_createsubscriptionattribute)(void) | Creates a [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance.| 72| [OH_Sensor_DestroySubscriptionAttribute](#oh_sensor_destroysubscriptionattribute)([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) *attribute) | Destroys a [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance and reclaims memory.| 73| [OH_SensorSubscriptionAttribute_SetSamplingInterval](#oh_sensorsubscriptionattribute_setsamplinginterval)([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, const int64_t samplingInterval) | Sets the sensor data reporting interval. | 74| [OH_SensorSubscriptionAttribute_GetSamplingInterval](#oh_sensorsubscriptionattribute_getsamplinginterval) ([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, int64_t \*samplingInterval) | Obtains the sensor data reporting interval. | 75| [OH_Sensor_CreateSubscriber](#oh_sensor_createsubscriber)(void) | Creates a [Sensor_Subscriber](#sensor_subscriber) instance. | 76| [OH_Sensor_DestroySubscriber](#oh_sensor_destroysubscriber)([Sensor_Subscriber](#sensor_subscriber) \*subscriber) | Destroys a [Sensor_Subscriber](#sensor_subscriber) instance and reclaims memory.| 77| [OH_SensorSubscriber_SetCallback](#oh_sensorsubscriber_setcallback)([Sensor_Subscriber](#sensor_subscriber)* subscriber, const [Sensor_EventCallback](#sensor_eventcallback) callback) | Sets a callback function to report sensor data. | 78| [OH_SensorSubscriber_GetCallback](#oh_sensorsubscriber_getcallback)([Sensor_Subscriber](#sensor_subscriber)* subscriber, [Sensor_EventCallback](#sensor_eventcallback) \*callback) | Obtains the callback function used to report sensor data. | 79 80 81## Type Description 82 83 84### Sensor_Accuracy 85 86``` 87typedef enum Sensor_Accuracy Sensor_Accuracy 88``` 89**Description** 90 91Defines an enum that enumerates the accuracy levels of data reported by a sensor. 92 93**Since**: 11 94 95 96### Sensor_Event 97 98``` 99typedef struct Sensor_Event Sensor_Event 100``` 101**Description** 102 103Defines the sensor data information. 104 105**Since**: 11 106 107 108### Sensor_EventCallback 109 110``` 111typedef void(*Sensor_EventCallback)(Sensor_Event *event) 112``` 113**Description** 114 115Defines the callback function used to report sensor data. 116 117**Since**: 11 118 119 120### Sensor_Info 121 122``` 123typedef struct Sensor_Info Sensor_Info 124``` 125**Description** 126 127Defines the sensor information. 128 129**Since**: 11 130 131 132### Sensor_Result 133 134``` 135typedef enum Sensor_Result Sensor_Result 136``` 137**Description** 138 139Defines an enum that enumerates the sensor result codes. 140 141**Since**: 11 142 143 144### Sensor_Subscriber 145 146``` 147typedef struct Sensor_Subscriber Sensor_Subscriber 148``` 149**Description** 150 151Defines the sensor subscriber information. 152 153**Since**: 11 154 155 156### Sensor_SubscriptionAttribute 157 158``` 159typedef struct Sensor_SubscriptionAttribute Sensor_SubscriptionAttribute 160``` 161**Description** 162 163Defines the sensor subscription attribute. 164 165**Since**: 11 166 167 168### Sensor_SubscriptionId 169 170``` 171typedef struct Sensor_SubscriptionId Sensor_SubscriptionId 172``` 173**Description** 174 175Defines the sensor subscription ID, which uniquely identifies a sensor. 176 177**Since**: 11 178 179 180### Sensor_Type 181 182``` 183typedef enum Sensor_Type Sensor_Type 184``` 185**Description** 186 187Defines an enum that enumerates the sensor types. 188 189**Since**: 11 190 191 192## Enum Description 193 194 195### Sensor_Accuracy 196 197``` 198enum Sensor_Accuracy 199``` 200**Description** 201 202Enumerates the accuracy levels of data reported by a sensor. 203 204**Since**: 11 205 206| Value| Description| 207| -------- | -------- | 208| SENSOR_ACCURACY_UNRELIABLE | The sensor data is unreliable. It is possible that the sensor does not contact with the device to measure.| 209| SENSOR_ACCURACY_LOW | The sensor data is at a low accuracy level. The data must be calibrated based on the environment before being used.| 210| SENSOR_ACCURACY_MEDIUM | The sensor data is at a medium accuracy level. You are advised to calibrate the data based on the environment before using it.| 211| SENSOR_ACCURACY_HIGH | The sensor data is at a high accuracy level. The data can be used directly.| 212 213 214### Sensor_Result 215 216``` 217enum Sensor_Result 218``` 219**Description** 220 221Enumerates the sensor result codes. 222 223**Since**: 11 224 225| Value| Description| 226| -------- | -------- | 227| SENSOR_SUCCESS | The operation is successful.| 228| SENSOR_PERMISSION_DENIED | Permission verification failed.| 229| SENSOR_PARAMETER_ERROR | Parameter check failed. For example, a mandatory parameter is not passed in, or the parameter type passed in is incorrect.| 230| SENSOR_SERVICE_EXCEPTION | The sensor service is abnormal.| 231 232 233### Sensor_Type 234 235``` 236enum Sensor_Type 237``` 238**Description** 239 240Enumerates the sensor types. 241 242**Since**: 11 243 244| Value| Description| 245| -------- | -------- | 246| SENSOR_TYPE_ACCELEROMETER | Acceleration sensor.| 247| SENSOR_TYPE_GYROSCOPE | Gyroscope sensor.| 248| SENSOR_TYPE_AMBIENT_LIGHT | Ambient light sensor.| 249| SENSOR_TYPE_MAGNETIC_FIELD | Magnetic field sensor.| 250| SENSOR_TYPE_BAROMETER | Barometer sensor| 251| SENSOR_TYPE_HALL | Hall effect sensor.| 252| SENSOR_TYPE_PROXIMITY | Proximity sensor.| 253| SENSOR_TYPE_ORIENTATION | Orientation sensor.| 254| SENSOR_TYPE_GRAVITY | Gravity sensor.| 255| SENSOR_TYPE_ROTATION_VECTOR | Rotation vector sensor.| 256| SENSOR_TYPE_PEDOMETER_DETECTION | Pedometer detection sensor.| 257| SENSOR_TYPE_PEDOMETER | Pedometer sensor.| 258| SENSOR_TYPE_HEART_RATE | Heart rate sensor.| 259 260 261## Function Description 262 263 264### OH_Sensor_CreateInfos() 265 266``` 267Sensor_Info **OH_Sensor_CreateInfos(uint32_t count) 268``` 269**Description** 270 271Creates an array of [Sensor_Info](#sensor_info) instances with the given number. 272 273**Since**: 11 274 275**Parameters** 276 277| Name| Description| 278| -------- | -------- | 279| count | Number of [Sensor_Info](#sensor_info) instances to create.| 280 281**Returns** 282 283Returns the double pointer to the array of [Sensor_Info](#sensor_info) instances if the operation is successful; returns **NULL** otherwise. 284 285 286### OH_Sensor_CreateSubscriber() 287 288``` 289Sensor_Subscriber *OH_Sensor_CreateSubscriber(void) 290``` 291**Description** 292 293Creates a [Sensor_Subscriber](#sensor_subscriber) instance. 294 295**Since**: 11 296 297**Returns** 298 299Returns the pointer to the [Sensor_Subscriber](#sensor_subscriber) instances if the operation is successful; returns **NULL** otherwise. 300 301 302### OH_Sensor_CreateSubscriptionAttribute() 303 304``` 305Sensor_SubscriptionAttribute *OH_Sensor_CreateSubscriptionAttribute(void) 306``` 307**Description** 308 309Creates a [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance. 310 311**Since**: 11 312 313**Returns** 314 315Returns the pointer to the [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instances if the operation is successful; returns **NULL** otherwise. 316 317 318### OH_Sensor_CreateSubscriptionId() 319 320``` 321Sensor_SubscriptionId *OH_Sensor_CreateSubscriptionId(void) 322``` 323**Description** 324 325Creates a [Sensor_SubscriptionId](#sensor_subscriptionid) instance. 326 327**Since**: 11 328 329**Returns** 330 331Returns the pointer to the [Sensor_SubscriptionId](#sensor_subscriptionid) instances if the operation is successful; returns **NULL** otherwise. 332 333 334### OH_Sensor_DestroyInfos() 335 336``` 337int32_t OH_Sensor_DestroyInfos(Sensor_Info **sensors, uint32_t count) 338``` 339**Description** 340 341Destroys an array of [Sensor_Info](#sensor_info) instances and reclaims memory. 342 343**Since**: 11 344 345**Parameters** 346 347| Name| Description| 348| -------- | -------- | 349| sensors | Double pointer to the array of [Sensor_Info](#sensor_info) instances.| 350| count | Number of [Sensor_Info](#sensor_info) instances to destroy.| 351 352**Returns** 353 354Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 355 356 357### OH_Sensor_DestroySubscriber() 358 359``` 360int32_t OH_Sensor_DestroySubscriber(Sensor_Subscriber *subscriber) 361``` 362**Description** 363 364Destroys a [Sensor_Subscriber](#sensor_subscriber) instance and reclaims memory. 365 366**Since**: 11 367 368**Parameters** 369 370| Name| Description| 371| -------- | -------- | 372| subscriber | Pointer to the [Sensor_Subscriber](#sensor_subscriber) instance.| 373 374**Returns** 375 376Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 377 378 379### OH_Sensor_DestroySubscriptionAttribute() 380 381``` 382int32_t OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute *attribute) 383``` 384**Description** 385 386Destroys a [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance and reclaims memory. 387 388**Since**: 11 389 390**Parameters** 391 392| Name| Description| 393| -------- | -------- | 394| attribute | Pointer to the [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance.| 395 396**Returns** 397 398Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 399 400 401### OH_Sensor_DestroySubscriptionId() 402 403``` 404int32_t OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId *id) 405``` 406**Description** 407 408Destroys a [Sensor_SubscriptionId](#sensor_subscriptionid) instance and reclaims memory. 409 410**Since**: 11 411 412**Parameters** 413 414| Name| Description| 415| -------- | -------- | 416| id | Pointer to the [Sensor_SubscriptionId](#sensor_subscriptionid) instance.| 417 418**Returns** 419 420Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 421 422 423### OH_Sensor_GetInfos() 424 425``` 426Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count) 427``` 428**Description** 429 430Obtains information about all sensors on the device. 431 432**Since**: 11 433 434**Parameters** 435 436| Name| Description| 437| -------- | -------- | 438| infos | Double pointers to the information about all sensors on the device. For details, see [Sensor_Info](#sensor_info).| 439| count | Pointer to the number of sensors on the device. | 440 441**Returns** 442 443Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 444 445 446### OH_Sensor_Subscribe() 447 448``` 449Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber) 450``` 451**Description** 452 453Subscribe to sensor data. The system will report sensor data to the subscriber at the specified frequency. 454 455- To subscribe to data of acceleration sensors, request the **ohos.permission.ACCELEROMETER** permission. 456- To subscribe to data of gyroscope sensors, request the **ohos.permission.GYROSCOPE** permission. 457- To subscribe to data of pedometer-related sensors, request the **ohos.permission.ACTIVITY_MOTION** permission. 458- To subscribe to data of health-related sensors, such as heart rate sensors, request the **ohos.permission.READ_HEALTH_DATA** permission. 459 460Otherwise, the subscription fails. You do not need to request any permission to subscribe to data of other types of sensors. 461 462**Since**: 11 463 464**Parameters** 465 466| Name| Description| 467| -------- | -------- | 468| id | Pointer to the sensor subscription ID. For details, see [Sensor_SubscriptionId](#sensor_subscriptionid).| 469| attribute | Pointer to the subscription attribute, which is used to specify the data reporting frequency. For details, see [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute).| 470| subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see [Sensor_Subscriber](#sensor_subscriber).| 471 472**Returns** 473 474Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 475 476**Required Permissions** 477 478ohos.permission.ACCELEROMETER, ohos.permission.GYROSCOPE, ohos.permission.ACTIVITY_MOTION, or ohos.permission.READ_HEALTH_DATA 479 480 481### OH_Sensor_Unsubscribe() 482 483``` 484Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber) 485``` 486**Description** 487 488Unsubscribes from sensor data. 489 490- To unsubscribe from data of acceleration sensors, request the **ohos.permission.ACCELEROMETER** permission. 491 492- To unsubscribe from data of gyroscope sensors, request the **ohos.permission.GYROSCOPE** permission. 493 494- To unsubscribe from data of pedometer-related sensors, request the **ohos.permission.ACTIVITY_MOTION** permission. 495 496- To unsubscribe from data of health-related sensors, request the **ohos.permission.READ_HEALTH_DATA** permission. 497 498Otherwise, the unsubscription fails. You do not need to request any permission to unsubscribe from data of other types of sensors. 499 500**Since**: 11 501 502**Parameters** 503 504| Name| Description| 505| -------- | -------- | 506| id | Pointer to the sensor subscription ID. For details, see [Sensor_SubscriptionId](#sensor_subscriptionid).| 507| subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see [Sensor_Subscriber](#sensor_subscriber).| 508 509**Returns** 510 511Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 512 513**Required Permissions** 514 515ohos.permission.ACCELEROMETER, ohos.permission.GYROSCOPE, ohos.permission.ACTIVITY_MOTION, or ohos.permission.READ_HEALTH_DATA 516 517 518### OH_SensorEvent_GetAccuracy() 519 520``` 521int32_t OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy) 522``` 523**Description** 524 525Obtains the accuracy of sensor data. 526 527**Since**: 11 528 529**Parameters** 530 531| Name| Description| 532| -------- | -------- | 533| sensorEvent | Pointer to the sensor data information. | 534| accuracy | Pointer to the accuracy. | 535 536**Returns** 537 538Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 539 540 541### OH_SensorEvent_GetData() 542 543``` 544int32_t OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length) 545``` 546**Description** 547 548Obtains sensor data. The data length and content depend on the sensor type. 549 550The format of the sensor data reported is as follows: 551 552- SENSOR_TYPE_ACCELEROMETER: data[0], data[1], and data[2], indicating the acceleration around the x, y, and z axes of a device, respectively, in m/s². 553 554- SENSOR_TYPE_GYROSCOPE: data[0], data[1], and data[2], indicating the angular velocity of rotation around the x, y, and z axes of a device, respectively, in rad/s. 555 556- SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux. 557 558- SENSOR_TYPE_MAGNETIC_FIELD: data[0], data[1], and data[2], indicating the magnetic field strength around the x, y, and z axes of a device, respectively, in μT. 559 560- SENSOR_TYPE_BAROMETER: data[0], indicating the atmospheric pressure, in hPa. 561 562- SENSOR_TYPE_HALL: data[0], indicating the opening/closing state of the flip cover. The value **0** means that the flip cover is opened, and a value greater than 0 means that the flip cover is closed. 563 564- SENSOR_TYPE_PROXIMITY: data[0], indicates the approaching state. The value **0** means the two objects are close to each other, and a value greater than 0 means that they are far away from each other. 565 566- SENSOR_TYPE_ORIENTATION: data[0], data[1], and data[2], indicating the rotation angles of a device around the z, x, and y axes, respectively, in degree. 567 568- SENSOR_TYPE_GRAVITY: data[0], data[1], and data[2], indicating the gravitational acceleration around the x, y, and z axes of a device, respectively, in m/s². 569 570- SENSOR_TYPE_ROTATION_VECTOR: data[0], data[1] and data[2], indicating the rotation angles of a device around the x, y, and z axes, respectively, in degree. data[3] indicates the rotation vector. 571 572- SENSOR_TYPE_PEDOMETER_DETECTION: data[0], indicating the pedometer detection status. The value **1** means that the number of detected steps changes. 573 574- SENSOR_TYPE_PEDOMETER: data[0], indicating the number of steps a user has walked. 575 576- SENSOR_TYPE_HEART_RATE: data[0], indicating the heart rate value. 577 578**Since**: 11 579 580**Parameters** 581 582| Name| Description| 583| -------- | -------- | 584| sensorEvent | Pointer to the sensor data information. | 585| data | Double pointer to the sensor data. | 586| length | Pointer to the array length. | 587 588**Returns** 589 590Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 591 592 593### OH_SensorEvent_GetTimestamp() 594 595``` 596int32_t OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp) 597``` 598**Description** 599 600Obtains the timestamp of sensor data. 601 602**Since**: 11 603 604**Parameters** 605 606| Name| Description| 607| -------- | -------- | 608| sensorEvent | Pointer to the sensor data information. | 609| timestamp | Pointer to the timestamp. | 610 611**Returns** 612 613Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 614 615 616### OH_SensorEvent_GetType() 617 618``` 619int32_t OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType) 620``` 621**Description** 622 623Obtains the sensor type. 624 625**Since**: 11 626 627**Parameters** 628 629| Name| Description| 630| -------- | -------- | 631| sensorEvent | Pointer to the sensor data information. | 632| sensorType | Pointer to the sensor type. | 633 634**Returns** 635 636Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 637 638 639### OH_SensorInfo_GetMaxSamplingInterval() 640 641``` 642int32_t OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval) 643``` 644**Description** 645 646Obtains the maximum data reporting interval of a sensor. 647 648**Since**: 11 649 650**Parameters** 651 652| Name| Description| 653| -------- | -------- | 654| sensor | Pointer to the sensor information. | 655| maxSamplingInterval | Pointer to the maximum data reporting interval, in nanoseconds. | 656 657**Returns** 658 659Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 660 661 662### OH_SensorInfo_GetMinSamplingInterval() 663 664``` 665int32_t OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval) 666``` 667**Description** 668 669Obtains the minimum data reporting interval of a sensor. 670 671**Since**: 11 672 673**Parameters** 674 675| Name| Description| 676| -------- | -------- | 677| sensor | Pointer to the sensor information. | 678| minSamplingInterval | Pointer to the minimum data reporting interval, in nanoseconds. | 679 680**Returns** 681 682Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 683 684 685### OH_SensorInfo_GetName() 686 687``` 688int32_t OH_SensorInfo_GetName (Sensor_Info* sensor, char *sensorName, uint32_t *length) 689``` 690**Description** 691 692Obtains the sensor name. 693 694**Since**: 11 695 696**Parameters** 697 698| Name| Description| 699| -------- | -------- | 700| sensor | Pointer to the sensor information. | 701| sensorName | Pointer to the sensor data. | 702| length | Pointer to the length, in bytes. | 703 704**Returns** 705 706Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 707 708 709### OH_SensorInfo_GetResolution() 710 711``` 712int32_t OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution) 713``` 714**Description** 715 716Obtains the sensor resolution. 717 718**Since**: 11 719 720**Parameters** 721 722| Name| Description| 723| -------- | -------- | 724| sensor | Pointer to the sensor information. | 725| resolution | Pointer to the sensor resolution. | 726 727**Returns** 728 729Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 730 731 732### OH_SensorInfo_GetType() 733 734``` 735int32_t OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType) 736``` 737**Description** 738 739Obtains the sensor type. 740 741**Since**: 11 742 743**Parameters** 744 745| Name| Description| 746| -------- | -------- | 747| sensor | Pointer to the sensor information. | 748| sensorType | Pointer to the sensor type. | 749 750**Returns** 751 752Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 753 754 755### OH_SensorInfo_GetVendorName() 756 757``` 758int32_t OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length) 759``` 760**Description** 761 762Obtains the sensor's vendor name. 763 764**Since**: 11 765 766**Parameters** 767 768| Name| Description| 769| -------- | -------- | 770| sensor | Pointer to the sensor information. | 771| vendorName | Pointer to the vendor name. | 772| length | Pointer to the length, in bytes. | 773 774**Returns** 775 776Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 777 778 779### OH_SensorSubscriber_GetCallback() 780 781``` 782int32_t OH_SensorSubscriber_GetCallback(Sensor_Subscriber* subscriber, Sensor_EventCallback *callback) 783``` 784**Description** 785 786Obtains the callback function used to report sensor data. 787 788**Since**: 11 789 790**Parameters** 791 792| Name| Description| 793| -------- | -------- | 794| subscriber | Pointer to the sensor subscriber information. | 795| callback | Pointer to the callback function. | 796 797**Returns** 798 799Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 800 801 802### OH_SensorSubscriber_SetCallback() 803 804``` 805int32_t OH_SensorSubscriber_SetCallback (Sensor_Subscriber* subscriber, const Sensor_EventCallback callback ) 806``` 807**Description** 808 809Sets a callback function to report sensor data. 810 811**Since**: 11 812 813**Parameters** 814 815| Name| Description| 816| -------- | -------- | 817| subscriber | Pointer to the sensor subscriber information. | 818| callback | Callback function to set. | 819 820**Returns** 821 822Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 823 824 825### OH_SensorSubscriptionAttribute_GetSamplingInterval() 826 827``` 828int32_t OH_SensorSubscriptionAttribute_GetSamplingInterval (Sensor_SubscriptionAttribute * attribute, int64_t * samplingInterval ) 829``` 830**Description** 831 832Obtains the sensor data reporting interval. 833 834**Since**: 11 835 836**Parameters** 837 838| Name| Description| 839| -------- | -------- | 840| attribute | Pointer to the sensor subscription attribute. | 841| samplingInterval | Pointer to the data reporting interval, in nanoseconds. | 842 843**Returns** 844 845Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 846 847 848### OH_SensorSubscriptionAttribute_SetSamplingInterval() 849 850``` 851int32_t OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, const int64_t samplingInterval) 852``` 853**Description** 854 855Sets the sensor data reporting interval. 856 857**Since**: 11 858 859**Parameters** 860 861| Name| Description| 862| -------- | -------- | 863| attribute | Pointer to the sensor subscription attribute. | 864| samplingInterval | Data reporting interval to set, in nanoseconds. | 865 866**Returns** 867 868Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 869 870 871### OH_SensorSubscriptionId_GetType() 872 873``` 874int32_t OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType) 875``` 876**Description** 877 878Obtains the sensor type. 879 880**Since**: 11 881 882**Parameters** 883 884| Name| Description| 885| -------- | -------- | 886| id | Pointer to the sensor subscription ID. | 887| sensorType | Pointer to the sensor type. | 888 889**Returns** 890 891Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 892 893 894### OH_SensorSubscriptionId_SetType() 895 896``` 897int32_t OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType) 898``` 899**Description** 900 901Sets the sensor type. 902 903**Since**: 11 904 905**Parameters** 906 907| Name| Description| 908| -------- | -------- | 909| id | Pointer to the sensor subscription ID. | 910| sensorType | Sensor type to set. | 911 912**Returns** 913 914Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 915