• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @system.sensor (传感器)
2<!--Kit: Sensor Service Kit-->
3<!--Subsystem: Sensors-->
4<!--Owner: @dilligencer-->
5<!--Designer: @butterls-->
6<!--Tester: @murphy84-->
7<!--Adviser: @hu-zhiqiong-->
8
9sensor模块提供订阅传感器数据基本能力,主要包含查询传感器的列表、订阅/取消传感器的数据、执行控制命令等。
10
11根据传感器的用途,可以将传感器分为六大类:运动类传感器、环境类传感器、方向类传感器、光线类传感器、健康类传感器、其他类传感器(如霍尔传感器),每一大类传感器包含不同类型的传感器,某种类型的传感器可能是单一的物理传感器,也可能是由多个物理传感器复合而成。
12
13
14> **说明:**
15>
16> - 从API Version 8开始,该接口不再维护,推荐使用新接口[`@ohos.sensor`](js-apis-sensor.md)。
17> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
18> - 该功能使用需要对应硬件支持,仅支持真机调试。
19
20
21## 导入模块
22
23
24```
25import { Sensor } from '@kit.SensorServiceKit';
26```
27
28## Sensor.subscribeAccelerometer
29
30 subscribeAccelerometer(options: subscribeAccelerometerOptions): void
31
32观察加速度数据变化。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
33
34**系统能力**:SystemCapability.Sensors.Sensor.Lite
35
36**需要权限**:ohos.permission.ACCELEROMETER,该权限为系统权限
37
38**参数**:
39
40| 参数名  | 类型                                                         | 必填 | 说明                                       |
41| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------ |
42| options | [subscribeAccelerometerOptions](#subscribeaccelerometeroptions) | 是   | 监听加速度传感器数据的回调函数的执行频率。 |
43
44**示例**:
45
46```ts
47import { Sensor, AccelerometerResponse, subscribeAccelerometerOptions } from '@kit.SensorServiceKit';
48
49let accelerometerOptions: subscribeAccelerometerOptions = {
50  interval: 'normal',
51  success: (ret: AccelerometerResponse) => {
52    console.info('Succeeded in subscribing. X-axis data: ' + ret.x);
53    console.info('Succeeded in subscribing. Y-axis data: ' + ret.y);
54    console.info('Succeeded in subscribing. Z-axis data: ' + ret.z);
55  },
56  fail: (data: string, code: number) => {
57    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
58  },
59};
60Sensor.subscribeAccelerometer(accelerometerOptions);
61```
62
63> **说明:**
64> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
65
66## Sensor.unsubscribeAccelerometer
67
68unsubscribeAccelerometer(): void
69
70取消订阅加速度数据。
71
72**系统能力**:SystemCapability.Sensors.Sensor.Lite
73
74**需要权限**:ohos.permission.ACCELEROMETER,该权限为系统权限
75
76**示例**:
77
78```ts
79Sensor.unsubscribeAccelerometer();
80```
81
82## Sensor.subscribeCompass
83
84 subscribeCompass(options: SubscribeCompassOptions): void
85
86订阅罗盘数据变化。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
87
88**系统能力**:SystemCapability.Sensors.Sensor.Lite
89
90**参数**:
91
92| 参数名  | 类型                                                | 必填 | 说明                             |
93| ------- | --------------------------------------------------- | ---- | -------------------------------- |
94| options | [SubscribeCompassOptions](#subscribecompassoptions) | 是   | 当罗盘传感器数据发生变化时调用。 |
95
96**示例**:
97
98```ts
99import { Sensor, CompassResponse, SubscribeCompassOptions } from '@kit.SensorServiceKit';
100
101let subscribeCompassOptions: SubscribeCompassOptions = {
102  success: (ret: CompassResponse) => {
103    console.info('Succeeded in subscribing. Get data direction:' + ret.direction);
104  },
105  fail: (data: string, code: number) => {
106    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
107  },
108};
109Sensor.subscribeCompass(subscribeCompassOptions);
110```
111
112> **说明:**
113> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
114
115## Sensor.unsubscribeCompass
116
117unsubscribeCompass(): void
118
119取消订阅罗盘。
120
121**系统能力**:SystemCapability.Sensors.Sensor.Lite
122
123**示例**:
124
125```ts
126Sensor.unsubscribeCompass();
127```
128
129## Sensor.subscribeProximity
130
131 subscribeProximity(options: SubscribeProximityOptions): void
132
133订阅距离感应数据变化。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
134
135**系统能力**:SystemCapability.Sensors.Sensor.Lite
136
137**参数**:
138
139| 参数名  | 类型                                                    | 必填 | 说明                             |
140| ------- | ------------------------------------------------------- | ---- | -------------------------------- |
141| options | [SubscribeProximityOptions](#subscribeproximityoptions) | 是   | 当距离传感器数据发生变化时调用。 |
142
143**示例**:
144
145```ts
146import { Sensor, ProximityResponse, SubscribeProximityOptions } from '@kit.SensorServiceKit';
147
148let subscribeProximityOptions: SubscribeProximityOptions = {
149  success: (ret: ProximityResponse) => {
150    console.info('Succeeded in subscribing. Get data distance:' + ret.distance);
151  },
152  fail: (data: string, code: number) => {
153    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
154  },
155};
156Sensor.subscribeProximity(subscribeProximityOptions);
157```
158
159> **说明:**
160> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
161
162## Sensor.unsubscribeProximity
163
164unsubscribeProximity(): void
165
166取消订阅距离感应。
167
168**系统能力**:SystemCapability.Sensors.Sensor.Lite
169
170**示例**:
171
172```ts
173Sensor.unsubscribeProximity();
174```
175
176## Sensor.subscribeLight
177
178 subscribeLight(options: SubscribeLightOptions): void
179
180订阅环境光线感应数据变化。再次调用时,会覆盖前一次调用效果,即仅最后一次调用生效。
181
182**系统能力**:SystemCapability.Sensors.Sensor.Lite
183
184**参数**:
185
186| 参数名  | 类型                                            | 必填 | 说明                               |
187| ------- | ----------------------------------------------- | ---- | ---------------------------------- |
188| options | [SubscribeLightOptions](#subscribelightoptions) | 是   | 当环境光传感器数据发生变化时调用。 |
189
190**示例**:
191
192```ts
193import { Sensor, LightResponse, SubscribeLightOptions } from '@kit.SensorServiceKit';
194
195let subscribeLightOptions: SubscribeLightOptions = {
196  success: (ret: LightResponse) => {
197    console.info('Succeeded in subscribing. Get data intensity:' + ret.intensity);
198  },
199  fail: (data: string, code: number) => {
200    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
201  },
202};
203Sensor.subscribeLight(subscribeLightOptions);
204```
205
206> **说明:**
207> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
208
209## Sensor.unsubscribeLight
210
211unsubscribeLight(): void
212
213取消订阅环境光线感应。
214
215**系统能力**:SystemCapability.Sensors.Sensor.Lite
216
217**示例**:
218
219```ts
220Sensor.unsubscribeLight();
221```
222
223## Sensor.subscribeStepCounter
224
225 subscribeStepCounter(options: SubscribeStepCounterOptions): void
226
227订阅计步传感器数据变化。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
228
229**系统能力**:SystemCapability.Sensors.Sensor.Lite
230
231**需要权限**:ohos.permission.ACTIVITY_MOTION
232
233**参数**:
234
235| 参数名  | 类型                                                        | 必填 | 说明                                   |
236| ------- | ----------------------------------------------------------- | ---- | -------------------------------------- |
237| options | [SubscribeStepCounterOptions](#subscribestepcounteroptions) | 是   | 当步进计数器传感器数据发生变化时调用。 |
238
239**示例**:
240
241```ts
242import { Sensor, StepCounterResponse, SubscribeStepCounterOptions } from '@kit.SensorServiceKit';
243
244let subscribeStepCounterOptions: SubscribeStepCounterOptions = {
245  success: (ret: StepCounterResponse) => {
246    console.info('Succeeded in subscribing. Get step value:' + ret.steps);
247  },
248  fail: (data: string, code: number) => {
249    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
250  },
251};
252Sensor.subscribeStepCounter(subscribeStepCounterOptions);
253```
254
255> **说明:**
256> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
257
258## Sensor.unsubscribeStepCounter
259
260unsubscribeStepCounter(): void
261
262取消订阅计步传感器。
263
264**系统能力**:SystemCapability.Sensors.Sensor.Lite
265
266**需要权限**:ohos.permission.ACTIVITY_MOTION
267
268**示例**:
269
270```ts
271Sensor.unsubscribeStepCounter();
272```
273
274
275## Sensor.subscribeBarometer
276
277subscribeBarometer(options: SubscribeBarometerOptions): void
278
279订阅气压计传感器数据变化。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
280
281**系统能力**:SystemCapability.Sensors.Sensor.Lite
282
283**参数**:
284
285| 参数名  | 类型                                                    | 必填 | 说明                               |
286| ------- | ------------------------------------------------------- | ---- | ---------------------------------- |
287| options | [SubscribeBarometerOptions](#subscribebarometeroptions) | 是   | 当气压计传感器数据发生变化时调用。 |
288
289**示例**:
290
291```ts
292import { Sensor, BarometerResponse, SubscribeBarometerOptions } from '@kit.SensorServiceKit';
293
294let subscribeBarometerOptions: SubscribeBarometerOptions = {
295  success: (ret: BarometerResponse) => {
296    console.info('Succeeded in subscribing. Get data value:' + ret.pressure);
297  },
298  fail: (data: string, code: number) => {
299    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
300  },
301};
302Sensor.subscribeBarometer(subscribeBarometerOptions);
303```
304
305> **说明:**
306> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
307
308
309## Sensor.unsubscribeBarometer
310
311unsubscribeBarometer(): void
312
313取消订阅气压计传感器。
314
315**系统能力**:SystemCapability.Sensors.Sensor.Lite
316
317**示例**:
318
319```ts
320Sensor.unsubscribeBarometer();
321```
322
323
324## Sensor.subscribeHeartRate
325
326 subscribeHeartRate(options: SubscribeHeartRateOptions): void
327
328订阅心率传感器数据变化。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
329
330**系统能力**:SystemCapability.Sensors.Sensor.Lite
331
332**需要权限**:ohos.permission.READ_HEALTH_DATA
333
334**参数**:
335
336| 参数名  | 类型                                                    | 必填 | 说明                             |
337| ------- | ------------------------------------------------------- | ---- | -------------------------------- |
338| options | [SubscribeHeartRateOptions](#subscribeheartrateoptions) | 是   | 当心率传感器数据发生变化时调用。 |
339
340**示例**:
341
342```ts
343import { Sensor, HeartRateResponse, SubscribeHeartRateOptions } from '@kit.SensorServiceKit';
344
345let subscribeHeartRateOptions: SubscribeHeartRateOptions = {
346  success: (ret: HeartRateResponse) => {
347    console.info('Succeeded in subscribing. Get heartRate value:' + ret.heartRate);
348  },
349  fail: (data: string, code: number) => {
350    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
351  },
352};
353Sensor.subscribeHeartRate(subscribeHeartRateOptions);
354```
355
356> **说明:**
357> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
358
359
360## Sensor.unsubscribeHeartRate
361
362unsubscribeHeartRate(): void
363
364取消订阅心率传感器。
365
366**系统能力**:SystemCapability.Sensors.Sensor.Lite
367
368**需要权限**:ohos.permission.READ_HEALTH_DATA
369
370**示例**:
371
372```ts
373Sensor.unsubscribeHeartRate();
374```
375
376## Sensor.subscribeOnBodyState
377
378 subscribeOnBodyState(options: SubscribeOnBodyStateOptions): void
379
380订阅设备佩戴状态。针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效。
381
382**系统能力**:SystemCapability.Sensors.Sensor.Lite
383
384**参数**:
385
386| 参数名  | 类型                                                        | 必填 | 说明                   |
387| ------- | ----------------------------------------------------------- | ---- | ---------------------- |
388| options | [SubscribeOnBodyStateOptions](#subscribeonbodystateoptions) | 是   | 当穿着状态改变时调用。 |
389
390**示例**:
391
392```ts
393import { Sensor, OnBodyStateResponse, SubscribeOnBodyStateOptions } from '@kit.SensorServiceKit';
394
395let subscribeOnBodyStateOptions: SubscribeOnBodyStateOptions = {
396  success: (ret: OnBodyStateResponse) => {
397    console.info('Succeeded in subscribing. Get on-body state value:' + ret.value);
398  },
399  fail: (data: string, code: number) => {
400    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
401  },
402};
403Sensor.subscribeOnBodyState(subscribeOnBodyStateOptions);
404```
405
406> **说明:**
407> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
408
409## Sensor.unsubscribeOnBodyState
410
411unsubscribeOnBodyState(): void
412
413取消订阅设备佩戴状态。
414
415**系统能力**:SystemCapability.Sensors.Sensor.Lite
416
417**示例**:
418
419```ts
420Sensor.unsubscribeOnBodyState();
421```
422
423## Sensor.getOnBodyState
424
425 getOnBodyState(options: GetOnBodyStateOptions): void
426
427获取设备佩戴状态。
428
429**系统能力**:SystemCapability.Sensors.Sensor.Lite
430
431**参数**:
432
433| 参数名  | 类型                                            | 必填 | 说明                       |
434| ------- | ----------------------------------------------- | ---- | -------------------------- |
435| options | [GetOnBodyStateOptions](#getonbodystateoptions) | 是   | 获取传感器所在设备穿戴状态时调用。 |
436
437**示例**:
438
439```ts
440import { Sensor, OnBodyStateResponse, GetOnBodyStateOptions } from '@kit.SensorServiceKit';
441
442let getOnBodyStateOptions: GetOnBodyStateOptions = {
443  success: (ret: OnBodyStateResponse) => {
444    console.info('Succeeded in subscribing. On body state: ' + ret.value);
445  },
446  fail: (data: string, code: number) => {
447    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
448  },
449};
450Sensor.getOnBodyState(getOnBodyStateOptions);
451```
452
453## Sensor.subscribeDeviceOrientation<sup>6+</sup>
454
455 subscribeDeviceOrientation(options: SubscribeDeviceOrientationOptions): void
456
457观察设备方向传感器数据变化。
458
459针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效;针对同一个方法内,不支持多次调用。
460
461**系统能力**:SystemCapability.Sensors.Sensor.Lite
462
463**参数**:
464
465| 参数名  | 类型                                                         | 必填 | 说明                                             |
466| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------ |
467| options | [SubscribeDeviceOrientationOptions](#subscribedeviceorientationoptions6) | 是   | 用于监听设备方向传感器数据的回调函数的执行频率。 |
468
469**示例**:
470
471```ts
472import { Sensor, DeviceOrientationResponse, SubscribeDeviceOrientationOptions } from '@kit.SensorServiceKit';
473
474let subscribeDeviceOrientationOptions: SubscribeDeviceOrientationOptions = {
475  interval: 'normal',
476  success: (ret: DeviceOrientationResponse) => {
477    console.info('Succeeded in subscribing. Alpha data: ' + ret.alpha);
478    console.info('Succeeded in subscribing. Beta data: ' + ret.beta);
479    console.info('Succeeded in subscribing. Gamma data: ' + ret.gamma);
480  },
481  fail: (data: string, code: number) => {
482    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
483  }
484};
485Sensor.subscribeDeviceOrientation(subscribeDeviceOrientationOptions);
486```
487
488> **说明:**
489> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
490
491## Sensor.unsubscribeDeviceOrientation<sup>6+</sup>
492
493unsubscribeDeviceOrientation(): void
494
495取消订阅设备方向传感器数据。
496
497**系统能力**:SystemCapability.Sensors.Sensor.Lite
498
499**示例**:
500
501```ts
502Sensor.unsubscribeDeviceOrientation();
503```
504
505## Sensor.subscribeGyroscope<sup>6+</sup>
506
507 subscribeGyroscope(options: SubscribeGyroscopeOptions): void
508
509观察陀螺仪传感器数据变化。
510
511针对同一个应用,多次点击调用时,会覆盖前面的调用效果,即仅最后一次调用生效;针对同一个方法内,不支持多次调用。
512
513**系统能力**:SystemCapability.Sensors.Sensor.Lite
514
515**需要权限**:ohos.permission.GYROSCOPE,该权限为系统权限
516
517**参数**:
518
519| 参数名  | 类型                                                     | 必填 | 说明                                           |
520| ------- | -------------------------------------------------------- | ---- | ---------------------------------------------- |
521| options | [SubscribeGyroscopeOptions](#subscribegyroscopeoptions6) | 是   | 用于侦听陀螺仪传感器数据的回调函数的执行频率。 |
522
523**示例**:
524
525```ts
526import { Sensor, GyroscopeResponse, SubscribeGyroscopeOptions } from '@kit.SensorServiceKit';
527
528let subscribeGyroscopeOptions: SubscribeGyroscopeOptions = {
529  interval: 'normal',
530  success: (ret: GyroscopeResponse) => {
531    console.info('Succeeded in subscribing. X-axis data: ' + ret.x);
532    console.info('Succeeded in subscribing. Y-axis data: ' + ret.y);
533    console.info('Succeeded in subscribing. Z-axis data: ' + ret.z);
534  },
535  fail: (data: string, code: number) => {
536    console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
537  }
538};
539Sensor.subscribeGyroscope(subscribeGyroscopeOptions);
540```
541
542> **说明:**
543> 建议在页面销毁时,即onDestroy回调中,取消数据订阅,避免不必要的性能开销。
544
545## Sensor.unsubscribeGyroscope<sup>6+</sup>
546
547unsubscribeGyroscope(): void
548
549取消订阅陀螺仪传感器数据。
550
551**系统能力**:SystemCapability.Sensors.Sensor.Lite
552
553**需要权限**:ohos.permission.GYROSCOPE,该权限为系统权限
554
555**示例**:
556
557```ts
558Sensor.unsubscribeGyroscope();
559```
560
561## subscribeAccelerometerOptions
562
563用于监听加速度传感器数据的回调函数的执行频率。
564
565**系统能力**:SystemCapability.Sensors.Sensor.Lite
566
567**需要权限**:ohos.permission.ACCELEROMETER
568
569| 名称     | 类型                                            | 必填 | 说明                                                         |
570| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ |
571| interval | string                                          | 是   | 频率参数,加速度的回调函数执行频率。<br/>默认为normal,可选值有:<br/>game:极高的回调频率,20ms/次,适用于游戏。<br/>ui:较高的回调频率,60ms/次,适用于UI更新。<br/>normal:普通的回调频率,200ms/次,低功耗。 |
572| success  | [AccelerometerResponse](#accelerometerresponse) | 是   | 感应到加速度数据变化后的回调函数。                           |
573| fail     | Function                                        | 否   | 接口调用失败的回调函数。                                     |
574
575## AccelerometerResponse
576
577感应到加速度数据变化后的回调函数。
578
579**系统能力**:SystemCapability.Sensors.Sensor.Lite
580
581**需要权限**:ohos.permission.ACCELEROMETER
582
583| 名称 | 类型   | 必填 | 说明          |
584| ---- | ------ | ---- | ------------- |
585| x    | number | 是   | x轴的加速度。 |
586| y    | number | 是   | y轴的加速度。 |
587| z    | number | 是   | z轴的加速度。 |
588
589## SubscribeCompassOptions
590
591当罗盘传感器数据发生变化时调用。
592
593**系统能力**:SystemCapability.Sensors.Sensor.Lite
594
595| 名称    | 类型                                | 必填 | 说明                           |
596| ------- | ----------------------------------- | ---- | ------------------------------ |
597| success | [CompassResponse](#compassresponse) | 是   | 罗盘数据改变后触发的回调函数。 |
598| fail    | Function                            | 否   | 接口调用失败的回调函数。       |
599
600## CompassResponse
601
602罗盘数据改变后触发的回调函数。
603
604**系统能力**:SystemCapability.Sensors.Sensor.Lite
605
606| 名称      | 类型   | 必填 | 说明                 |
607| --------- | ------ | ---- | -------------------- |
608| direction | number | 是   | 设备面对的方向度数。 |
609
610## SubscribeProximityOptions
611
612当距离传感器数据发生变化时调用。
613
614**系统能力**:SystemCapability.Sensors.Sensor.Lite
615
616| 名称    | 类型                                    | 必填 | 说明                               |
617| ------- | --------------------------------------- | ---- | ---------------------------------- |
618| success | [ProximityResponse](#proximityresponse) | 是   | 距离感应数据改变后调用的回调函数。 |
619| fail    | Function                                | 否   | 接口调用失败的回调函数。           |
620
621## ProximityResponse
622
623距离感应数据改变后调用的回调函数。
624
625**系统能力**:SystemCapability.Sensors.Sensor.Lite
626
627| 名称     | 类型   | 必填 | 说明                                       |
628| -------- | ------ | ---- | ------------------------------------------ |
629| distance | number | 是   | 可见物体相对于设备显示屏的接近或远离状态。 |
630
631## SubscribeLightOptions
632
633当环境光传感器数据发生变化时调用。
634
635**系统能力**:SystemCapability.Sensors.Sensor.Lite
636
637| 名称    | 类型                            | 必填 | 说明                           |
638| ------- | ------------------------------- | ---- | ------------------------------ |
639| success | [LightResponse](#lightresponse) | 是   | 光线感应数据改变后的回调函数。 |
640| fail    | Function                        | 否   | 接口调用失败的回调函数。       |
641
642## LightResponse
643
644光线感应数据改变后的回调函数。
645
646**系统能力**:SystemCapability.Sensors.Sensor.Lite
647
648| 名称      | 类型   | 必填 | 说明                  |
649| --------- | ------ | ---- | --------------------- |
650| intensity | number | 是   | 光线强度,单位为lux。 |
651
652## SubscribeStepCounterOptions
653
654当步进计数器传感器数据发生变化时调用。
655
656**需要权限**:ohos.permission.ACTIVITY_MOTION
657
658**系统能力**:SystemCapability.Sensors.Sensor.Lite
659
660| 名称    | 类型                                        | 必填 | 说明                             |
661| ------- | ------------------------------------------- | ---- | -------------------------------- |
662| success | [StepCounterResponse](#stepcounterresponse) | 是   | 计步传感器数据改变后的回调函数。 |
663| fail    | Function                                    | 否   | 接口调用失败的回调函数。         |
664
665## StepCounterResponse
666
667计步传感器数据改变后的回调函数。
668
669**需要权限**:ohos.permission.ACTIVITY_MOTION
670
671**系统能力**:SystemCapability.Sensors.Sensor.Lite
672
673| 名称  | 类型   | 必填 | 说明                             |
674| ----- | ------ | ---- | -------------------------------- |
675| steps | number | 是   | 计步传感器重启后累计记录的步数。 |
676
677## SubscribeBarometerOptions
678
679当气压计传感器数据发生变化时调用。
680
681**系统能力**:SystemCapability.Sensors.Sensor.Lite
682
683| 名称    | 类型                                    | 必填 | 说明                             |
684| ------- | --------------------------------------- | ---- | -------------------------------- |
685| success | [BarometerResponse](#barometerresponse) | 是   | 气压计传感器数据改变后的回调函数。 |
686| fail    | Function                                | 否   | 接口调用失败的回调函数。         |
687
688## BarometerResponse
689
690气压计传感器数据改变后的回调函数。
691
692**系统能力**:SystemCapability.Sensors.Sensor.Lite
693
694| 名称     | 类型   | 必填 | 说明                   |
695| -------- | ------ | ---- | ---------------------- |
696| pressure | number | 是   | 气压值,单位:帕斯卡。 |
697
698## SubscribeHeartRateOptions
699
700当心率传感器数据发生变化时调用。
701
702**需要权限**:ohos.permission.READ_HEALTH_DATA
703
704**系统能力**:SystemCapability.Sensors.Sensor.Lite
705
706| 名称    | 类型                                    | 必填 | 说明                                            |
707| ------- | --------------------------------------- | ---- | ----------------------------------------------- |
708| success | [HeartRateResponse](#heartrateresponse) | 是   | 心率传感器数据改变后的回调函数,默认频率5s/次。 |
709| fail    | Function                                | 否   | 接口调用失败的回调函数。                        |
710
711## HeartRateResponse
712
713心率传感器数据改变后的回调函数,默认频率5s/次。
714
715**需要权限**:ohos.permission.READ_HEALTH_DATA
716
717**系统能力**:SystemCapability.Sensors.Sensor.Lite
718
719| 名称      | 类型   | 必填 | 说明     |
720| --------- | ------ | ---- | -------- |
721| heartRate | number | 是   | 心率值。 |
722
723## SubscribeOnBodyStateOptions
724
725当传感器所在设备穿戴状态改变时调用,穿戴状态分为已穿戴和未穿戴。
726
727**系统能力**:SystemCapability.Sensors.Sensor.Lite
728
729| 名称    | 类型                                        | 必填 | 说明                       |
730| ------- | ------------------------------------------- | ---- | -------------------------- |
731| success | [OnBodyStateResponse](#onbodystateresponse) | 是   | 传感器所在设备穿戴状态改变后的回调函数。 |
732| fail    | Function                                    | 否   | 接口调用失败的回调函数。   |
733
734## OnBodyStateResponse
735
736传感器所在设备是否穿戴。
737
738**系统能力**:SystemCapability.Sensors.Sensor.Lite
739
740| 名称  | 类型    | 必填 | 说明                                               |
741| ----- | ------- | ---- | -------------------------------------------------- |
742| value | boolean | 是   | 是否已佩戴设备,当返回true表示已佩戴,否则未佩戴。 |
743
744## GetOnBodyStateOptions
745
746 获取传感器所在设备穿戴状态时调用。
747
748**系统能力**:SystemCapability.Sensors.Sensor.Lite
749
750| 名称     | 类型                                        | 必填 | 说明                     |
751| -------- | ------------------------------------------- | ---- | ------------------------ |
752| success  | [OnBodyStateResponse](#onbodystateresponse) | 是   | 接口调用成功的回调函数。 |
753| fail     | Function                                    | 否   | 接口调用失败的回调函数。 |
754| complete | Function                                    | 否   | 接口调用结束的回调函数。 |
755
756## SubscribeDeviceOrientationOptions<sup>6+</sup>
757
758用于监听设备方向传感器数据的回调函数的执行频率。
759
760**系统能力**:SystemCapability.Sensors.Sensor.Lite
761
762| 名称     | 类型                                                     | 必填 | 说明                                                         |
763| -------- | -------------------------------------------------------- | ---- | ------------------------------------------------------------ |
764| interval | string                                                   | 是   | 频率参数,设备方向传感器的回调函数执行频率。<br/>默认为normal,可选值有:<br/>-&nbsp;game:极高的回调频率,20ms/次,适用于游戏。<br/>-&nbsp;ui:较高的回调频率,60ms/次,适用于UI更新。<br/>-&nbsp;normal:普通的回调频率,200ms/次,低功耗。 |
765| success  | [DeviceOrientationResponse](#deviceorientationresponse6) | 是   | 感应到设备方向传感器数据变化后的回调函数。                   |
766| fail     | Function                                                 | 否   | 接口调用失败的回调函数。                                     |
767
768## DeviceOrientationResponse<sup>6+</sup>
769
770感应到设备方向传感器数据变化后的回调函数。
771
772**系统能力**:SystemCapability.Sensors.Sensor.Lite
773
774| 名称  | 类型   | 必填 | 说明                                                         |
775| ----- | ------ | ---- | ------------------------------------------------------------ |
776| alpha | number | 是   | 当设备坐标&nbsp;X/Y&nbsp;和地球&nbsp;X/Y&nbsp;重合时,绕着&nbsp;Z&nbsp;轴转动的夹角为&nbsp;alpha。 |
777| beta  | number | 是   | 当设备坐标&nbsp;Y/Z&nbsp;和地球&nbsp;Y/Z&nbsp;重合时,绕着&nbsp;X&nbsp;轴转动的夹角为&nbsp;beta。 |
778| gamma | number | 是   | 当设备&nbsp;X/Z&nbsp;和地球&nbsp;X/Z&nbsp;重合时,绕着&nbsp;Y&nbsp;轴转动的夹角为&nbsp;gamma。 |
779
780## SubscribeGyroscopeOptions<sup>6+</sup>
781
782用于侦听陀螺仪传感器数据的回调函数的执行频率。
783
784**需要权限**:ohos.permission.GYROSCOPE
785
786**系统能力**:SystemCapability.Sensors.Sensor.Lite
787
788| 名称     | 类型                                     | 必填 | 说明                                                         |
789| -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
790| interval | string                                   | 是   | 频率参数,陀螺仪的回调函数执行频率。<br/>默认为normal,可选值有:<br/>game:极高的回调频率,20ms/次,适用于游戏。<br/>ui:较高的回调频率,60ms/次,适用于UI更新。<br/>normal:普通的回调频率,200ms/次,低功耗。 |
791| success  | [GyroscopeResponse](#gyroscoperesponse6) | 是   | 感应到陀螺仪数据变化后的回调函数。                           |
792| fail     | Function                                 | 否   | 接口调用失败的回调函数。                                     |
793
794## GyroscopeResponse<sup>6+</sup>
795
796感应到陀螺仪传感器数据变化后的回调函数。
797
798**需要权限**:ohos.permission.GYROSCOPE
799
800**系统能力**:SystemCapability.Sensors.Sensor.Lite
801
802| 名称 | 类型   | 必填 | 说明              |
803| ---- | ------ | ---- | ----------------- |
804| x    | number | 是   | x轴的旋转角速度。 |
805| y    | number | 是   | y轴的旋转角速度。 |
806| z    | number | 是   | z轴的旋转角速度。 |