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 for sensor types. |
29| [Sensor_Result](#sensor_result) | Defines an enum for 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
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. 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.
454
455**Since**: 11
456
457**Parameters**
458
459| Name| Description|
460| -------- | -------- |
461| id | Pointer to the sensor subscription ID. For details, see [Sensor_SubscriptionId](#sensor_subscriptionid).|
462| attribute | Pointer to the subscription attribute, which is used to specify the data reporting frequency. For details, see [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute).|
463| subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see [Sensor_Subscriber](#sensor_subscriber).|
464
465**Returns**
466
467Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
468
469**Required Permissions**
470
471ohos.permission.ACCELEROMETER, ohos.permission.GYROSCOPE, ohos.permission.ACTIVITY_MOTION, or ohos.permission.READ_HEALTH_DATA
472
473
474### OH_Sensor_Unsubscribe()
475
476```
477Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber)
478```
479**Description**
480
481Unsubscribes 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.
482
483**Since**: 11
484
485**Parameters**
486
487| Name| Description|
488| -------- | -------- |
489| id | Pointer to the sensor subscription ID. For details, see [Sensor_SubscriptionId](#sensor_subscriptionid).|
490| subscriber | Pointer to the subscriber information, which is used by the callback function to report sensor data. For details, see [Sensor_Subscriber](#sensor_subscriber).|
491
492**Returns**
493
494Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
495
496**Required Permissions**
497
498ohos.permission.ACCELEROMETER, ohos.permission.GYROSCOPE, ohos.permission.ACTIVITY_MOTION, or ohos.permission.READ_HEALTH_DATA
499
500
501### OH_SensorEvent_GetAccuracy()
502
503```
504int32_t OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy)
505```
506**Description**
507
508Obtains the accuracy of sensor data.
509
510**Since**: 11
511
512**Parameters**
513
514| Name| Description|
515| -------- | -------- |
516| sensorEvent | Pointer to the sensor data information. |
517| accuracy | Pointer to the accuracy. |
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
524### OH_SensorEvent_GetData()
525
526```
527int32_t OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length)
528```
529**Description**
530
531Obtains sensor data. The data length and content depend on the sensor type. The format of the sensor data reported is as follows:
532
533- 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².
534
535- 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.
536
537- SENSOR_TYPE_AMBIENT_LIGHT: data[0], indicating the ambient light intensity, in lux.
538
539- 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.
540
541- SENSOR_TYPE_BAROMETER: data[0], indicating the atmospheric pressure, in hPa.
542
543- 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.
544
545- 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.
546
547- 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.
548
549- 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².
550
551- 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.
552
553- SENSOR_TYPE_PEDOMETER_DETECTION: data[0], indicating the pedometer detection status. The value **1** means that the number of detected steps changes.
554
555- SENSOR_TYPE_PEDOMETER: data[0], indicating the number of steps a user has walked.
556
557- SENSOR_TYPE_HEART_RATE: data[0], indicating the heart rate value.
558
559- 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.
560
561- 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.
562
563**Since**: 11
564
565**Parameters**
566
567| Name| Description|
568| -------- | -------- |
569| sensorEvent | Pointer to the sensor data information. |
570| data | Double pointer to the sensor data. |
571| length | Pointer to the array length. |
572
573**Returns**
574
575Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
576
577
578### OH_SensorEvent_GetTimestamp()
579
580```
581int32_t OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp)
582```
583**Description**
584
585Obtains the timestamp of sensor data.
586
587**Since**: 11
588
589**Parameters**
590
591| Name| Description|
592| -------- | -------- |
593| sensorEvent | Pointer to the sensor data information. |
594| timestamp | Pointer to the timestamp. |
595
596**Returns**
597
598Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
599
600
601### OH_SensorEvent_GetType()
602
603```
604int32_t OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType)
605```
606**Description**
607
608Obtains the sensor type.
609
610**Since**: 11
611
612**Parameters**
613
614| Name| Description|
615| -------- | -------- |
616| sensorEvent | Pointer to the sensor data information. |
617| sensorType | Pointer to the sensor type. |
618
619**Returns**
620
621Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
622
623
624### OH_SensorInfo_GetMaxSamplingInterval()
625
626```
627int32_t OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval)
628```
629**Description**
630
631Obtains the maximum data reporting interval of a sensor.
632
633**Since**: 11
634
635**Parameters**
636
637| Name| Description|
638| -------- | -------- |
639| sensor | Pointer to the sensor information. |
640| maxSamplingInterval | Pointer to the maximum data reporting interval, in nanoseconds. |
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
647### OH_SensorInfo_GetMinSamplingInterval()
648
649```
650int32_t OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval)
651```
652**Description**
653
654Obtains the minimum data reporting interval of a sensor.
655
656**Since**: 11
657
658**Parameters**
659
660| Name| Description|
661| -------- | -------- |
662| sensor | Pointer to the sensor information. |
663| minSamplingInterval | Pointer to the minimum data reporting interval, in nanoseconds. |
664
665**Returns**
666
667Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
668
669
670### OH_SensorInfo_GetName()
671
672```
673int32_t OH_SensorInfo_GetName (Sensor_Info* sensor, char *sensorName, uint32_t *length)
674```
675**Description**
676
677Obtains the sensor name.
678
679**Since**: 11
680
681**Parameters**
682
683| Name| Description|
684| -------- | -------- |
685| sensor | Pointer to the sensor information. |
686| sensorName | Pointer to the sensor data. |
687| length | Pointer to the length, in bytes. |
688
689**Returns**
690
691Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
692
693
694### OH_SensorInfo_GetResolution()
695
696```
697int32_t OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution)
698```
699**Description**
700
701Obtains the sensor resolution.
702
703**Since**: 11
704
705**Parameters**
706
707| Name| Description|
708| -------- | -------- |
709| sensor | Pointer to the sensor information. |
710| resolution | Pointer to the sensor resolution. |
711
712**Returns**
713
714Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
715
716
717### OH_SensorInfo_GetType()
718
719```
720int32_t OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType)
721```
722**Description**
723
724Obtains the sensor type.
725
726**Since**: 11
727
728**Parameters**
729
730| Name| Description|
731| -------- | -------- |
732| sensor | Pointer to the sensor information. |
733| sensorType | Pointer to the sensor type. |
734
735**Returns**
736
737Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
738
739
740### OH_SensorInfo_GetVendorName()
741
742```
743int32_t OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length)
744```
745**Description**
746
747Obtains the sensor's vendor name.
748
749**Since**: 11
750
751**Parameters**
752
753| Name| Description|
754| -------- | -------- |
755| sensor | Pointer to the sensor information. |
756| vendorName | Pointer to the vendor name. |
757| length | Pointer to the length, in bytes. |
758
759**Returns**
760
761Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
762
763
764### OH_SensorSubscriber_GetCallback()
765
766```
767int32_t OH_SensorSubscriber_GetCallback(Sensor_Subscriber* subscriber, Sensor_EventCallback *callback)
768```
769**Description**
770
771Obtains the callback function used to report sensor data.
772
773**Since**: 11
774
775**Parameters**
776
777| Name| Description|
778| -------- | -------- |
779| subscriber | Pointer to the sensor subscriber information. |
780| callback | Pointer to the callback function. |
781
782**Returns**
783
784Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
785
786
787### OH_SensorSubscriber_SetCallback()
788
789```
790int32_t OH_SensorSubscriber_SetCallback (Sensor_Subscriber* subscriber, const Sensor_EventCallback callback )
791```
792**Description**
793
794Sets a callback function to report sensor data.
795
796**Since**: 11
797
798**Parameters**
799
800| Name| Description|
801| -------- | -------- |
802| subscriber | Pointer to the sensor subscriber information. |
803| callback | Callback function to set. |
804
805**Returns**
806
807Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
808
809
810### OH_SensorSubscriptionAttribute_GetSamplingInterval()
811
812```
813int32_t OH_SensorSubscriptionAttribute_GetSamplingInterval (Sensor_SubscriptionAttribute * attribute, int64_t * samplingInterval )
814```
815**Description**
816
817Obtains the interval for reporting sensor data.
818
819**Since**: 11
820
821**Parameters**
822
823| Name| Description|
824| -------- | -------- |
825| attribute | Pointer to the sensor subscription attribute. |
826| samplingInterval | Pointer to the data reporting interval, in nanoseconds. |
827
828**Returns**
829
830Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
831
832
833### OH_SensorSubscriptionAttribute_SetSamplingInterval()
834
835```
836int32_t OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, const int64_t samplingInterval)
837```
838**Description**
839
840Sets the interval for reporting sensor data.
841
842**Since**: 11
843
844**Parameters**
845
846| Name| Description|
847| -------- | -------- |
848| attribute | Pointer to the sensor subscription attribute. |
849| samplingInterval | Data reporting interval to set, in nanoseconds. |
850
851**Returns**
852
853Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
854
855
856### OH_SensorSubscriptionId_GetType()
857
858```
859int32_t OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType)
860```
861**Description**
862
863Obtains the sensor type.
864
865**Since**: 11
866
867**Parameters**
868
869| Name| Description|
870| -------- | -------- |
871| id | Pointer to the sensor subscription ID. |
872| sensorType | Pointer to the sensor type. |
873
874**Returns**
875
876Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
877
878
879### OH_SensorSubscriptionId_SetType()
880
881```
882int32_t OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType)
883```
884**Description**
885
886Sets the sensor type.
887
888**Since**: 11
889
890**Parameters**
891
892| Name| Description|
893| -------- | -------- |
894| id | Pointer to the sensor subscription ID. |
895| sensorType | Sensor type to set. |
896
897**Returns**
898
899Returns **SENSOR_SUCCESS** if the operation is successful; returns an error code defined in [Sensor_Result](#sensor_result) otherwise.
900