1# Sensor
2
3
4## 概述
5
6提供API来使用常见的传感器特性。例如,调用API来获取传感器和订阅或取消订阅传感器数据。
7
8提供标准的开放api,定义常用传感器属性。
9
10**起始版本**:11
11
12
13## 汇总
14
15
16### 文件
17
18| 名称 | 描述 |
19| -------- | -------- |
20| [oh_sensor.h](oh_sensor_8h.md) | 声明操作传感器的API,包括获取传感器信息和订阅取消订阅传感器数据。  |
21| [oh_sensor_type.h](oh_sensor_type_8h.md) | 定义常用传感器属性。  |
22
23
24### 类型定义
25
26| 名称 | 描述 |
27| -------- | -------- |
28| [Sensor_Type](#sensor_type) | 枚举传感器类型。  |
29| [Sensor_Result](#sensor_result) | 定义传感器错误码。  |
30| [Sensor_Accuracy](#sensor_accuracy) | 枚举传感器上报的数据的精度级别。 |
31| [Sensor_Info](#sensor_info) | 定义传感器信息。  |
32| [Sensor_Event](#sensor_event) | 定义传感器数据信息。  |
33| [Sensor_SubscriptionId](#sensor_subscriptionid) | 定义传感器订阅ID,唯一标识传感器。  |
34| [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) | 定义传感器订阅属性。 |
35| void ([*Sensor_EventCallback](#sensor_eventcallback)) ([Sensor_Event](#sensor_event) \*event) | 定义用于传感器数据的回调函数。 |
36| [Sensor_Subscriber](#sensor_subscriber) | 定义传感器订阅者信息。  |
37
38
39### 枚举
40
41| 名称 | 描述 |
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/>} | 枚举传感器类型。  |
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/> } | 定义传感器错误码。  |
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/>} | 枚举传感器上报数据的精度级别。 |
46
47
48### 函数
49
50| 名称 | 描述 |
51| -------- | -------- |
52| [OH_Sensor_GetInfos](#oh_sensor_getinfos)([Sensor_Info](#sensor_info) **infos, uint32_t *count) | 获取设备上所有传感器的信息。                                 |
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) | 订阅传感器数据。系统将以指定的频率向用户上报传感器数据。<br/>订阅加速度传感器,需要申请ohos.permission.ACCELEROMETER权限;<br/>订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限;<br/>订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限;<br/>订阅与健康相关的传感器时,比如心率传感器需要申请ohos.permission.READ_HEALTH_DATA权限,否则订阅失败;<br/>订阅其余传感器不需要申请权限。 |
54| [OH_Sensor_Unsubscribe](#oh_sensor_unsubscribe)(const [Sensor_SubscriptionId](#sensor_subscriptionid) *id, const [Sensor_Subscriber](#sensor_subscriber) *subscriber) | 取消订阅传感器数据。<br/>取消订阅加速度计传感器,需要申请ohos.permission.ACCELEROMETER权限; <br/>取消订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; <br/>取消订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限;<br/>取消订阅与健康相关的传感器时,需要申请ohos.permission.READ_HEALTH_DATA权限,否则取消订阅失败。 <br/>取消订阅其余传感器不需要申请权限。 |
55| [OH_Sensor_CreateInfos](#oh_sensor_createinfos)(uint32_t count) | 用给定的数字创建一个实例数组,请参考Sensor_Info](#sensor_info)。 |
56| [OH_Sensor_DestroyInfos](#oh_sensor_destroyinfos)([Sensor_Info](#sensor_info) **sensors, uint32_t count) | 销毁实例数组并回收内存,请参考[Sensor_Info](#sensor_info)。 |
57| [OH_SensorInfo_GetName](#oh_sensorinfo_getname)([Sensor_Info](#sensor_info) *sensor, char *sensorName, uint32_t *length) | 获取传感器名称。  |
58| [OH_SensorInfo_GetVendorName](#oh_sensorinfo_getvendorname)([Sensor_Info](#sensor_info)* sensor, char *vendorName, uint32_t *length) | 获取传感器的厂商名称。  |
59| [OH_SensorInfo_GetType](#oh_sensorinfo_gettype)([Sensor_Info](#sensor_info)* sensor, [Sensor_Type](#sensor_type) *sensorType) | 获取传感器类型。  |
60| [OH_SensorInfo_GetResolution](#oh_sensorinfo_getresolution)([Sensor_Info](#sensor_info)* sensor, float *resolution) | 获取传感器分辨率。                                           |
61| [OH_SensorInfo_GetMinSamplingInterval](#oh_sensorinfo_getminsamplinginterval)([Sensor_Info](#sensor_info)* sensor, int64_t *minSamplingInterval) | 获取传感器的最小数据上报间隔。  |
62| [OH_SensorInfo_GetMaxSamplingInterval](#oh_sensorinfo_getmaxsamplinginterval)([Sensor_Info](#sensor_info)* sensor, int64_t *maxSamplingInterval) | 获取传感器的最大数据上报间隔时间。  |
63| [OH_SensorEvent_GetType](#oh_sensorevent_gettype)([Sensor_Event](#sensor_event)* sensorEvent, [Sensor_Type](#sensor_type) *sensorType) | 获取传感器类型。  |
64| [OH_SensorEvent_GetTimestamp](#oh_sensorevent_gettimestamp)([Sensor_Event](#sensor_event)* sensorEvent, int64_t *timestamp) | 获取传感器数据的时间戳。                                     |
65| [OH_SensorEvent_GetAccuracy](#oh_sensorevent_getaccuracy)([Sensor_Event](#sensor_event)* sensorEvent, Sensor_Accuracy *accuracy) | 获取传感器数据的精度。                                       |
66| [OH_SensorEvent_GetData](#oh_sensorevent_getdata)([Sensor_Event](#sensor_event)* sensorEvent, float **data, uint32_t *length) | 获取传感器数据。<br/>数据的长度和内容依赖于监听的传感器类型,传感器上报的数据格式如下:<br/>1.SENSOR_TYPE_ACCELEROMETER:data[0]、data[1]、data[2]分别表示设备x、y、z轴的加速度分量,单位m/s²;<br/>2.SENSOR_TYPE_GYROSCOPE:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角速度,单位弧度/s;<br/>3.SENSOR_TYPE_AMBIENT_LIGHT:data[0]表示环境光强度,单位lux;从API Version 12开始,将返回两个额外的数据,其中data[1]表示色温,单位kelvin;data[2]表示红外亮度,单位cd/m²;<br/>4. SENSOR_TYPE_MAGNETIC_FIELD:data[0]、data[1]、data[2]分别表示设备x、y、z轴的地磁分量,单位微特斯拉; <br/>5.SENSOR_TYPE_BAROMETER:data[0]表示气压值,单位hPa;<br/>6.SENSOR_TYPE_HALL:data[0]表示皮套吸合状态,0表示打开,大于0表示吸附;<br/>7.SENSOR_TYPE_PROXIMITY:data[0]表示接近状态,0表示接近,大于0表示远离;<br/>8.SENSOR_TYPE_ORIENTATION:data[0]、data[1]、data[2]分别表示设备绕z、x、y轴的角度,单位度;<br/>9.SENSOR_TYPE_GRAVITY:data[0]、data[1]、data[2]分别表示设备x、y、z轴的重力加速度分量,单位m/s²;<br/>10.SENSOR_TYPE_ROTATION_VECTOR:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角度,单位度,data[3]表示旋转向量元素;<br/>11.SENSOR_TYPE_PEDOMETER_DETECTION:data[0]表示计步检测状态,1表示检测到了步数变化;<br/>12.SENSOR_TYPE_PEDOMETER:data[0]表示步数;<br/>13.SENSOR_TYPE_HEART_RATE:data[0]表示心率数值;<br/>14.SENSOR_TYPE_LINEAR_ACCELERATION:从API Version 13开始支持,data[0]、data[1]、data[2]分别表示绕设备的x、y、z的线性加速度,单位m/s²;<br/>15.SENSOR_TYPE_GAME_ROTATION_VECTOR:从API Version 13开始支持,data[0]、data[1]、data[2]分别表示设备分别围绕x、y、z的旋转角度,单位为度,data[3]表示旋转向量。 |
67| [OH_Sensor_CreateSubscriptionId](#oh_sensor_createsubscriptionid)(void) | 创建一个[Sensor_SubscriptionId](#sensor_subscriptionid)实例。 |
68| [OH_Sensor_DestroySubscriptionId](#oh_sensor_destroysubscriptionid)([Sensor_SubscriptionId](#sensor_subscriptionid) *id) | 销毁[Sensor_SubscriptionId](#sensor_subscriptionid)实例并回收内存。 |
69| [OH_SensorSubscriptionId_GetType](#oh_sensorsubscriptionid_gettype)([Sensor_SubscriptionId](#sensor_subscriptionid) *id, [Sensor_Type](#sensor_type) *sensorType) | 获取传感器类型。  |
70| [OH_SensorSubscriptionId_SetType](#oh_sensorsubscriptionid_settype)([Sensor_SubscriptionId](#sensor_subscriptionid)* id, const [Sensor_Type](#sensor_type) sensorType) | 设置传感器类型。  |
71| [OH_Sensor_CreateSubscriptionAttribute](#oh_sensor_createsubscriptionattribute)(void) | 创建[Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)实例。 |
72| [OH_Sensor_DestroySubscriptionAttribute](#oh_sensor_destroysubscriptionattribute)([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) *attribute) | 销毁[Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)实例并回收内存。 |
73| [OH_SensorSubscriptionAttribute_SetSamplingInterval](#oh_sensorsubscriptionattribute_setsamplinginterval)([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, const int64_t samplingInterval) | 设置传感器数据上报间隔。                                  |
74| [OH_SensorSubscriptionAttribute_GetSamplingInterval](#oh_sensorsubscriptionattribute_getsamplinginterval) ([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, int64_t *samplingInterval) | 获取传感器数据上报间隔。                                   |
75| [OH_Sensor_CreateSubscriber](#oh_sensor_createsubscriber)(void) | 创建一个[Sensor_Subscriber](#sensor_subscriber)实例。      |
76| [OH_Sensor_DestroySubscriber](#oh_sensor_destroysubscriber)([Sensor_Subscriber](#sensor_subscriber) *subscriber) | 销毁[Sensor_Subscriber](#sensor_subscriber)实例并回收内存。 |
77| [OH_SensorSubscriber_SetCallback](#oh_sensorsubscriber_setcallback)([Sensor_Subscriber](#sensor_subscriber)* subscriber, const [Sensor_EventCallback](#sensor_eventcallback) callback) | 设置一个回调函数来上报传感器数据。 |
78| [OH_SensorSubscriber_GetCallback](#oh_sensorsubscriber_getcallback)([Sensor_Subscriber](#sensor_subscriber)* subscriber, [Sensor_EventCallback](#sensor_eventcallback) *callback) | 获取用于上报传感器数据的回调函数。 |
79
80
81## 类型定义说明
82
83
84### Sensor_Accuracy
85
86```
87typedef enum Sensor_Accuracy Sensor_Accuracy
88```
89**描述**:
90
91枚举传感器上报的数据的精度级别。
92
93**起始版本**:11
94
95
96### Sensor_Event
97
98```
99typedef struct Sensor_Event Sensor_Event
100```
101**描述**:
102
103定义传感器数据信息。
104
105**起始版本**:11
106
107
108### Sensor_EventCallback
109
110```
111typedef void(*Sensor_EventCallback)(Sensor_Event *event)
112```
113**描述**:
114
115定义用于上报传感器数据的回调函数。
116
117**起始版本**:11
118
119
120### Sensor_Info
121
122```
123typedef struct Sensor_Info Sensor_Info
124```
125**描述**:
126
127定义传感器信息。
128
129**起始版本**:11
130
131
132### Sensor_Result
133
134```
135typedef enum Sensor_Result Sensor_Result
136```
137**描述**:
138
139定义传感器错误码。
140
141**起始版本**:11
142
143
144### Sensor_Subscriber
145
146```
147typedef struct Sensor_Subscriber Sensor_Subscriber
148```
149**描述**:
150
151定义传感器订阅者信息。
152
153**起始版本**:11
154
155
156### Sensor_SubscriptionAttribute
157
158```
159typedef struct Sensor_SubscriptionAttribute Sensor_SubscriptionAttribute
160```
161**描述**:
162
163定义传感器订阅属性。
164
165**起始版本**:11
166
167
168### Sensor_SubscriptionId
169
170```
171typedef struct Sensor_SubscriptionId Sensor_SubscriptionId
172```
173**描述**:
174
175定义传感器订阅ID,唯一标识传感器。
176
177**起始版本**:11
178
179
180### Sensor_Type
181
182```
183typedef enum Sensor_Type Sensor_Type
184```
185**描述**:
186
187枚举传感器类型。
188
189**起始版本**:11
190
191
192## 枚举类型说明
193
194
195### Sensor_Accuracy
196
197```
198enum Sensor_Accuracy
199```
200**描述**:
201
202枚举传感器上报的数据的精度级别。
203
204**起始版本**:11
205
206| 枚举值 | 描述 |
207| -------- | -------- |
208| SENSOR_ACCURACY_UNRELIABLE  | 传感器数据不可靠。有可能传感器不与设备接触而进行测量。 |
209| SENSOR_ACCURACY_LOW  | 传感器数据精度较低。数据在使用前必须根据环境进行校准。 |
210| SENSOR_ACCURACY_MEDIUM  | 传感器数据处于中等精度水平。建议用户在使用前根据实际环境进行数据校准。 |
211| SENSOR_ACCURACY_HIGH  | 传感器数据具有很高的精度。数据可以直接使用。 |
212
213
214### Sensor_Result
215
216```
217enum Sensor_Result
218```
219**描述**:
220
221定义传感器错误码。
222
223**起始版本**:11
224
225| 枚举值 | 描述 |
226| -------- | -------- |
227| SENSOR_SUCCESS  | 操作成功。 |
228| SENSOR_PERMISSION_DENIED  | 权限验证失败。 |
229| SENSOR_PARAMETER_ERROR  | 参数检查失败。例如,没有传入强制参数,或者传入的参数类型不正确。 |
230| SENSOR_SERVICE_EXCEPTION  | 传感器服务异常。 |
231
232
233### Sensor_Type
234
235```
236enum Sensor_Type
237```
238**描述**:
239
240枚举传感器类型。
241
242**起始版本**:11
243
244| 枚举值 | 描述 |
245| -------- | -------- |
246| SENSOR_TYPE_ACCELEROMETER  | 加速度传感器。 |
247| SENSOR_TYPE_GYROSCOPE  | 陀螺仪传感器。 |
248| SENSOR_TYPE_AMBIENT_LIGHT  | 环境光传感器。 |
249| SENSOR_TYPE_MAGNETIC_FIELD  | 地磁传感器。 |
250| SENSOR_TYPE_BAROMETER  | 气压传感器。 |
251| SENSOR_TYPE_HALL  | 霍尔传感器。 |
252| SENSOR_TYPE_PROXIMITY  | 接近光传感器。 |
253| SENSOR_TYPE_ORIENTATION  | 方向传感器。 |
254| SENSOR_TYPE_GRAVITY  | 重力传感器。 |
255| SENSOR_TYPE_ROTATION_VECTOR  | 旋转矢量传感器。 |
256| SENSOR_TYPE_PEDOMETER_DETECTION  | 计步器检测传感器。 |
257| SENSOR_TYPE_PEDOMETER  | 计步器传感器。 |
258| SENSOR_TYPE_HEART_RATE  | 心率传感器。 |
259
260
261## 函数说明
262
263
264### OH_Sensor_CreateInfos()
265
266```
267Sensor_Info **OH_Sensor_CreateInfos(uint32_t count)
268```
269**描述**:
270
271用给定的数字创建一个实例数组,请参考 [Sensor_Info](#sensor_info)。
272
273**起始版本**:11
274
275**参数**:
276
277| 名称 | 描述 |
278| -------- | -------- |
279| count | 要创建的实例的数量,请参考 [Sensor_Info](#sensor_info)。 |
280
281**返回**:
282
283如果操作成功,返回指向 [Sensor_Info](#sensor_info) 实例数组的双指针;否则返回**NULL**。
284
285
286### OH_Sensor_CreateSubscriber()
287
288```
289Sensor_Subscriber *OH_Sensor_CreateSubscriber(void)
290```
291**描述**:
292
293创建一个 [Sensor_Subscriber ](#sensor_subscriber)实例。
294
295**起始版本**:11
296
297**返回**:
298
299如果操作成功,返回指向 [Sensor_Subscriber](#sensor_subscriber) 实例的指针;否则返回**NULL**。
300
301
302### OH_Sensor_CreateSubscriptionAttribute()
303
304```
305Sensor_SubscriptionAttribute *OH_Sensor_CreateSubscriptionAttribute(void)
306```
307**描述**:
308
309创建 [Sensor_SubscriptionAttribute ](#sensor_subscriptionattribute)实例。
310
311**起始版本**:11
312
313**返回**:
314
315如果操作成功,返回指向 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例的指针;否则返回**NULL**。
316
317
318### OH_Sensor_CreateSubscriptionId()
319
320```
321Sensor_SubscriptionId *OH_Sensor_CreateSubscriptionId(void)
322```
323**描述**:
324
325创建一个 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例。
326
327**起始版本**:11
328
329**返回**:
330
331如果操作成功,返回指向 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例的指针;否则返回**NULL**。
332
333
334### OH_Sensor_DestroyInfos()
335
336```
337int32_t OH_Sensor_DestroyInfos(Sensor_Info **sensors, uint32_t count)
338```
339**描述**:
340
341销毁实例数组并回收内存,请参考 [Sensor_Info](#sensor_info)。
342
343**起始版本**:11
344
345**参数**:
346
347| 名称 | 描述 |
348| -------- | -------- |
349| sensors | 指向 [Sensor_Info](#sensor_info) 实例数组的双指针。 |
350| count | 要销毁的 [Sensor_Info](#sensor_info) 实例的数量。 |
351
352**返回**:
353
354如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
355
356
357### OH_Sensor_DestroySubscriber()
358
359```
360int32_t OH_Sensor_DestroySubscriber(Sensor_Subscriber *subscriber)
361```
362**描述**:
363
364销毁 [Sensor_Subscriber ](#sensor_subscriber)实例并回收内存。
365
366**起始版本**:11
367
368**参数**:
369
370| 名称 | 描述 |
371| -------- | -------- |
372| subscriber | 指向 [Sensor_Subscriber](#sensor_subscriber) 实例的指针。 |
373
374**返回**:
375
376如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
377
378
379### OH_Sensor_DestroySubscriptionAttribute()
380
381```
382int32_t OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute *attribute)
383```
384**描述**:
385
386销毁 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例并回收内存。
387
388**起始版本**:11
389
390**参数**:
391
392| 名称 | 描述 |
393| -------- | -------- |
394| attribute | 指向 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例的指针。 |
395
396**返回**:
397
398如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
399
400
401### OH_Sensor_DestroySubscriptionId()
402
403```
404int32_t OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId *id)
405```
406**描述**:
407
408销毁 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例并回收内存。
409
410**起始版本**:11
411
412**参数**:
413
414| 名称 | 描述 |
415| -------- | -------- |
416| id | 指向 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例的指针。 |
417
418**返回**:
419
420如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
421
422
423### OH_Sensor_GetInfos()
424
425```
426Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count)
427```
428**描述**:
429
430获取设备上所有传感器的信息。
431
432**起始版本**:11
433
434**参数**:
435
436| 名称 | 描述 |
437| -------- | -------- |
438| infos | 双指针指向设备上所有传感器的信息。请参考 [Sensor_Info](#sensor_info)。 |
439| count | 指向设备上传感器数量的指针。  |
440
441**返回**:
442
443如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
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**描述**:
452
453订阅传感器数据。系统将以指定的频率向用户上报传感器数据。 订阅加速度传感器,需要申请ohos.permission.ACCELEROMETER权限; 订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; 订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限; 订阅与健康相关的传感器时,比如心率传感器,需要申请ohos.permission.READ_HEALTH_DATA权限,否则订阅失败。 订阅其余传感器不需要申请权限。
454
455**起始版本**:11
456
457**参数**:
458
459| 名称 | 描述 |
460| -------- | -------- |
461| id | 指向传感器订阅ID的指针。请参考 [Sensor_SubscriptionId](#sensor_subscriptionid)。 |
462| attribute | 指向订阅属性的指针,该属性用于指定数据报告频率。请参考 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)。 |
463| subscriber | 指向订阅者信息的指针,该信息用于指定的回调函数报告传感器数据。请参考 [Sensor_Subscriber](#sensor_subscriber)。 |
464
465**返回**:
466
467如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
468
469**Permission**:
470
471ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or 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**描述**:
480
481取消订阅传感器数据。 取消订阅加速度计传感器,需要申请ohos.permission.ACCELEROMETER权限; 取消订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; 取消订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限; 取消订阅与健康相关的传感器时,需要申请ohos.permission.READ_HEALTH_DATA权限,否则取消订阅失败。 取消订阅其余传感器不需要申请权限。
482
483**起始版本**:11
484
485**参数**:
486
487| 名称 | 描述 |
488| -------- | -------- |
489| id | 指向传感器订阅ID的指针。请参考 [Sensor_SubscriptionId](#sensor_subscriptionid)。 |
490| subscriber | 指向订阅者信息的指针,该信息用于指定的回调函数报告传感器数据。请参考 [Sensor_Subscriber](#sensor_subscriber)。 |
491
492**返回**:
493
494如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
495
496**Permission**:
497
498ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or 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**描述**:
507
508获取传感器数据的精度。
509
510**起始版本**:11
511
512**参数**:
513
514| 名称 | 描述 |
515| -------- | -------- |
516| sensorEvent | 指向传感器数据信息的指针。  |
517| accuracy | 指向精度的指针。  |
518
519**返回**:
520
521如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
522
523
524### OH_SensorEvent_GetData()
525
526```
527int32_t OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length)
528```
529**描述**:
530
531获取传感器数据。数据的长度和内容依赖于监听的传感器类型,传感器上报的数据格式如下:
532
533SENSOR_TYPE_ACCELEROMETER:data[0]、data[1]、data[2]分别表示设备x、y、z轴的加速度分量,单位m/s²;
534
535SENSOR_TYPE_GYROSCOPE:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角速度,单位弧度/s;
536
537SENSOR_TYPE_AMBIENT_LIGHT:data[0]表示环境光强度,in lux;
538
539SENSOR_TYPE_MAGNETIC_FIELD:data[0]、data[1]、data[2]分别表示设备x、y、z轴的地磁分量,单位微特斯拉;
540
541SENSOR_TYPE_BAROMETER:data[0]表示气压值,单位hPa;
542
543SENSOR_TYPE_HALL:data[0]表示皮套吸合状态,0表示打开,大于0表示吸附;
544
545SENSOR_TYPE_PROXIMITY:data[0]表示接近状态,0表示接近,大于0表示远离;
546
547SENSOR_TYPE_ORIENTATION:data[0]、data[1]、data[2]分别表示设备绕z、x、y轴的角度,单位度;
548
549SENSOR_TYPE_GRAVITY:data[0]、data[1]、data[2]分别表示设备x、y、z轴的重力加速度分量,单位m/s²;
550
551SENSOR_TYPE_ROTATION_VECTOR:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角度,单位度,data[3]表示旋转向量元素;
552
553SENSOR_TYPE_PEDOMETER_DETECTION:data[0]表示计步检测状态,1表示检测到了步数变化;
554
555SENSOR_TYPE_PEDOMETER:data[0]表示步数;
556
557SENSOR_TYPE_HEART_RATE:data[0]表示心率数值;
558
559SENSOR_TYPE_LINEAR_ACCELERATION:从API Version 13开始支持,data[0]、data[1]、data[2]分别表示绕设备的x、y、z的线性加速度,单位m/s²;
560
561SENSOR_TYPE_GAME_ROTATION_VECTOR:从API Version 13开始支持,data[0]、data[1]、data[2]分别表示设备分别围绕x、y、z的旋转角度,单位为度,data[3]表示旋转向量。
562
563**起始版本**:11
564
565**参数**:
566
567| 名称 | 描述 |
568| -------- | -------- |
569| sensorEvent | 传感器数据信息。  |
570| data | 出参,传感器数据。  |
571| length | 出参,数组长度。  |
572
573**返回**:
574
575如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
576
577
578### OH_SensorEvent_GetTimestamp()
579
580```
581int32_t OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp)
582```
583**描述**:
584
585获取传感器数据的时间戳。
586
587**起始版本**:11
588
589**参数**:
590
591| 名称 | 描述 |
592| -------- | -------- |
593| sensorEvent | 指向传感器数据信息的指针。  |
594| timestamp | 时间戳指针。  |
595
596**返回**:
597
598如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
599
600
601### OH_SensorEvent_GetType()
602
603```
604int32_t OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType)
605```
606**描述**:
607
608获取传感器类型。
609
610**起始版本**:11
611
612**参数**:
613
614| 名称 | 描述 |
615| -------- | -------- |
616| sensorEvent | 指向传感器数据信息的指针。  |
617| sensorType | 指向传感器类型的指针。  |
618
619**返回**:
620
621如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
622
623
624### OH_SensorInfo_GetMaxSamplingInterval()
625
626```
627int32_t OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval)
628```
629**描述**:
630
631获取传感器的最大数据上报间隔时间。
632
633**起始版本**:11
634
635**参数**:
636
637| 名称 | 描述 |
638| -------- | -------- |
639| sensor | 指向传感器信息的指针。  |
640| maxSamplingInterval | 指向最大数据报告间隔的指针,单位为纳秒。  |
641
642**返回**:
643
644如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
645
646
647### OH_SensorInfo_GetMinSamplingInterval()
648
649```
650int32_t OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval)
651```
652**描述**:
653
654获取传感器的最小数据上报间隔。
655
656**起始版本**:11
657
658**参数**:
659
660| 名称 | 描述 |
661| -------- | -------- |
662| sensor | 指向传感器信息的指针。  |
663| minSamplingInterval | 指向最小数据报告间隔的指针,以纳秒为单位。  |
664
665**返回**:
666
667如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
668
669
670### OH_SensorInfo_GetName()
671
672```
673int32_t OH_SensorInfo_GetName (Sensor_Info* sensor, char *sensorName, uint32_t *length)
674```
675**描述**:
676
677获取传感器名称。
678
679**起始版本**:11
680
681**参数**:
682
683| 名称 | 描述 |
684| -------- | -------- |
685| sensor | 指向传感器信息的指针。  |
686| sensorName | 指向传感器名称的指针。  |
687| length | 指向长度的指针,以字节为单位。  |
688
689**返回**:
690
691如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
692
693
694### OH_SensorInfo_GetResolution()
695
696```
697int32_t OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution)
698```
699**描述**:
700
701获取传感器分辨率。
702
703**起始版本**:11
704
705**参数**:
706
707| 名称 | 描述 |
708| -------- | -------- |
709| sensor | 指向传感器信息的指针。  |
710| resolution | 指向传感器分辨率的指针。  |
711
712**返回**:
713
714如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
715
716
717### OH_SensorInfo_GetType()
718
719```
720int32_t OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType)
721```
722**描述**:
723
724获取传感器类型。
725
726**起始版本**: 11
727
728**参数**:
729
730| 名称 | 描述 |
731| -------- | -------- |
732| sensor | 指向传感器信息的指针。  |
733| sensorType | 指向传感器类型的指针。  |
734
735**返回**:
736
737如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
738
739
740### OH_SensorInfo_GetVendorName()
741
742```
743int32_t OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length)
744```
745**描述**:
746
747获取传感器的厂商名称。
748
749**起始版本**:11
750
751**参数**:
752
753| 名称 | 描述 |
754| -------- | -------- |
755| sensor | 指向传感器信息的指针。  |
756| vendorName | 指向供应商名称的指针。  |
757| length | 指向长度的指针,以字节为单位。  |
758
759**返回**:
760
761如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
762
763
764### OH_SensorSubscriber_GetCallback()
765
766```
767int32_t OH_SensorSubscriber_GetCallback(Sensor_Subscriber* subscriber, Sensor_EventCallback *callback)
768```
769**描述**:
770
771获取用于上报传感器数据的回调函数。
772
773**起始版本**:11
774
775**参数**:
776
777| 名称 | 描述 |
778| -------- | -------- |
779| subscriber | 指向传感器订阅者信息的指针。  |
780| callback | 指向回调函数的指针。  |
781
782**返回**:
783
784如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
785
786
787### OH_SensorSubscriber_SetCallback()
788
789```
790int32_t OH_SensorSubscriber_SetCallback (Sensor_Subscriber* subscriber, const Sensor_EventCallback callback )
791```
792**描述**:
793
794设置一个回调函数来上报传感器数据。
795
796**起始版本**:11
797
798**参数**:
799
800| 名称 | 描述 |
801| -------- | -------- |
802| subscriber | 指向传感器订阅者信息的指针。  |
803| callback | 设置回调函数。  |
804
805**返回**:
806
807如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
808
809
810### OH_SensorSubscriptionAttribute_GetSamplingInterval()
811
812```
813int32_t OH_SensorSubscriptionAttribute_GetSamplingInterval (Sensor_SubscriptionAttribute * attribute, int64_t * samplingInterval )
814```
815**描述**:
816
817获取传感器数据上报间隔。
818
819**起始版本**:11
820
821**参数**:
822
823| 名称 | 描述 |
824| -------- | -------- |
825| attribute | 指向传感器订阅属性的指针。  |
826| samplingInterval | 指向数据报告间隔的指针,以纳秒为单位。  |
827
828**返回**:
829
830如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
831
832
833### OH_SensorSubscriptionAttribute_SetSamplingInterval()
834
835```
836int32_t OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, const int64_t samplingInterval)
837```
838**描述**:
839
840设置传感器数据上报间隔。
841
842**起始版本**:11
843
844**参数**:
845
846| 名称 | 描述 |
847| -------- | -------- |
848| attribute | 指向传感器订阅属性的指针。  |
849| samplingInterval | 要设置的数据报告间隔,以纳秒为单位。  |
850
851**返回**:
852
853如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
854
855
856### OH_SensorSubscriptionId_GetType()
857
858```
859int32_t OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType)
860```
861**描述**:
862
863获取传感器类型。
864
865**起始版本**:11
866
867**参数**:
868
869| 名称 | 描述 |
870| -------- | -------- |
871| id | 指向传感器订阅ID的指针。  |
872| sensorType | 指向传感器类型的指针。  |
873
874**返回**:
875
876如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
877
878
879### OH_SensorSubscriptionId_SetType()
880
881```
882int32_t OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType)
883```
884**描述**:
885
886设置传感器类型。
887
888**起始版本**:11
889
890**参数**:
891
892| 名称 | 描述 |
893| -------- | -------- |
894| id | 指向传感器订阅ID的指针。  |
895| sensorType | 要设置的传感器类型。  |
896
897**返回**:
898
899如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。
900