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) | Enumerates the sensor types. | 29| [Sensor_Result](#sensor_result) | Enumerates the sensor result codes. | 30| [Sensor_Accuracy](#sensor_accuracy) | Defines an enum for accuracy levels of data reported by a sensor.| 31| [Sensor_Info](#sensor_info) | Defines a struct for the sensor information. | 32| [Sensor_Event](#sensor_event) | Defines a struct for the sensor data information. | 33| [Sensor_SubscriptionId](#sensor_subscriptionid) | Defines a struct for the sensor subscription ID, which uniquely identifies a sensor. | 34| [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) | Defines a struct for 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 a struct for 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_LINEAR_ACCELERATION = 258,<br>SENSOR_TYPE_ROTATION_VECTOR = 259,<br>SENSOR_TYPE_GAME_ROTATION_VECTOR = 262,<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>} | Defines an enum for 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>- SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux. Since API version 12, two extra data records are returned, where **data[1]** indicates the color temperature (in kelvin), and **data[2]** indicates the infrared luminance (in cd/m²).<br>4. - 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.<br>- SENSOR_TYPE_LINEAR_ACCELERATION: data[0], data[1], and data[2], indicating the acceleration around the x, y, and z axes of a device, respectively, in m/s². This parameter is supported since API version 13.<br>- SENSOR_TYPE_GAME_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], indicating the rotation vector. This parameter is supported since API version 13.| 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 interval for reporting sensor data. | 74| [OH_SensorSubscriptionAttribute_GetSamplingInterval](#oh_sensorsubscriptionattribute_getsamplinginterval) ([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, int64_t *samplingInterval) | Obtains the interval for reporting sensor data. | 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 for 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 a struct for 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 a struct for 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 for 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 a struct for 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 a struct for 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 a struct for 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 for 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 202Defines an enum for 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 221Defines an enum for 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 240Defines an enum for 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**Example** 302 303For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 304 305 ```c 306 #include "sensors/oh_sensor.h" 307 #include "napi/native_api.h" 308 #include "hilog/log.h" 309 310 const int SENSOR_LOG_DOMAIN = 0xD002700; 311 const char *TAG = "[Sensor]"; 312 313 static napi_value CreateSubscriber(napi_env env, napi_callback_info info) { 314 Sensor_Result ret; 315 // Create a Sensor_Subscriber instance. 316 Sensor_Subscriber *subscriberTemp = OH_Sensor_CreateSubscriber(); 317 if (subscriberTemp == nullptr) { 318 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_CreateSubscriber failed"); 319 ret = SENSOR_SERVICE_EXCEPTION; 320 } else { 321 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_CreateSubscriber successful"); 322 ret = SENSOR_SUCCESS; 323 } 324 // Destroy the Sensor_Subscriber instance when it is no longer needed. 325 if (subscriberTemp != nullptr) { 326 OH_Sensor_DestroySubscriber(subscriberTemp); 327 } 328 napi_value result; 329 napi_create_int32(env, ret, &result); 330 return result; 331 } 332 ``` 333 334### OH_Sensor_CreateSubscriptionAttribute() 335 336``` 337Sensor_SubscriptionAttribute *OH_Sensor_CreateSubscriptionAttribute(void) 338``` 339**Description** 340 341Creates a [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance. 342 343**Since**: 11 344 345**Returns** 346 347Returns the pointer to the [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instances if the operation is successful; returns **NULL** otherwise. 348 349**Example** 350 351For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 352 353 ```c 354 #include "sensors/oh_sensor.h" 355 #include "napi/native_api.h" 356 #include "hilog/log.h" 357 358 const int SENSOR_LOG_DOMAIN = 0xD002700; 359 const char *TAG = "[Sensor]"; 360 361 static napi_value CreateSubscriptionAttribute(napi_env env, napi_callback_info info) { 362 Sensor_Result ret; 363 // Create a Sensor_SubscriptionAttribute instance. 364 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); 365 if (attr == nullptr) { 366 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_CreateSubscriptionAttribute failed"); 367 ret = SENSOR_SERVICE_EXCEPTION; 368 } else { 369 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_CreateSubscriptionAttribute successful"); 370 ret = SENSOR_SUCCESS; 371 } 372 // Destroy the Sensor_SubscriptionAttribute instance when it is no longer needed. 373 if (attr != nullptr) { 374 OH_Sensor_DestroySubscriptionAttribute(attr); 375 } 376 napi_value result; 377 napi_create_int32(env, ret, &result); 378 return result; 379 } 380 ``` 381 382### OH_Sensor_CreateSubscriptionId() 383 384``` 385Sensor_SubscriptionId *OH_Sensor_CreateSubscriptionId(void) 386``` 387**Description** 388 389Creates a [Sensor_SubscriptionId](#sensor_subscriptionid) instance. 390 391**Since**: 11 392 393**Returns** 394 395Returns the pointer to the [Sensor_SubscriptionId](#sensor_subscriptionid) instances if the operation is successful; returns **NULL** otherwise. 396 397**Example** 398 399For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 400 401 ```c 402 #include "sensors/oh_sensor.h" 403 #include "napi/native_api.h" 404 #include "hilog/log.h" 405 406 const int SENSOR_LOG_DOMAIN = 0xD002700; 407 const char *TAG = "[Sensor]"; 408 409 static napi_value CreateSubscriptionId(napi_env env, napi_callback_info info) { 410 Sensor_Result ret; 411 // Create a Sensor_SubscriptionId instance. 412 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); 413 if (id == nullptr) { 414 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_CreateSubscriptionId failed"); 415 ret = SENSOR_SERVICE_EXCEPTION; 416 } else { 417 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_CreateSubscriptionId successful"); 418 ret = SENSOR_SUCCESS; 419 } 420 // Destroy the Sensor_SubscriptionId instance when it is no longer needed. 421 if (id != nullptr) { 422 OH_Sensor_DestroySubscriptionId(id); 423 } 424 napi_value result; 425 napi_create_int32(env, ret, &result); 426 return result; 427 } 428 ``` 429 430### OH_Sensor_DestroyInfos() 431 432``` 433int32_t OH_Sensor_DestroyInfos(Sensor_Info **sensors, uint32_t count) 434``` 435**Description** 436 437Destroys an array of [Sensor_Info](#sensor_info) instances and reclaims memory. 438 439**Since**: 11 440 441**Parameters** 442 443| Name| Description| 444| -------- | -------- | 445| sensors | Double pointer to the array of [Sensor_Info](#sensor_info) instances.| 446| count | Number of [Sensor_Info](#sensor_info) instances to destroy.| 447 448**Returns** 449 450Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 451 452 453### OH_Sensor_DestroySubscriber() 454 455``` 456int32_t OH_Sensor_DestroySubscriber(Sensor_Subscriber *subscriber) 457``` 458**Description** 459 460Destroys a [Sensor_Subscriber](#sensor_subscriber) instance and reclaims memory. 461 462**Since**: 11 463 464**Parameters** 465 466| Name| Description| 467| -------- | -------- | 468| subscriber | Pointer to the [Sensor_Subscriber](#sensor_subscriber) instance.| 469 470**Returns** 471 472Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 473 474**Example** 475 476For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 477 478 ```c 479 #include "sensors/oh_sensor.h" 480 #include "napi/native_api.h" 481 #include "hilog/log.h" 482 483 const int SENSOR_LOG_DOMAIN = 0xD002700; 484 const char *TAG = "[Sensor]"; 485 486 static napi_value DestroySubscriber(napi_env env, napi_callback_info info) { 487 // Create a Sensor_Subscriber instance. 488 Sensor_Subscriber *subscriberTemp = OH_Sensor_CreateSubscriber(); 489 // Destroy the Sensor_Subscriber instance when it is no longer needed. 490 int32_t ret = OH_Sensor_DestroySubscriber(subscriberTemp); 491 if (ret != SENSOR_SUCCESS) { 492 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_DestroySubscriber failed"); 493 } else { 494 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_DestroySubscriber successful"); 495 } 496 napi_value result; 497 napi_create_int32(env, ret, &result); 498 return result; 499 } 500 ``` 501 502### OH_Sensor_DestroySubscriptionAttribute() 503 504``` 505int32_t OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute *attribute) 506``` 507**Description** 508 509Destroys a [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance and reclaims memory. 510 511**Since**: 11 512 513**Parameters** 514 515| Name| Description| 516| -------- | -------- | 517| attribute | Pointer to the [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) instance.| 518 519**Returns** 520 521Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 522 523**Example** 524 525For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 526 527 ```c 528 #include "sensors/oh_sensor.h" 529 #include "napi/native_api.h" 530 #include "hilog/log.h" 531 532 const int SENSOR_LOG_DOMAIN = 0xD002700; 533 const char *TAG = "[Sensor]"; 534 535 static napi_value DestroySubscriptionAttribute(napi_env env, napi_callback_info info) { 536 // Create a Sensor_SubscriptionAttribute instance. 537 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); 538 // Destroy the Sensor_SubscriptionAttribute instance when it is no longer needed. 539 int32_t ret = OH_Sensor_DestroySubscriptionAttribute(attr); 540 if (ret != SENSOR_SUCCESS) { 541 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_DestroySubscriptionAttribute failed"); 542 } else { 543 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_DestroySubscriptionAttribute successful"); 544 } 545 napi_value result; 546 napi_create_int32(env, ret, &result); 547 return result; 548 } 549 ``` 550 551### OH_Sensor_DestroySubscriptionId() 552 553``` 554int32_t OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId *id) 555``` 556**Description** 557 558Destroys a [Sensor_SubscriptionId](#sensor_subscriptionid) instance and reclaims memory. 559 560**Since**: 11 561 562**Parameters** 563 564| Name| Description| 565| -------- | -------- | 566| id | Pointer to the [Sensor_SubscriptionId](#sensor_subscriptionid) instance.| 567 568**Returns** 569 570Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 571 572**Example** 573 574For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 575 576 ```c 577 #include "sensors/oh_sensor.h" 578 #include "napi/native_api.h" 579 #include "hilog/log.h" 580 581 const int SENSOR_LOG_DOMAIN = 0xD002700; 582 const char *TAG = "[Sensor]"; 583 584 static napi_value DestroySubscriptionId(napi_env env, napi_callback_info info) { 585 // Create a Sensor_SubscriptionId instance. 586 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); 587 // Destroy the Sensor_SubscriptionId instance when it is no longer needed. 588 int32_t ret = OH_Sensor_DestroySubscriptionId(id); 589 if (ret != SENSOR_SUCCESS) { 590 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_DestroySubscriptionId failed"); 591 } else { 592 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_DestroySubscriptionId successful"); 593 } 594 napi_value result; 595 napi_create_int32(env, ret, &result); 596 return result; 597 } 598 ``` 599 600### OH_Sensor_GetInfos() 601 602``` 603Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count) 604``` 605**Description** 606 607Obtains information about all sensors on the device. 608 609**Since**: 11 610 611**Parameters** 612 613| Name| Description| 614| -------- | -------- | 615| infos | Double pointers to the information about all sensors on the device. For details, see [Sensor_Info](#sensor_info).| 616| count | Pointer to the number of sensors on the device. | 617 618**Returns** 619 620Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 621 622 623### OH_Sensor_Subscribe() 624 625``` 626Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber) 627``` 628**Description** 629 630Subscribe 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. 631 632**Since**: 11 633 634**Parameters** 635 636| Name| Description| 637| -------- | -------- | 638| id | Pointer to the sensor subscription ID. For details, see [Sensor_SubscriptionId](#sensor_subscriptionid).| 639| attribute | Pointer to the subscription attribute, which is used to specify the data reporting frequency. For details, see [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute).| 640| subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see [Sensor_Subscriber](#sensor_subscriber).| 641 642**Returns** 643 644Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 645 646**Required Permissions** 647 648ohos.permission.ACCELEROMETER, ohos.permission.GYROSCOPE, ohos.permission.ACTIVITY_MOTION, or ohos.permission.READ_HEALTH_DATA 649 650**Example** 651 652For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 653 654 ```c 655 #include "sensors/oh_sensor.h" 656 #include "napi/native_api.h" 657 #include "hilog/log.h" 658 #include <thread> 659 660 const int SENSOR_LOG_DOMAIN = 0xD002700; 661 const char *TAG = "[Sensor]"; 662 constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_ACCELEROMETER }; 663 constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64; 664 constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000; 665 constexpr int32_t SLEEP_TIME_MS = 1000; 666 constexpr int64_t INVALID_VALUE = -1; 667 constexpr float INVALID_RESOLUTION = -1.0F; 668 Sensor_Subscriber *g_user = nullptr; 669 670 // Define the callback. 671 void SensorDataCallbackImpl(Sensor_Event *event) { 672 if (event == nullptr) { 673 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "event is null"); 674 return; 675 } 676 int64_t timestamp = INVALID_VALUE; 677 // Obtain the timestamp of sensor data. 678 int32_t ret = OH_SensorEvent_GetTimestamp(event, ×tamp); 679 if (ret != SENSOR_SUCCESS) { 680 return; 681 } 682 Sensor_Type sensorType; 683 // Obtain the sensor type. 684 ret = OH_SensorEvent_GetType(event, &sensorType); 685 if (ret != SENSOR_SUCCESS) { 686 return; 687 } 688 Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE; 689 // Obtain the accuracy of sensor data. 690 ret = OH_SensorEvent_GetAccuracy(event, &accuracy); 691 if (ret != SENSOR_SUCCESS) { 692 return; 693 } 694 float *data = nullptr; 695 uint32_t length = 0; 696 // Obtain sensor data. 697 ret = OH_SensorEvent_GetData(event, &data, &length); 698 if (ret != SENSOR_SUCCESS) { 699 return; 700 } 701 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy); 702 for (uint32_t i = 0; i < length; ++i) { 703 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "accData[%{public}d]:%{public}f", i, data[i]); 704 } 705 } 706 707 static napi_value Subscribe(napi_env env, napi_callback_info info) { 708 // Create a Sensor_Subscriber instance. 709 g_user = OH_Sensor_CreateSubscriber(); 710 // Set the callback used to return sensor data. 711 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); 712 if (ret != SENSOR_SUCCESS) { 713 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriber_SetCallback failed"); 714 return nullptr; 715 } 716 // Create a Sensor_SubscriptionId instance. 717 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); 718 // Set the sensor type. For example, if you use SENSOR_TYPE_ACCELEROMETER, you need to request the ohos.permission.ACCELEROMETER permission. 719 // Configure the required permission as instructed in step 2 in the Sensor Development. 720 ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID); 721 if (ret != SENSOR_SUCCESS) { 722 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionId_SetType failed"); 723 return nullptr; 724 } 725 // Create a **Sensor_SubscriptionAttribute** instance. 726 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); 727 // Set the sensor data reporting interval. 728 ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); 729 if (ret != SENSOR_SUCCESS) { 730 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionAttribute_SetSamplingInterval failed"); 731 return nullptr; 732 } 733 // Subscribe to sensor data. 734 ret = OH_Sensor_Subscribe(id, attr, g_user); 735 if (ret != SENSOR_SUCCESS) { 736 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Subscribe failed"); 737 return nullptr; 738 } 739 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Subscribe successful"); 740 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); 741 // Unsubscribe from sensor data. 742 ret = OH_Sensor_Unsubscribe(id, g_user); 743 if (ret != SENSOR_SUCCESS) { 744 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Unsubscribe failed"); 745 return nullptr; 746 } 747 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Unsubscribe successful"); 748 if (id != nullptr) { 749 // Destroy the Sensor_SubscriptionId instance. 750 OH_Sensor_DestroySubscriptionId(id); 751 } 752 if (attr != nullptr) { 753 // Destroy the Sensor_SubscriptionAttribute instance. 754 OH_Sensor_DestroySubscriptionAttribute(attr); 755 } 756 if (g_user != nullptr) { 757 // Destroy the Sensor_Subscriber instance and reclaim memory. 758 OH_Sensor_DestroySubscriber(g_user); 759 g_user = nullptr; 760 } 761 return nullptr; 762 } 763 ``` 764 765### OH_Sensor_Unsubscribe() 766 767``` 768Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber) 769``` 770**Description** 771 772Unsubscribes 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. 773 774**Since**: 11 775 776**Parameters** 777 778| Name| Description| 779| -------- | -------- | 780| id | Pointer to the sensor subscription ID. For details, see [Sensor_SubscriptionId](#sensor_subscriptionid).| 781| subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see [Sensor_Subscriber](#sensor_subscriber).| 782 783**Returns** 784 785Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 786 787**Required Permissions** 788 789ohos.permission.ACCELEROMETER, ohos.permission.GYROSCOPE, ohos.permission.ACTIVITY_MOTION, or ohos.permission.READ_HEALTH_DATA 790 791**Example** 792 793For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 794 795 ```c 796 #include "sensors/oh_sensor.h" 797 #include "napi/native_api.h" 798 #include "hilog/log.h" 799 #include <thread> 800 801 const int SENSOR_LOG_DOMAIN = 0xD002700; 802 const char *TAG = "[Sensor]"; 803 constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_ACCELEROMETER }; 804 constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64; 805 constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000; 806 constexpr int32_t SLEEP_TIME_MS = 1000; 807 constexpr int64_t INVALID_VALUE = -1; 808 constexpr float INVALID_RESOLUTION = -1.0F; 809 Sensor_Subscriber *g_user = nullptr; 810 811 // Define the callback. 812 void SensorDataCallbackImpl(Sensor_Event *event) { 813 if (event == nullptr) { 814 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "event is null"); 815 return; 816 } 817 int64_t timestamp = INVALID_VALUE; 818 // Obtain the timestamp of sensor data. 819 int32_t ret = OH_SensorEvent_GetTimestamp(event, ×tamp); 820 if (ret != SENSOR_SUCCESS) { 821 return; 822 } 823 Sensor_Type sensorType; 824 // Obtain the sensor type. 825 ret = OH_SensorEvent_GetType(event, &sensorType); 826 if (ret != SENSOR_SUCCESS) { 827 return; 828 } 829 Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE; 830 // Obtain the accuracy of sensor data. 831 ret = OH_SensorEvent_GetAccuracy(event, &accuracy); 832 if (ret != SENSOR_SUCCESS) { 833 return; 834 } 835 float *data = nullptr; 836 uint32_t length = 0; 837 // Obtain sensor data. 838 ret = OH_SensorEvent_GetData(event, &data, &length); 839 if (ret != SENSOR_SUCCESS) { 840 return; 841 } 842 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy); 843 for (uint32_t i = 0; i < length; ++i) { 844 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "accData[%{public}d]:%{public}f", i, data[i]); 845 } 846 } 847 848 static napi_value Unsubscribe(napi_env env, napi_callback_info info) { 849 // Create a Sensor_Subscriber instance. 850 g_user = OH_Sensor_CreateSubscriber(); 851 // Set the callback used to return sensor data. 852 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); 853 if (ret != SENSOR_SUCCESS) { 854 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriber_SetCallback failed"); 855 return nullptr; 856 } 857 // Create a Sensor_SubscriptionId instance. 858 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); 859 // Set the sensor type. For example, if you use SENSOR_TYPE_ACCELEROMETER, you need to request the ohos.permission.ACCELEROMETER permission. 860 // Configure the required permission as instructed in step 2 in the Sensor Development. 861 ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID); 862 if (ret != SENSOR_SUCCESS) { 863 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionId_SetType failed"); 864 return nullptr; 865 } 866 // Create a **Sensor_SubscriptionAttribute** instance. 867 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); 868 // Set the sensor data reporting interval. 869 ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); 870 if (ret != SENSOR_SUCCESS) { 871 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionAttribute_SetSamplingInterval failed"); 872 return nullptr; 873 } 874 // Subscribe to sensor data. 875 ret = OH_Sensor_Subscribe(id, attr, g_user); 876 if (ret != SENSOR_SUCCESS) { 877 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Subscribe failed"); 878 return nullptr; 879 } 880 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Subscribe successful"); 881 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); 882 // Unsubscribe from sensor data. 883 ret = OH_Sensor_Unsubscribe(id, g_user); 884 if (ret != SENSOR_SUCCESS) { 885 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Unsubscribe failed"); 886 return nullptr; 887 } 888 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Unsubscribe successful"); 889 if (id != nullptr) { 890 // Destroy the Sensor_SubscriptionId instance. 891 OH_Sensor_DestroySubscriptionId(id); 892 } 893 if (attr != nullptr) { 894 // Destroy the Sensor_SubscriptionAttribute instance. 895 OH_Sensor_DestroySubscriptionAttribute(attr); 896 } 897 if (g_user != nullptr) { 898 // Destroy the Sensor_Subscriber instance and reclaim memory. 899 OH_Sensor_DestroySubscriber(g_user); 900 g_user = nullptr; 901 } 902 return nullptr; 903 } 904 ``` 905 906### OH_SensorEvent_GetAccuracy() 907 908``` 909int32_t OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy) 910``` 911**Description** 912 913Obtains the accuracy of sensor data. 914 915**Since**: 11 916 917**Parameters** 918 919| Name| Description| 920| -------- | -------- | 921| sensorEvent | Pointer to the sensor data information. | 922| accuracy | Pointer to the accuracy. | 923 924**Returns** 925 926Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 927 928 929### OH_SensorEvent_GetData() 930 931``` 932int32_t OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length) 933``` 934**Description** 935 936Obtains sensor data. The data length and content depend on the sensor type. The format of the sensor data reported is as follows: 937 938- 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². 939 940- 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. 941 942- SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux. 943 944- 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. 945 946- SENSOR_TYPE_BAROMETER: data[0], indicating the atmospheric pressure, in hPa. 947 948- 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. 949 950- 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. 951 952- 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. 953 954- 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². 955 956- 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. 957 958- SENSOR_TYPE_PEDOMETER_DETECTION: data[0], indicating the pedometer detection status. The value **1** means that the number of detected steps changes. 959 960- SENSOR_TYPE_PEDOMETER: data[0], indicating the number of steps a user has walked. 961 962- SENSOR_TYPE_HEART_RATE: data[0], indicating the heart rate value. 963 964- SENSOR_TYPE_LINEAR_ACCELERATION: data[0], data[1], and data[2], indicating the acceleration around the x, y, and z axes of a device, respectively, in m/s². This parameter is supported since API version 13. 965 966- SENSOR_TYPE_GAME_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], indicating the rotation vector. This parameter is supported since API version 13. 967 968**Since**: 11 969 970**Parameters** 971 972| Name| Description| 973| -------- | -------- | 974| sensorEvent | Pointer to the sensor data information. | 975| data | Double pointer to the sensor data. | 976| length | Pointer to the array length. | 977 978**Returns** 979 980Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 981 982**Example** 983 984For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 985 986 ```c 987 #include "sensors/oh_sensor.h" 988 #include "napi/native_api.h" 989 #include "hilog/log.h" 990 #include <thread> 991 992 const int SENSOR_LOG_DOMAIN = 0xD002700; 993 const char *TAG = "[Sensor]"; 994 constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_ACCELEROMETER }; 995 constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64; 996 constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000; 997 constexpr int32_t SLEEP_TIME_MS = 1000; 998 constexpr int64_t INVALID_VALUE = -1; 999 constexpr float INVALID_RESOLUTION = -1.0F; 1000 Sensor_Subscriber *g_user = nullptr; 1001 1002 // Define the callback. 1003 void SensorDataCallbackImpl(Sensor_Event *event) { 1004 if (event == nullptr) { 1005 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "event is null"); 1006 return; 1007 } 1008 int64_t timestamp = INVALID_VALUE; 1009 // Obtain the timestamp of sensor data. 1010 int32_t ret = OH_SensorEvent_GetTimestamp(event, ×tamp); 1011 if (ret != SENSOR_SUCCESS) { 1012 return; 1013 } 1014 Sensor_Type sensorType; 1015 // Obtain the sensor type. 1016 ret = OH_SensorEvent_GetType(event, &sensorType); 1017 if (ret != SENSOR_SUCCESS) { 1018 return; 1019 } 1020 Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE; 1021 // Obtain the accuracy of sensor data. 1022 ret = OH_SensorEvent_GetAccuracy(event, &accuracy); 1023 if (ret != SENSOR_SUCCESS) { 1024 return; 1025 } 1026 float *data = nullptr; 1027 uint32_t length = 0; 1028 // Obtain sensor data. 1029 ret = OH_SensorEvent_GetData(event, &data, &length); 1030 if (ret != SENSOR_SUCCESS) { 1031 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorEvent_GetData failed"); 1032 return; 1033 } 1034 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorEvent_GetData successful"); 1035 for (uint32_t i = 0; i < length; ++i) { 1036 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "accData[%{public}d]:%{public}f", i, data[i]); 1037 } 1038 } 1039 1040 static napi_value SensorEventGetData(napi_env env, napi_callback_info info) { 1041 // Create a Sensor_Subscriber instance. 1042 g_user = OH_Sensor_CreateSubscriber(); 1043 // Set the callback used to return sensor data. 1044 int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); 1045 if (ret != SENSOR_SUCCESS) { 1046 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriber_SetCallback failed"); 1047 return nullptr; 1048 } 1049 // Create a Sensor_SubscriptionId instance. 1050 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); 1051 // Set the sensor type. For example, if you use SENSOR_TYPE_ACCELEROMETER, you need to request the ohos.permission.ACCELEROMETER permission. 1052 // Configure the required permission as instructed in step 2 in the Sensor Development. 1053 ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID); 1054 if (ret != SENSOR_SUCCESS) { 1055 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionId_SetType failed"); 1056 return nullptr; 1057 } 1058 // Create a **Sensor_SubscriptionAttribute** instance. 1059 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); 1060 // Set the sensor data reporting interval. 1061 ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); 1062 if (ret != SENSOR_SUCCESS) { 1063 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionAttribute_SetSamplingInterval failed"); 1064 return nullptr; 1065 } 1066 // Subscribe to sensor data. 1067 ret = OH_Sensor_Subscribe(id, attr, g_user); 1068 if (ret != SENSOR_SUCCESS) { 1069 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Subscribe failed"); 1070 return nullptr; 1071 } 1072 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Subscribe successful"); 1073 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); 1074 // Unsubscribe from sensor data. 1075 ret = OH_Sensor_Unsubscribe(id, g_user); 1076 if (ret != SENSOR_SUCCESS) { 1077 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Unsubscribe failed"); 1078 return nullptr; 1079 } 1080 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_Sensor_Unsubscribe successful"); 1081 if (id != nullptr) { 1082 // Destroy the Sensor_SubscriptionId instance. 1083 OH_Sensor_DestroySubscriptionId(id); 1084 } 1085 if (attr != nullptr) { 1086 // Destroy the Sensor_SubscriptionAttribute instance. 1087 OH_Sensor_DestroySubscriptionAttribute(attr); 1088 } 1089 if (g_user != nullptr) { 1090 // Destroy the Sensor_Subscriber instance and reclaim memory. 1091 OH_Sensor_DestroySubscriber(g_user); 1092 g_user = nullptr; 1093 } 1094 return nullptr; 1095 } 1096 ``` 1097 1098### OH_SensorEvent_GetTimestamp() 1099 1100``` 1101int32_t OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp) 1102``` 1103**Description** 1104 1105Obtains the timestamp of sensor data. 1106 1107**Since**: 11 1108 1109**Parameters** 1110 1111| Name| Description| 1112| -------- | -------- | 1113| sensorEvent | Pointer to the sensor data information. | 1114| timestamp | Pointer to the timestamp. | 1115 1116**Returns** 1117 1118Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1119 1120 1121### OH_SensorEvent_GetType() 1122 1123``` 1124int32_t OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType) 1125``` 1126**Description** 1127 1128Obtains the sensor type. 1129 1130**Since**: 11 1131 1132**Parameters** 1133 1134| Name| Description| 1135| -------- | -------- | 1136| sensorEvent | Pointer to the sensor data information. | 1137| sensorType | Pointer to the sensor type. | 1138 1139**Returns** 1140 1141Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1142 1143 1144### OH_SensorInfo_GetMaxSamplingInterval() 1145 1146``` 1147int32_t OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval) 1148``` 1149**Description** 1150 1151Obtains the maximum data reporting interval of a sensor. 1152 1153**Since**: 11 1154 1155**Parameters** 1156 1157| Name| Description| 1158| -------- | -------- | 1159| sensor | Pointer to the sensor information. | 1160| maxSamplingInterval | Pointer to the maximum data reporting interval, in nanoseconds. | 1161 1162**Returns** 1163 1164Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1165 1166 1167### OH_SensorInfo_GetMinSamplingInterval() 1168 1169``` 1170int32_t OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval) 1171``` 1172**Description** 1173 1174Obtains the minimum data reporting interval of a sensor. 1175 1176**Since**: 11 1177 1178**Parameters** 1179 1180| Name| Description| 1181| -------- | -------- | 1182| sensor | Pointer to the sensor information. | 1183| minSamplingInterval | Pointer to the minimum data reporting interval, in nanoseconds. | 1184 1185**Returns** 1186 1187Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1188 1189 1190### OH_SensorInfo_GetName() 1191 1192``` 1193int32_t OH_SensorInfo_GetName (Sensor_Info* sensor, char *sensorName, uint32_t *length) 1194``` 1195**Description** 1196 1197Obtains the sensor name. 1198 1199**Since**: 11 1200 1201**Parameters** 1202 1203| Name| Description| 1204| -------- | -------- | 1205| sensor | Pointer to the sensor information. | 1206| sensorName | Pointer to the sensor data. | 1207| length | Pointer to the length, in bytes. | 1208 1209**Returns** 1210 1211Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1212 1213 1214### OH_SensorInfo_GetResolution() 1215 1216``` 1217int32_t OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution) 1218``` 1219**Description** 1220 1221Obtains the sensor resolution. 1222 1223**Since**: 11 1224 1225**Parameters** 1226 1227| Name| Description| 1228| -------- | -------- | 1229| sensor | Pointer to the sensor information. | 1230| resolution | Pointer to the sensor resolution. | 1231 1232**Returns** 1233 1234Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1235 1236 1237### OH_SensorInfo_GetType() 1238 1239``` 1240int32_t OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType) 1241``` 1242**Description** 1243 1244Obtains the sensor type. 1245 1246**Since**: 11 1247 1248**Parameters** 1249 1250| Name| Description| 1251| -------- | -------- | 1252| sensor | Pointer to the sensor information. | 1253| sensorType | Pointer to the sensor type. | 1254 1255**Returns** 1256 1257Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1258 1259 1260### OH_SensorInfo_GetVendorName() 1261 1262``` 1263int32_t OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length) 1264``` 1265**Description** 1266 1267Obtains the sensor's vendor name. 1268 1269**Since**: 11 1270 1271**Parameters** 1272 1273| Name| Description| 1274| -------- | -------- | 1275| sensor | Pointer to the sensor information. | 1276| vendorName | Pointer to the vendor name. | 1277| length | Pointer to the length, in bytes. | 1278 1279**Returns** 1280 1281Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1282 1283 1284### OH_SensorSubscriber_GetCallback() 1285 1286``` 1287int32_t OH_SensorSubscriber_GetCallback(Sensor_Subscriber* subscriber, Sensor_EventCallback *callback) 1288``` 1289**Description** 1290 1291Obtains the callback function used to report sensor data. 1292 1293**Since**: 11 1294 1295**Parameters** 1296 1297| Name| Description| 1298| -------- | -------- | 1299| subscriber | Pointer to the sensor subscriber information. | 1300| callback | Pointer to the callback function. | 1301 1302**Returns** 1303 1304Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1305 1306 1307### OH_SensorSubscriber_SetCallback() 1308 1309``` 1310int32_t OH_SensorSubscriber_SetCallback (Sensor_Subscriber* subscriber, const Sensor_EventCallback callback ) 1311``` 1312**Description** 1313 1314Sets a callback function to report sensor data. 1315 1316**Since**: 11 1317 1318**Parameters** 1319 1320| Name| Description| 1321| -------- | -------- | 1322| subscriber | Pointer to the sensor subscriber information. | 1323| callback | Callback function to set. | 1324 1325**Returns** 1326 1327Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1328 1329**Example** 1330 1331For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 1332 1333 ```c 1334 #include "sensors/oh_sensor.h" 1335 #include "napi/native_api.h" 1336 #include "hilog/log.h" 1337 1338 const int SENSOR_LOG_DOMAIN = 0xD002700; 1339 const char *TAG = "[Sensor]"; 1340 constexpr int64_t INVALID_VALUE = -1; 1341 1342 void SensorDataCallbackImpl(Sensor_Event *event) { 1343 if (event == nullptr) { 1344 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "event is null"); 1345 return; 1346 } 1347 int64_t timestamp = INVALID_VALUE; 1348 // Obtain the timestamp of sensor data. 1349 int32_t ret = OH_SensorEvent_GetTimestamp(event, ×tamp); 1350 if (ret != SENSOR_SUCCESS) { 1351 return; 1352 } 1353 Sensor_Type sensorType; 1354 // Obtain the sensor type. 1355 ret = OH_SensorEvent_GetType(event, &sensorType); 1356 if (ret != SENSOR_SUCCESS) { 1357 return; 1358 } 1359 Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE; 1360 // Obtain the accuracy of sensor data. 1361 ret = OH_SensorEvent_GetAccuracy(event, &accuracy); 1362 if (ret != SENSOR_SUCCESS) { 1363 return; 1364 } 1365 float *data = nullptr; 1366 uint32_t length = 0; 1367 // Obtain sensor data. 1368 ret = OH_SensorEvent_GetData(event, &data, &length); 1369 if (ret != SENSOR_SUCCESS) { 1370 return; 1371 } 1372 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy); 1373 for (uint32_t i = 0; i < length; ++i) { 1374 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "data[%{public}d]:%{public}f", i, data[i]); 1375 } 1376 } 1377 1378 static napi_value SensorSubscriberSetCallback(napi_env env, napi_callback_info info) { 1379 // Create a Sensor_Subscriber instance. 1380 Sensor_Subscriber *subscriberTemp = OH_Sensor_CreateSubscriber(); 1381 int32_t ret = OH_SensorSubscriber_SetCallback(subscriberTemp, SensorDataCallbackImpl); 1382 if (ret != SENSOR_SUCCESS) { 1383 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriber_SetCallback failed"); 1384 } else { 1385 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriber_SetCallback successful"); 1386 } 1387 // Destroy the Sensor_Subscriber instance when it is no longer needed. 1388 if (subscriberTemp != nullptr) { 1389 OH_Sensor_DestroySubscriber(subscriberTemp); 1390 } 1391 napi_value result; 1392 napi_create_int32(env, ret, &result); 1393 return result; 1394 } 1395 ``` 1396 1397### OH_SensorSubscriptionAttribute_GetSamplingInterval() 1398 1399``` 1400int32_t OH_SensorSubscriptionAttribute_GetSamplingInterval (Sensor_SubscriptionAttribute * attribute, int64_t * samplingInterval ) 1401``` 1402**Description** 1403 1404Obtains the interval for reporting sensor data. 1405 1406**Since**: 11 1407 1408**Parameters** 1409 1410| Name| Description| 1411| -------- | -------- | 1412| attribute | Pointer to the sensor subscription attribute. | 1413| samplingInterval | Pointer to the data reporting interval, in nanoseconds. | 1414 1415**Returns** 1416 1417Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1418 1419 1420### OH_SensorSubscriptionAttribute_SetSamplingInterval() 1421 1422``` 1423int32_t OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, const int64_t samplingInterval) 1424``` 1425**Description** 1426 1427Sets the interval for reporting sensor data. 1428 1429**Since**: 11 1430 1431**Parameters** 1432 1433| Name| Description| 1434| -------- | -------- | 1435| attribute | Pointer to the sensor subscription attribute. | 1436| samplingInterval | Data reporting interval to set, in nanoseconds. | 1437 1438**Returns** 1439 1440Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1441 1442**Example** 1443 1444For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 1445 1446 ```c 1447 #include "sensors/oh_sensor.h" 1448 #include "napi/native_api.h" 1449 #include "hilog/log.h" 1450 1451 const int SENSOR_LOG_DOMAIN = 0xD002700; 1452 const char *TAG = "[Sensor]"; 1453 1454 static napi_value SensorSubscriptionAttributeSetSamplingInterval(napi_env env, napi_callback_info info) { 1455 // Create a **Sensor_SubscriptionAttribute** instance. 1456 Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute(); 1457 int64_t sensorSamplePeriod = 200000000; 1458 int32_t ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, sensorSamplePeriod); 1459 if (ret != SENSOR_SUCCESS) { 1460 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionAttribute_SetSamplingInterval failed"); 1461 } else { 1462 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionAttribute_SetSamplingInterval successful"); 1463 } 1464 // Destroy the Sensor_SubscriptionAttribute instance when it is no longer needed. 1465 if (attr != nullptr) { 1466 OH_Sensor_DestroySubscriptionAttribute(attr); 1467 } 1468 napi_value result; 1469 napi_create_int32(env, ret, &result); 1470 return result; 1471 } 1472 ``` 1473 1474### OH_SensorSubscriptionId_GetType() 1475 1476``` 1477int32_t OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType) 1478``` 1479**Description** 1480 1481Obtains the sensor type. 1482 1483**Since**: 11 1484 1485**Parameters** 1486 1487| Name| Description| 1488| -------- | -------- | 1489| id | Pointer to the sensor subscription ID. | 1490| sensorType | Pointer to the sensor type. | 1491 1492**Returns** 1493 1494Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1495 1496 1497### OH_SensorSubscriptionId_SetType() 1498 1499``` 1500int32_t OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType) 1501``` 1502**Description** 1503 1504Sets the sensor type. 1505 1506**Since**: 11 1507 1508**Parameters** 1509 1510| Name| Description| 1511| -------- | -------- | 1512| id | Pointer to the sensor subscription ID. | 1513| sensorType | Sensor type to set. | 1514 1515**Returns** 1516 1517Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise. 1518 1519**Example** 1520 1521For details about the development procedure, see [Sensor Development](../../device/sensor/sensor-guidelines-capi.md). 1522 1523 ```c 1524 #include "sensors/oh_sensor.h" 1525 #include "napi/native_api.h" 1526 #include "hilog/log.h" 1527 1528 const int SENSOR_LOG_DOMAIN = 0xD002700; 1529 const char *TAG = "[Sensor]"; 1530 1531 static napi_value SensorSubscriptionIdSetType(napi_env env, napi_callback_info info) { 1532 // Create a Sensor_SubscriptionId instance. 1533 Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); 1534 Sensor_Type sensorId { SENSOR_TYPE_ACCELEROMETER }; 1535 int32_t ret = OH_SensorSubscriptionId_SetType(id, sensorId); 1536 if (ret != SENSOR_SUCCESS) { 1537 OH_LOG_Print(LOG_APP, LOG_ERROR, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionId_SetType failed"); 1538 } else { 1539 OH_LOG_Print(LOG_APP, LOG_INFO, SENSOR_LOG_DOMAIN, TAG, "OH_SensorSubscriptionId_SetType successful"); 1540 } 1541 // Destroy the Sensor_SubscriptionId instance when it is no longer needed. 1542 if (id != nullptr) { 1543 OH_Sensor_DestroySubscriptionId(id); 1544 } 1545 napi_value result; 1546 napi_create_int32(env, ret, &result); 1547 return result; 1548 } 1549 ``` 1550