js-apis-system-sensor.md 18.4 KB
Newer Older
1
# @system.sensor (Sensor)
2

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

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**

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

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
> **NOTE**
71 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
> 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**

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

Return values of the success callback

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

**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 已提交
123
> **NOTE**
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
> 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**

150 151 152 153
| Name    | Type      | Mandatory  | Description               |
| ------- | -------- | ---- | ----------------- |
| success | Function | Yes   | Called when the proximity sensor data changes.|
| fail    | Function | No   | Callback upon failure.     |
154 155 156

Return values of the success callback

157 158 159
| Name     | Type    | Description                   |
| -------- | ------ | --------------------- |
| distance | number | Distance between a visible object and the device screen.|
160 161 162 163 164 165 166 167 168 169 170 171 172 173

**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 已提交
174
> **NOTE**
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
> 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**

201 202 203 204
| Name    | Type      | Mandatory  | Description             |
| ------- | -------- | ---- | --------------- |
| success | Function | Yes   | Called when the ambient light sensor data changes|
| fail    | Function | No   | Callback upon failure.   |
205 206 207

Return values of the success callback

208 209 210
| Name      | Type    | Description          |
| --------- | ------ | ------------ |
| intensity | number | Light intensity, in lux.|
211 212 213 214 215 216 217 218 219 220 221 222 223 224

**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 已提交
225
> **NOTE**
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
> 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**

254 255 256 257
| Name    | Type      | Mandatory  | Description              |
| ------- | -------- | ---- | ---------------- |
| success | Function | Yes   | Called when the step counter sensor data changes.|
| fail    | Function | No   | Callback upon failure.    |
258 259 260

Return values of the success callback

261 262 263
| Name  | Type    | Description                   |
| ----- | ------ | --------------------- |
| steps | number | Number of counted steps after the sensor is restarted.<br>|
264 265 266 267 268 269 270 271 272 273 274 275 276 277

**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 已提交
278
> **NOTE**
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
> 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 已提交
300
subscribeBarometer(Object): void
301 302 303 304 305 306 307

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**

308 309 310 311
| Name    | Type      | Mandatory  | Description              |
| ------- | -------- | ---- | ---------------- |
| success | Function | Yes   | Called when the barometer sensor data changes.|
| fail    | Function | No   | Callback upon failure.    |
312 313 314

Return values of the success callback

315 316 317
| Name     | Type    | Description         |
| -------- | ------ | ----------- |
| pressure | number | Pressure, in pascal.|
318 319 320 321 322 323 324 325 326 327 328 329 330 331

**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 已提交
332
> **NOTE**
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
> 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**

363 364 365 366
| 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.             |
367 368 369

Return values of the success callback

370 371 372
| Name      | Type    | Description  |
| --------- | ------ | ---- |
| heartRate | number | Heart rate.|
373 374 375 376 377 378 379 380 381 382 383 384 385 386

**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 已提交
387
> **NOTE**
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
> 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**

417 418 419 420
| Name    | Type      | Mandatory  | Description           |
| ------- | -------- | ---- | ------------- |
| success | Function | Yes   | Called when the wearing state changes.|
| fail    | Function | No   | Callback upon failure. |
421 422 423

Return values of the success callback

424 425 426
| Name  | Type     | Description    |
| ----- | ------- | ------ |
| value | boolean | Whether the wearable device is worn.|
427 428 429 430 431 432 433 434 435 436 437 438 439 440

**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 已提交
441
> **NOTE**
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
> 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**

468 469 470 471 472
| 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.|
473 474 475

Return values of the success callback

476 477 478
| Name  | Type     | Description    |
| ----- | ------- | ------ |
| value | boolean | Whether the wearable device is worn.|
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

**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**

505 506 507 508 509
| 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.                            |
510 511

 Return values of the success callback
512
| Name  | Type    | Description                                      |
H
HelloCrease 已提交
513
| ----- | ------ | ---------------------------------------- |
514 515 516
| 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.|
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

**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 已提交
534
> **NOTE**
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
> 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**

565 566 567 568 569
| 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.                            |
570 571 572

Return values of the success callback

573 574 575 576 577
| 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.|
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

**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 已提交
595
> **NOTE**
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
> 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();
```