js-apis-system-sensor.md 19.2 KB
Newer Older
1 2
# Sensor

W
wusongqing 已提交
3
The **Sensor** module provides APIs for querying the sensor list, subscribing to or unsubscribing from sensor data, and executing control commands.
4

W
wusongqing 已提交
5 6 7 8 9 10
The sensors are classified into the following categories based on their functions: motion, environment, orientation, light, body, and other categories (such as Hall effect sensors). Each category includes different sensor types. A sensor type may be a single hardware sensor or a composite of multiple hardware sensors.


> **NOTE**
>
> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.sensor`](js-apis-sensor.md) instead.
11 12 13 14 15 16 17 18 19 20 21 22 23
> - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - This module requires hardware support and can only be debugged on real devices.


## Modules to Import


```
import sensor from '@system.sensor';
```

## Error Codes

H
HelloCrease 已提交
24 25 26
| Error Code | Description                              |
| ---------- | ---------------------------------------- |
| 900        | The current device does not support the corresponding sensor. |
27 28 29 30 31 32 33 34 35 36 37 38 39

## sensor.subscribeAccelerometer

subscribeAccelerometer(Object): void

Subscribes to data changes of the acceleration sensor. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.ACCELEROMETER (a system permission)

**Parameters**

H
HelloCrease 已提交
40 41 42 43 44
| Name     | Type     | Mandatory | Description                              |
| -------- | -------- | --------- | ---------------------------------------- |
| interval | string   | Yes       | Execution frequency of the callback for returning the acceleration sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios. |
| success  | Function | Yes       | Called when the acceleration sensor data changes. |
| fail     | Function | No        | Callback upon failure.                   |
45 46 47

Return values of the success callback

H
HelloCrease 已提交
48 49 50 51 52
| Name | Type   | Description                 |
| ---- | ------ | --------------------------- |
| x    | number | Acceleration on the x-axis. |
| y    | number | Acceleration on the y-axis. |
| z    | number | Acceleration on the z-axis. |
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

**Example**

```
sensor.subscribeAccelerometer({
  interval: 'normal',
  success: function(ret) {
    console.log('X-axis data: ' + ret.x);
    console.log('Y-axis data: ' + ret.y);
    console.log('Z-axis data: ' + ret.z);
  },
  fail: function(data, code) {
    console.error('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
70 71
> **NOTE**
>
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeAccelerometer

unsubscribeAccelerometer(): void

Unsubscribes from data changes of the acceleration sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.ACCELEROMETER (a system permission)

**Example**

```
sensor.unsubscribeAccelerometer();
```

## sensor.subscribeCompass

subscribeCompass(Object): void

Subscribes to data changes of the compass sensor. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
100 101 102 103
| Name    | Type     | Mandatory | Description                              |
| ------- | -------- | --------- | ---------------------------------------- |
| success | Function | Yes       | Called when the compass sensor data changes. |
| fail    | Function | No        | Callback upon failure.                   |
104 105 106

Return values of the success callback

H
HelloCrease 已提交
107 108 109
| Name      | Type   | Description                          |
| --------- | ------ | ------------------------------------ |
| direction | number | Direction of the device, in degrees. |
110 111 112 113 114 115 116 117 118 119 120 121 122 123

**Example**

```
sensor.subscribeCompass({
  success: function(ret) {
    console.log('get data direction:' + ret.direction);
  },
  fail: function(data, code) {
    console.error('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
124 125
> **NOTE**
>
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeCompass

unsubscribeCompass(): void

Unsubscribes from data changes of the compass sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Example**

```
sensor.unsubscribeCompass();
```

## sensor.subscribeProximity

subscribeProximity(Object): void

Subscribes to data changes of the proximity sensor. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
152 153 154 155
| Name    | Type     | Mandatory | Description                              |
| ------- | -------- | --------- | ---------------------------------------- |
| success | Function | Yes       | Called when the proximity sensor data changes. |
| fail    | Function | No        | Callback upon failure.                   |
156 157 158

Return values of the success callback

H
HelloCrease 已提交
159 160 161
| Name     | Type   | Description                              |
| -------- | ------ | ---------------------------------------- |
| distance | number | Distance between a visible object and the device screen. |
162 163 164 165 166 167 168 169 170 171 172 173 174 175

**Example**

```
sensor.subscribeProximity({
  success: function(ret) {
    console.log('get data distance:' + ret.distance);
  },
  fail: function(data, code) {
    console.error('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
176 177
> **NOTE**
>
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeProximity

unsubscribeProximity(): void

Unsubscribes from data changes of the proximity sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Example**

```
sensor.unsubscribeProximity();
```

## sensor.subscribeLight

sensor.subscribeLight(Object): void

Subscribes to data changes of the ambient light sensor. If this API is called multiple times, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
204 205 206 207
| Name    | Type     | Mandatory | Description                              |
| ------- | -------- | --------- | ---------------------------------------- |
| success | Function | Yes       | Called when the ambient light sensor data changes |
| fail    | Function | No        | Callback upon failure.                   |
208 209 210

Return values of the success callback

H
HelloCrease 已提交
211 212 213
| Name      | Type   | Description              |
| --------- | ------ | ------------------------ |
| intensity | number | Light intensity, in lux. |
214 215 216 217 218 219 220 221 222 223 224 225 226 227

**Example**

```
sensor.subscribeLight({
  success: function(ret) {
    console.log('get data intensity:' + ret.intensity);
  },
  fail: function(data, code) {
    console.error('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
228 229
> **NOTE**
>
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeLight

unsubscribeLight(): void

Unsubscribes from data changes of the ambient light sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Example**

```
sensor.unsubscribeLight();
```

## sensor.subscribeStepCounter

subscribeStepCounter(Object): void

Subscribes to data changes of the step counter sensor. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.ACTIVITY_MOTION

**Parameters**

H
HelloCrease 已提交
258 259 260 261
| Name    | Type     | Mandatory | Description                              |
| ------- | -------- | --------- | ---------------------------------------- |
| success | Function | Yes       | Called when the step counter sensor data changes. |
| fail    | Function | No        | Callback upon failure.                   |
262 263 264

Return values of the success callback

H
HelloCrease 已提交
265 266 267
| Name  | Type   | Description                              |
| ----- | ------ | ---------------------------------------- |
| steps | number | Number of counted steps after the sensor is restarted.<br> |
268 269 270 271 272 273 274 275 276 277 278 279 280 281

**Example**

```
sensor.subscribeStepCounter({
  success: function(ret) {
    console.log('get step value:' + ret.steps);
  },
  fail: function(data, code) {
    console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
282 283
> **NOTE**
>
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeStepCounter

unsubscribeStepCounter(): void

Unsubscribes from data changes of the step counter sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.ACTIVITY_MOTION

**Example**

```
sensor.unsubscribeStepCounter();
```


## sensor.subscribeBarometer

H
HelloCrease 已提交
305
subscribeBarometer(Object): void
306 307 308 309 310 311 312

Subscribes to data changes of the barometer sensor. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
313 314 315 316
| Name    | Type     | Mandatory | Description                              |
| ------- | -------- | --------- | ---------------------------------------- |
| success | Function | Yes       | Called when the barometer sensor data changes. |
| fail    | Function | No        | Callback upon failure.                   |
317 318 319

Return values of the success callback

H
HelloCrease 已提交
320 321 322
| Name     | Type   | Description          |
| -------- | ------ | -------------------- |
| pressure | number | Pressure, in pascal. |
323 324 325 326 327 328 329 330 331 332 333 334 335 336

**Example**

```
sensor.subscribeBarometer({
  success: function(ret) {
    console.log('get data value:' + ret.pressure);
  },
  fail: function(data, code) {
    console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
337 338
> **NOTE**
>
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.


## sensor.unsubscribeBarometer

unsubscribeBarometer(): void

Unsubscribes from data changes of the barometer sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Example**

```
sensor.unsubscribeBarometer();
```


## sensor.subscribeHeartRate

subscribeHeartRate(Object): void

Subscribes to data changes of the heart rate sensor. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.READ_HEALTH_DATA

**Parameters**

H
HelloCrease 已提交
369 370 371 372
| Name    | Type     | Mandatory | Description                              |
| ------- | -------- | --------- | ---------------------------------------- |
| success | Function | Yes       | Called when the heart rate sensor data changes. This callback is invoked every five seconds. |
| fail    | Function | No        | Callback upon failure.                   |
373 374 375

Return values of the success callback

H
HelloCrease 已提交
376 377 378
| Name      | Type   | Description |
| --------- | ------ | ----------- |
| heartRate | number | Heart rate. |
379 380 381 382 383 384 385 386 387 388 389 390 391 392

**Example**

```
sensor.subscribeHeartRate({
  success: function(ret) {
    console.log('get heartrate value:' + ret.heartRate);
  },
  fail: function(data, code) {
    console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
393 394
> **NOTE**
>
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.


## sensor.unsubscribeHeartRate

unsubscribeHeartRate(): void

Unsubscribes from data changes of the heart rate sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.READ_HEALTH_DATA

**Example**

```
sensor.unsubscribeHeartRate();
```

## sensor.subscribeOnBodyState

subscribeOnBodyState(Object): void

Subscribes to changes of the wearing state of a wearable device. If this API is called multiple times for the same application, the last call takes effect.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
424 425 426 427
| Name    | Type     | Mandatory | Description                            |
| ------- | -------- | --------- | -------------------------------------- |
| success | Function | Yes       | Called when the wearing state changes. |
| fail    | Function | No        | Callback upon failure.                 |
428 429 430

Return values of the success callback

H
HelloCrease 已提交
431 432 433
| Name  | Type    | Description                          |
| ----- | ------- | ------------------------------------ |
| value | boolean | Whether the wearable device is worn. |
434 435 436 437 438 439 440 441 442 443 444 445 446 447

**Example**

```
sensor.subscribeOnBodyState({
  success: function(ret) {
    console.log('get on-body state value:' + ret.value);
  },
  fail: function(data, code) {
    console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

W
wusongqing 已提交
448 449
> **NOTE**
>
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeOnBodyState

unsubscribeOnBodyState(): void

Unsubscribes from changes of the wearing state of a wearable device.

**System capability**: SystemCapability.Sensors.Sensor

**Example**

```
sensor.unsubscribeOnBodyState();
```

## sensor.getOnBodyState

getOnBodyState(Object): void

Obtains the wearing state of a wearable device.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
476 477 478 479 480
| Name     | Type     | Mandatory | Description                            |
| -------- | -------- | --------- | -------------------------------------- |
| success  | Function | No        | Callback upon success.                 |
| fail     | Function | No        | Callback upon failure.                 |
| complete | Function | No        | Called when the execution is complete. |
481 482 483

Return values of the success callback

H
HelloCrease 已提交
484 485 486
| Name  | Type    | Description                          |
| ----- | ------- | ------------------------------------ |
| value | boolean | Whether the wearable device is worn. |
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

**Example**

```
sensor.getOnBodyState({
  success: function(ret) {
    console.log('on body state: ' + ret.value);
  },
  fail: function(data, code) {
    console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
  },
});
```

## sensor.subscribeDeviceOrientation<sup>6+</sup>

subscribeDeviceOrientation(interval: string, success: (data: DeviceOrientationResponse), fail?: (data: string, code: number)): void

Subscribes to data changes of the device orientation sensor.

If this API is called multiple times for the same application, the last call takes effect. However, this API cannot be called multiple times in one click event.

**System capability**: SystemCapability.Sensors.Sensor

**Parameters**

H
HelloCrease 已提交
513 514 515 516 517
| Name     | Type     | Mandatory | Description                              |
| -------- | -------- | --------- | ---------------------------------------- |
| interval | string   | Yes       | Interval at which the callback is invoked to return the device orientation sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios. |
| success  | Function | Yes       | Called when the device orientation sensor data changes. |
| fail     | Function | No        | Callback upon failure.                   |
518 519

 Return values of the success callback
H
HelloCrease 已提交
520
| Name  | Type   | Description                              |
H
HelloCrease 已提交
521
| ----- | ------ | ---------------------------------------- |
H
HelloCrease 已提交
522 523 524
| alpha | number | Rotation angle around the Z axis when the X/Y axis of the device coincides with the X/Y axis of the earth. |
| beta  | number | Rotation angle around the X axis when the Y/Z axis of the device coincides with the Y/Z axis of the earth. |
| gamma | number | Rotation angle around the Y axis when the X/Z axis of the device coincides with the X/Z axis of the earth. |
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

**Example**

```
sensor.subscribeDeviceOrientation({
  interval: 'normal',
  success: function(ret) {
    console.log('Alpha data: ' + ret.alpha);
    console.log('Beta data: ' + ret.beta);
    console.log('Gamma data: ' + ret.gamma);
  },
  fail: function(data, code) {
    console.error('Subscription failed. Code: ' + code + '; Data: ' + data);
  }
});
```

W
wusongqing 已提交
542 543
> **NOTE**
>
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeDeviceOrientation<sup>6+</sup>

unsubscribeDeviceOrientation(): void

Unsubscribes from data changes of the device orientation sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Example**

```
sensor.unsubscribeDeviceOrientation();
```

## sensor.subscribeGyroscope<sup>6+</sup>

subscribeGyroscope(interval: string, success: (data: GyroscopeResponse), fail?: (data: string, code: number)): void

Subscribes to data changes of the gyroscope sensor.

If this API is called multiple times for the same application, the last call takes effect. However, this API cannot be called multiple times in one click event.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.GYROSCOPE (a system permission)

**Parameters**

H
HelloCrease 已提交
574 575 576 577 578
| Name     | Type     | Mandatory | Description                              |
| -------- | -------- | --------- | ---------------------------------------- |
| interval | string   | Yes       | Interval at which the callback is invoked to return the gyroscope sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios. |
| success  | Function | Yes       | Called when the gyroscope sensor data changes. |
| fail     | Function | No        | Callback upon failure.                   |
579 580 581

Return values of the success callback

H
HelloCrease 已提交
582 583 584 585 586
| Name | Type   | Description                              |
| ---- | ------ | ---------------------------------------- |
| x    | number | Rotation angular velocity of the X axis. |
| y    | number | Rotation angular velocity of the Y axis. |
| z    | number | Rotation angular velocity of the Z axis. |
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

**Example**

```
sensor.subscribeGyroscope({
  interval: 'normal',
  success: function(ret) {
    console.log('X-axis data: ' + ret.x);
    console.log('Y-axis data: ' + ret.y);
    console.log('Z-axis data: ' + ret.z);
  },
  fail: function(data, code) {
    console.error('Subscription failed. Code: ' + code + '; data: ' + data);
  }
});
```

W
wusongqing 已提交
604 605
> **NOTE**
>
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.

## sensor.unsubscribeGyroscope<sup>6+</sup>

unsubscribeGyroscope(): void

Unsubscribes from data changes of the gyroscope sensor.

**System capability**: SystemCapability.Sensors.Sensor

**Required permissions**: ohos.permission.GYROSCOPE (a system permission)

**Example**

```
sensor.unsubscribeGyroscope();
```