1# @ohos.sensor (Sensor) 2<!--Kit: Sensor Service Kit--> 3<!--Subsystem: Sensors--> 4<!--Owner: @dilligencer--> 5<!--Designer: @butterls--> 6<!--Tester: @murphy84--> 7<!--Adviser: @hu-zhiqiong--> 8 9The **Sensor** module provides APIs for obtaining the sensor list and subscribing to sensor data. It also provides some common sensor algorithms. 10 11> **NOTE** 12> 13> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. You can call [getSingleSensor](#sensorgetsinglesensor9) to obtain the target sensor before subscribing to the sensor data. Ensure that the **on** and **off** APIs are used in pairs. 14 15 16## Modules to Import 17 18```ts 19import { sensor } from '@kit.SensorServiceKit'; 20``` 21## sensor.on 22 23### ACCELEROMETER<sup>9+</sup> 24 25on(type: SensorId.ACCELEROMETER, callback: Callback<AccelerometerResponse>, options?: Options): void 26 27Subscribes to data of the acceleration sensor. 28 29**Required permissions**: ohos.permission.ACCELEROMETER 30 31**Atomic service API**: This API can be used in atomic services since API version 11. 32 33**System capability**: SystemCapability.Sensors.Sensor 34 35**Parameters** 36 37| Name | Type | Mandatory| Description | 38| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 39| type | [SensorId](#sensorid9).ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER**. | 40| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | Yes | Callback used to report the sensor data, which is an **AccelerometerResponse** object.| 41| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 42 43**Error codes** 44 45For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 46 47| ID| Error Message | 48| -------- | ------------------------------------------------------------ | 49| 201 | Permission denied. | 50| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 51| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 52 53**Example** 54 55```ts 56import { sensor } from '@kit.SensorServiceKit'; 57import { BusinessError } from '@kit.BasicServicesKit'; 58 59// Use try catch to capture possible exceptions. 60try { 61 sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 62 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 63 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 64 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 65 }, { interval: 100000000 }); 66 setTimeout(() => { 67 sensor.off(sensor.SensorId.ACCELEROMETER); 68 }, 500); 69} catch (error) { 70 let e: BusinessError = error as BusinessError; 71 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 72} 73``` 74 75### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 76 77on(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void 78 79Subscribes to data of the uncalibrated acceleration sensor. 80 81**Required permissions**: ohos.permission.ACCELEROMETER 82 83**System capability**: SystemCapability.Sensors.Sensor 84 85**Parameters** 86 87| Name | Type | Mandatory| Description | 88| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 89| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER_UNCALIBRATED**. | 90| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | Yes | Callback used to report the sensor data, which is an **AccelerometerUncalibratedResponse** object.| 91| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 92 93**Error codes** 94 95For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 96 97| ID| Error Message | 98| -------- | ------------------------------------------------------------ | 99| 201 | Permission denied. | 100| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 101| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 102 103**Example** 104 105```ts 106import { sensor } from '@kit.SensorServiceKit'; 107import { BusinessError } from '@kit.BasicServicesKit'; 108 109// Use try catch to capture possible exceptions. 110try { 111 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 112 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 113 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 114 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 115 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 116 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 117 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 118 }, { interval: 100000000 }); 119 setTimeout(() => { 120 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED); 121 }, 500); 122} catch (error) { 123 let e: BusinessError = error as BusinessError; 124 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 125} 126``` 127 128### AMBIENT_LIGHT<sup>9+</sup> 129 130on(type: SensorId.AMBIENT_LIGHT, callback: Callback<LightResponse>, options?: Options): void 131 132Subscribes to data of the ambient light sensor. 133 134**System capability**: SystemCapability.Sensors.Sensor 135 136**Parameters** 137 138| Name | Type | Mandatory| Description | 139| -------- | ----------------------------------------------- | ---- | ----------------------------------------------------------- | 140| type | [SensorId](#sensorid9).AMBIENT_LIGHT | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_LIGHT**. | 141| callback | Callback<[LightResponse](#lightresponse)> | Yes | Callback used to report the sensor data, which is a **LightResponse** object. | 142| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 143 144**Error codes** 145 146For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 147 148| ID| Error Message | 149| -------- | ------------------------------------------------------------ | 150| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 151| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 152 153**Example** 154 155```ts 156import { sensor } from '@kit.SensorServiceKit'; 157import { BusinessError } from '@kit.BasicServicesKit'; 158 159// Use try catch to capture possible exceptions. 160try { 161 sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => { 162 console.info('Succeeded in getting the ambient light intensity: ' + data.intensity); 163 }, { interval: 100000000 }); 164 setTimeout(() => { 165 sensor.off(sensor.SensorId.AMBIENT_LIGHT); 166 }, 500); 167} catch (error) { 168 let e: BusinessError = error as BusinessError; 169 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 170} 171``` 172 173### AMBIENT_TEMPERATURE<sup>9+</sup> 174 175on(type: SensorId.AMBIENT_TEMPERATURE, callback: Callback<AmbientTemperatureResponse>, options?: Options): void 176 177Subscribes to data of the ambient temperature sensor. 178 179**System capability**: SystemCapability.Sensors.Sensor 180 181**Parameters** 182 183| Name | Type | Mandatory| Description | 184| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 185| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_TEMPERATURE**. | 186| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | Yes | Callback used to report the sensor data, which is an **AmbientTemperatureResponse** object.| 187| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 188 189**Error codes** 190 191For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 192 193| ID| Error Message | 194| -------- | ------------------------------------------------------------ | 195| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 196| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 197 198**Example** 199 200```ts 201import { sensor } from '@kit.SensorServiceKit'; 202import { BusinessError } from '@kit.BasicServicesKit'; 203 204// Use try catch to capture possible exceptions. 205try { 206 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 207 console.info('Succeeded in invoking on. Temperature: ' + data.temperature); 208 }, { interval: 100000000 }); 209 setTimeout(() => { 210 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE); 211 }, 500); 212} catch (error) { 213 let e: BusinessError = error as BusinessError; 214 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 215} 216``` 217 218### BAROMETER<sup>9+</sup> 219 220on(type: SensorId.BAROMETER, callback: Callback<BarometerResponse>, options?: Options): void 221 222Subscribes to data of the barometer sensor. 223 224**System capability**: SystemCapability.Sensors.Sensor 225 226**Parameters** 227 228| Name | Type | Mandatory| Description | 229| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 230| type | [SensorId](#sensorid9).BAROMETER | Yes | Sensor type. The value is fixed at **SensorId.BAROMETER**. | 231| callback | Callback<[BarometerResponse](#barometerresponse)> | Yes | Callback used to report the sensor data, which is a **BarometerResponse** object. | 232| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 233 234**Error codes** 235 236For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 237 238| ID| Error Message | 239| -------- | ------------------------------------------------------------ | 240| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 241| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 242 243**Example** 244 245```ts 246import { sensor } from '@kit.SensorServiceKit'; 247import { BusinessError } from '@kit.BasicServicesKit'; 248 249// Use try catch to capture possible exceptions. 250try { 251 sensor.on(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => { 252 console.info('Succeeded in invoking on. Atmospheric pressure: ' + data.pressure); 253 }, { interval: 100000000 }); 254 setTimeout(() => { 255 sensor.off(sensor.SensorId.BAROMETER); 256 }, 500); 257} catch (error) { 258 let e: BusinessError = error as BusinessError; 259 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 260} 261``` 262 263### GRAVITY<sup>9+</sup> 264 265on(type: SensorId.GRAVITY, callback: Callback<GravityResponse>, options?: Options): void 266 267Subscribes to data of the gravity sensor. 268 269**System capability**: SystemCapability.Sensors.Sensor 270 271**Parameters** 272 273| Name | Type | Mandatory| Description | 274| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------------- | 275| type | [SensorId](#sensorid9).GRAVITY | Yes | Sensor type. The value is fixed at **SensorId.GRAVITY**. | 276| callback | Callback<[GravityResponse](#gravityresponse)> | Yes | Callback used to report the sensor data, which is a **GravityResponse** object. | 277| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 278 279**Error codes** 280 281For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 282 283| ID| Error Message | 284| -------- | ------------------------------------------------------------ | 285| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 286| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 287 288**Example** 289 290```ts 291import { sensor } from '@kit.SensorServiceKit'; 292import { BusinessError } from '@kit.BasicServicesKit'; 293 294// Use try catch to capture possible exceptions. 295try { 296 sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 297 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 298 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 299 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 300 }, { interval: 100000000 }); 301 setTimeout(() => { 302 sensor.off(sensor.SensorId.GRAVITY); 303 }, 500); 304} catch (error) { 305 let e: BusinessError = error as BusinessError; 306 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 307} 308``` 309 310### GYROSCOPE<sup>9+</sup> 311 312on(type: SensorId.GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void 313 314Subscribes to data of the gyroscope sensor. 315 316**Required permissions**: ohos.permission.GYROSCOPE 317 318**Atomic service API**: This API can be used in atomic services since API version 11. 319 320**System capability**: SystemCapability.Sensors.Sensor 321 322**Parameters** 323 324| Name | Type | Mandatory| Description | 325| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 326| type | [SensorId](#sensorid9).GYROSCOPE | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE**. | 327| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | Yes | Callback used to report the sensor data, which is a **GyroscopeResponse** object. | 328| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 329 330**Error codes** 331 332For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 333 334| ID| Error Message | 335| -------- | ------------------------------------------------------------ | 336| 201 | Permission denied. | 337| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 338| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 339 340**Example** 341 342```ts 343import { sensor } from '@kit.SensorServiceKit'; 344import { BusinessError } from '@kit.BasicServicesKit'; 345 346// Use try catch to capture possible exceptions. 347try { 348 sensor.on(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => { 349 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 350 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 351 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 352 }, { interval: 100000000 }); 353 setTimeout(() => { 354 sensor.off(sensor.SensorId.GYROSCOPE); 355 }, 500); 356} catch (error) { 357 let e: BusinessError = error as BusinessError; 358 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 359} 360``` 361 362### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 363 364on(type: SensorId.GYROSCOPE_UNCALIBRATED, callback: Callback<GyroscopeUncalibratedResponse>, 365 options?: Options): void 366 367Subscribes to data of the uncalibrated gyroscope sensor. 368 369**Required permissions**: ohos.permission.GYROSCOPE 370 371**System capability**: SystemCapability.Sensors.Sensor 372 373**Parameters** 374 375| Name | Type | Mandatory| Description | 376| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 377| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE_UNCALIBRATED**. | 378| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | Yes | Callback used to report the sensor data, which is a **GyroscopeUncalibratedResponse** object.| 379| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 380 381**Error codes** 382 383For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 384 385| ID| Error Message | 386| -------- | ------------------------------------------------------------ | 387| 201 | Permission denied. | 388| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 389| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 390 391**Example** 392 393```ts 394import { sensor } from '@kit.SensorServiceKit'; 395import { BusinessError } from '@kit.BasicServicesKit'; 396 397// Use try catch to capture possible exceptions. 398try { 399 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 400 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 401 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 402 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 403 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 404 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 405 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 406 }, { interval: 100000000 }); 407 setTimeout(() => { 408 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED); 409 }, 500); 410} catch (error) { 411 let e: BusinessError = error as BusinessError; 412 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 413} 414 415``` 416 417### HALL<sup>9+</sup> 418 419on(type: SensorId.HALL, callback: Callback<HallResponse>, options?: Options): void 420 421Subscribes to data of the Hall effect sensor. 422 423**System capability**: SystemCapability.Sensors.Sensor 424 425**Parameters** 426 427| Name | Type | Mandatory| Description | 428| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 429| type | [SensorId](#sensorid9).HALL | Yes | Sensor type. The value is fixed at **SensorId.HALL**. | 430| callback | Callback<[HallResponse](#hallresponse)> | Yes | Callback used to report the sensor data, which is a **HallResponse** object. | 431| options | [Options](#options) | No | List of optional parameters. The default value is 200,000,000 ns. This parameter is used to set the data reporting frequency when Hall effect events are frequently triggered.| 432 433**Error codes** 434 435For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 436 437| ID| Error Message | 438| -------- | ------------------------------------------------------------ | 439| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 440| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 441 442**Example** 443 444```ts 445import { sensor } from '@kit.SensorServiceKit'; 446import { BusinessError } from '@kit.BasicServicesKit'; 447 448// Use try catch to capture possible exceptions. 449try { 450 sensor.on(sensor.SensorId.HALL, (data: sensor.HallResponse) => { 451 console.info('Succeeded in invoking on. Hall status: ' + data.status); 452 }, { interval: 100000000 }); 453 setTimeout(() => { 454 sensor.off(sensor.SensorId.HALL); 455 }, 500); 456} catch (error) { 457 let e: BusinessError = error as BusinessError; 458 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 459} 460 461``` 462 463### HEART_RATE<sup>9+</sup> 464 465on(type: SensorId.HEART_RATE, callback: Callback<HeartRateResponse>, options?: Options): void 466 467Subscribes to data of the heart rate sensor. 468 469**Required permissions**: ohos.permission.READ_HEALTH_DATA 470 471**System capability**: SystemCapability.Sensors.Sensor 472 473**Parameters** 474 475| Name | Type | Mandatory| Description | 476| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 477| type | [SensorId](#sensorid9).HEART_RATE | Yes | Sensor type. The value is fixed at **SensorId.HEART_RATE**. | 478| callback | Callback<[HeartRateResponse](#heartrateresponse)> | Yes | Callback used to report the sensor data, which is a **HeartRateResponse** object. | 479| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 480 481**Error codes** 482 483For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 484 485| ID| Error Message | 486| -------- | ------------------------------------------------------------ | 487| 201 | Permission denied. | 488| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 489| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 490 491**Example** 492 493```ts 494import { sensor } from '@kit.SensorServiceKit'; 495import { BusinessError } from '@kit.BasicServicesKit'; 496 497// Use try catch to capture possible exceptions. 498try { 499 sensor.on(sensor.SensorId.HEART_RATE, (data: sensor.HeartRateResponse) => { 500 console.info('Succeeded in invoking on. Heart rate: ' + data.heartRate); 501 }, { interval: 100000000 }); 502 setTimeout(() => { 503 sensor.off(sensor.SensorId.HEART_RATE); 504 }, 500); 505} catch (error) { 506 let e: BusinessError = error as BusinessError; 507 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 508} 509``` 510 511### HUMIDITY<sup>9+</sup> 512 513on(type: SensorId.HUMIDITY, callback: Callback<HumidityResponse>, options?: Options): void 514 515Subscribes to data of the humidity sensor. 516 517**System capability**: SystemCapability.Sensors.Sensor 518 519**Parameters** 520 521| Name | Type | Mandatory| Description | 522| -------- | ----------------------------------------------------- | ---- | ----------------------------------------------------------- | 523| type | [SensorId](#sensorid9).HUMIDITY | Yes | Sensor type. The value is fixed at **SensorId.HUMIDITY**. | 524| callback | Callback<[HumidityResponse](#humidityresponse)> | Yes | Callback used to report the sensor data, which is a **HumidityResponse** object. | 525| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 526 527**Error codes** 528 529For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 530 531| ID| Error Message | 532| -------- | ------------------------------------------------------------ | 533| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 534| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 535 536**Example** 537 538```ts 539import { sensor } from '@kit.SensorServiceKit'; 540import { BusinessError } from '@kit.BasicServicesKit'; 541 542// Use try catch to capture possible exceptions. 543try { 544 sensor.on(sensor.SensorId.HUMIDITY, (data: sensor.HumidityResponse) => { 545 console.info('Succeeded in invoking on. Humidity: ' + data.humidity); 546 }, { interval: 100000000 }); 547 setTimeout(() => { 548 sensor.off(sensor.SensorId.HUMIDITY); 549 }, 500); 550} catch (error) { 551 let e: BusinessError = error as BusinessError; 552 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 553} 554``` 555 556### LINEAR_ACCELEROMETER<sup>9+</sup> 557 558on(type: SensorId.LINEAR_ACCELEROMETER, callback: Callback<LinearAccelerometerResponse>, 559 options?: Options): void 560 561Subscribes to data of the linear acceleration sensor. 562 563**Required permissions**: ohos.permission.ACCELEROMETER 564 565**System capability**: SystemCapability.Sensors.Sensor 566 567**Parameters** 568 569| Name | Type | Mandatory| Description | 570| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 571| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.LINEAR_ACCELEROMETER**. | 572| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | Yes | Callback used to report the sensor data, which is a **LinearAccelerometerResponse** object.| 573| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 574 575**Error codes** 576 577For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 578 579| ID| Error Message | 580| -------- | ------------------------------------------------------------ | 581| 201 | Permission denied. | 582| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 583| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 584 585**Example** 586 587```ts 588import { sensor } from '@kit.SensorServiceKit'; 589import { BusinessError } from '@kit.BasicServicesKit'; 590 591// Use try catch to capture possible exceptions. 592try { 593 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, (data: sensor.LinearAccelerometerResponse) => { 594 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 595 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 596 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 597 }, { interval: 100000000 }); 598 setTimeout(() => { 599 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER); 600 }, 500); 601} catch (error) { 602 let e: BusinessError = error as BusinessError; 603 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 604} 605``` 606 607### MAGNETIC_FIELD<sup>9+</sup> 608 609on(type: SensorId.MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>, options?: Options): void 610 611Subscribes to data of the magnetic field sensor. 612 613**System capability**: SystemCapability.Sensors.Sensor 614 615**Parameters** 616 617| Name | Type | Mandatory| Description | 618| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 619| type | [SensorId](#sensorid9).MAGNETIC_FIELD | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD**. | 620| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | Yes | Callback used to report the sensor data, which is a **MagneticFieldResponse** object.| 621| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 622 623**Error codes** 624 625For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 626 627| ID| Error Message | 628| -------- | ------------------------------------------------------------ | 629| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 630| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 631 632**Example** 633 634```ts 635import { sensor } from '@kit.SensorServiceKit'; 636import { BusinessError } from '@kit.BasicServicesKit'; 637 638// Use try catch to capture possible exceptions. 639try { 640 sensor.on(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 641 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 642 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 643 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 644 }, { interval: 100000000 }); 645 setTimeout(() => { 646 sensor.off(sensor.SensorId.MAGNETIC_FIELD); 647 }, 500); 648} catch (error) { 649 let e: BusinessError = error as BusinessError; 650 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 651} 652``` 653 654### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 655 656on(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void 657 658Subscribes to data of the uncalibrated magnetic field sensor. 659 660**System capability**: SystemCapability.Sensors.Sensor 661 662**Parameters** 663 664| Name | Type | Mandatory| Description | 665| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 666| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD_UNCALIBRATED**.| 667| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | Yes | Callback used to report the sensor data, which is a **MagneticFieldUncalibratedResponse** object.| 668| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 669 670**Error codes** 671 672For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 673 674| ID| Error Message | 675| -------- | ------------------------------------------------------------ | 676| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 677| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 678 679**Example** 680 681```ts 682import { sensor } from '@kit.SensorServiceKit'; 683import { BusinessError } from '@kit.BasicServicesKit'; 684 685// Use try catch to capture possible exceptions. 686try { 687 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 688 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 689 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 690 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 691 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 692 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 693 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 694 }, { interval: 100000000 }); 695 setTimeout(() => { 696 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED); 697 }, 500); 698} catch (error) { 699 let e: BusinessError = error as BusinessError; 700 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 701} 702``` 703 704### ORIENTATION<sup>9+</sup> 705 706on(type: SensorId.ORIENTATION, callback: Callback<OrientationResponse>, options?: Options): void 707 708Subscribes to data of the orientation sensor. 709 710> **NOTE** 711> 712> Applications or services invoking this API can prompt users to use figure-8 calibration to improve the accuracy of the direction sensor. The sensor has a theoretical error of ±5 degrees, but the specific precision may vary depending on different driver implementations and algorithmic designs. 713 714**Atomic service API**: This API can be used in atomic services since API version 11. 715 716**System capability**: SystemCapability.Sensors.Sensor 717 718**Error codes** 719 720For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 721 722| ID| Error Message | 723| -------- | ------------------------------------------------------------ | 724| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 725| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 726 727**Parameters** 728 729| Name | Type | Mandatory| Description | 730| -------- | ----------------------------------------------------------- | ---- | ----------------------------------------------------------- | 731| type | [SensorId](#sensorid9).ORIENTATION | Yes | Sensor type. The value is fixed at **SensorId.ORIENTATION**. | 732| callback | Callback<[OrientationResponse](#orientationresponse)> | Yes | Callback used to report the sensor data, which is a **OrientationResponse** object. | 733| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 734 735**Example** 736 737```ts 738import { sensor } from '@kit.SensorServiceKit'; 739import { BusinessError } from '@kit.BasicServicesKit'; 740 741// Use try catch to capture possible exceptions. 742try { 743 sensor.on(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => { 744 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 745 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 746 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 747 }, { interval: 100000000 }); 748 setTimeout(() => { 749 sensor.off(sensor.SensorId.ORIENTATION); 750 }, 500); 751} catch (error) { 752 let e: BusinessError = error as BusinessError; 753 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 754} 755``` 756 757### PEDOMETER<sup>9+</sup> 758 759on(type: SensorId.PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void 760 761Subscribes to data of the pedometer sensor. The step counter sensor's data reporting is subject to some delay, and the delay is determined by specific product implementations. 762 763**Required permissions**: ohos.permission.ACTIVITY_MOTION 764 765**System capability**: SystemCapability.Sensors.Sensor 766 767**Error codes** 768 769For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 770 771| ID| Error Message | 772| -------- | ------------------------------------------------------------ | 773| 201 | Permission denied. | 774| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 775| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 776 777**Parameters** 778 779| Name | Type | Mandatory| Description | 780| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 781| type | [SensorId](#sensorid9).PEDOMETER | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER**. | 782| callback | Callback<[PedometerResponse](#pedometerresponse)> | Yes | Callback used to report the sensor data, which is a **PedometerResponse** object. | 783| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 784 785**Example** 786 787```ts 788import { sensor } from '@kit.SensorServiceKit'; 789import { BusinessError } from '@kit.BasicServicesKit'; 790 791// Use try catch to capture possible exceptions. 792try { 793 sensor.on(sensor.SensorId.PEDOMETER, (data: sensor.PedometerResponse) => { 794 console.info('Succeeded in invoking on. Step count: ' + data.steps); 795 }, { interval: 100000000 }); 796 setTimeout(() => { 797 sensor.off(sensor.SensorId.PEDOMETER); 798 }, 500); 799} catch (error) { 800 let e: BusinessError = error as BusinessError; 801 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 802} 803``` 804 805### PEDOMETER_DETECTION<sup>9+</sup> 806 807on(type: SensorId.PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, 808 options?: Options): void 809 810Subscribes to data of the pedometer detection sensor. 811 812**Required permissions**: ohos.permission.ACTIVITY_MOTION 813 814**System capability**: SystemCapability.Sensors.Sensor 815 816**Parameters** 817 818| Name | Type | Mandatory| Description | 819| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 820| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER_DETECTION**. | 821| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | Yes | Callback used to report the sensor data, which is a **PedometerDetectionResponse** object.| 822| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 823 824**Error codes** 825 826For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 827 828| ID| Error Message | 829| -------- | ------------------------------------------------------------ | 830| 201 | Permission denied. | 831| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 832| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 833 834**Example** 835 836```ts 837import { sensor } from '@kit.SensorServiceKit'; 838import { BusinessError } from '@kit.BasicServicesKit'; 839 840// Use try catch to capture possible exceptions. 841try { 842 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 843 console.info('Succeeded in invoking on. Pedometer scalar: ' + data.scalar); 844 }, { interval: 100000000 }); 845 setTimeout(() => { 846 sensor.off(sensor.SensorId.PEDOMETER_DETECTION); 847 }, 500); 848} catch (error) { 849 let e: BusinessError = error as BusinessError; 850 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 851} 852``` 853 854### PROXIMITY<sup>9+</sup> 855 856on(type: SensorId.PROXIMITY, callback: Callback<ProximityResponse>, options?: Options): void 857 858Subscribes to data of the proximity sensor. 859 860**System capability**: SystemCapability.Sensors.Sensor 861 862**Parameters** 863 864| Name | Type | Mandatory| Description | 865| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 866| type | [SensorId](#sensorid9).PROXIMITY | Yes | Sensor type. The value is fixed at **SensorId.PROXIMITY**. | 867| callback | Callback<[ProximityResponse](#proximityresponse)> | Yes | Callback used to report the sensor data, which is a **ProximityResponse** object. | 868| options | [Options](#options) | No | List of optional parameters. The default value is 200,000,000 ns. This parameter is used to set the data reporting frequency when proximity sensor events are frequently triggered.| 869 870**Error codes** 871 872For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 873 874| ID| Error Message | 875| -------- | ------------------------------------------------------------ | 876| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 877| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 878 879**Example** 880 881```ts 882import { sensor } from '@kit.SensorServiceKit'; 883import { BusinessError } from '@kit.BasicServicesKit'; 884 885// Use try catch to capture possible exceptions. 886try { 887 sensor.on(sensor.SensorId.PROXIMITY, (data: sensor.ProximityResponse) => { 888 console.info('Succeeded in invoking on. Distance: ' + data.distance); 889 }, { interval: 100000000 }); 890 setTimeout(() => { 891 sensor.off(sensor.SensorId.PROXIMITY); 892 }, 500); 893} catch (error) { 894 let e: BusinessError = error as BusinessError; 895 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 896} 897``` 898 899### ROTATION_VECTOR<sup>9+</sup> 900 901on(type: SensorId.ROTATION_VECTOR, callback: Callback<RotationVectorResponse>, 902 options?: Options): void 903 904Subscribes to data of the rotation vector sensor. 905 906**System capability**: SystemCapability.Sensors.Sensor 907 908**Parameters** 909 910| Name | Type | Mandatory| Description | 911| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 912| type | [SensorId](#sensorid9).ROTATION_VECTOR | Yes | Sensor type. The value is fixed at **SensorId.ROTATION_VECTOR**. | 913| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | Yes | Callback used to report the sensor data, which is a **RotationVectorResponse** object.| 914| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 915 916**Error codes** 917 918For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 919 920| ID| Error Message | 921| -------- | ------------------------------------------------------------ | 922| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 923| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 924 925**Example** 926 927```ts 928import { sensor } from '@kit.SensorServiceKit'; 929import { BusinessError } from '@kit.BasicServicesKit'; 930 931// Use try catch to capture possible exceptions. 932try { 933 sensor.on(sensor.SensorId.ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 934 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 935 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 936 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 937 console.info('Succeeded in invoking on. Scalar quantity: ' + data.w); 938 }, { interval: 100000000 }); 939 setTimeout(() => { 940 sensor.off(sensor.SensorId.ROTATION_VECTOR); 941 }, 500); 942} catch (error) { 943 let e: BusinessError = error as BusinessError; 944 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 945} 946``` 947 948### SIGNIFICANT_MOTION<sup>9+</sup> 949 950on(type: SensorId.SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, 951 options?: Options): void 952 953Subscribes to the significant motion sensor data. 954 955**System capability**: SystemCapability.Sensors.Sensor 956 957**Parameters** 958 959| Name | Type | Mandatory| Description | 960| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 961| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | Yes | Sensor type. The value is fixed at **SensorId.SIGNIFICANT_MOTION**. | 962| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | Yes | Callback used to report the sensor data, which is a **SignificantMotionResponse** object.| 963| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 964 965**Error codes** 966 967For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 968 969| ID| Error Message | 970| -------- | ------------------------------------------------------------ | 971| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 972| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 973 974**Example** 975 976```ts 977import { sensor } from '@kit.SensorServiceKit'; 978import { BusinessError } from '@kit.BasicServicesKit'; 979 980// Use try catch to capture possible exceptions. 981try { 982 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 983 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 984 }, { interval: 100000000 }); 985 setTimeout(() => { 986 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION); 987 }, 500); 988} catch (error) { 989 let e: BusinessError = error as BusinessError; 990 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 991} 992``` 993 994### WEAR_DETECTION<sup>9+</sup> 995 996on(type: SensorId.WEAR_DETECTION, callback: Callback<WearDetectionResponse>, 997 options?: Options): void 998 999Subscribes to data of the wear detection sensor. 1000 1001**System capability**: SystemCapability.Sensors.Sensor 1002 1003**Parameters** 1004 1005| Name | Type | Mandatory| Description | 1006| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1007| type | [SensorId](#sensorid9).WEAR_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.WEAR_DETECTION**. | 1008| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | Yes | Callback used to report the sensor data, which is a **WearDetectionResponse** object.| 1009| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 1010 1011**Error codes** 1012 1013For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1014 1015| ID| Error Message | 1016| -------- | ------------------------------------------------------------ | 1017| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 1018| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1019 1020**Example** 1021 1022```ts 1023import { sensor } from '@kit.SensorServiceKit'; 1024import { BusinessError } from '@kit.BasicServicesKit'; 1025 1026// Use try catch to capture possible exceptions. 1027try { 1028 sensor.on(sensor.SensorId.WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 1029 console.info('Succeeded in invoking on. Wear status: ' + data.value); 1030 }, { interval: 100000000 }); 1031 setTimeout(() => { 1032 sensor.off(sensor.SensorId.WEAR_DETECTION); 1033 }, 500); 1034} catch (error) { 1035 let e: BusinessError = error as BusinessError; 1036 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 1037} 1038``` 1039 1040### sensorStatusChange<sup>19+</sup> 1041 1042on(type: 'sensorStatusChange', callback: Callback<SensorStatusEvent>): void 1043 1044Enables listening for sensor status changes. This API asynchronously returns the result through a callback. 1045 1046**System capability**: SystemCapability.Sensors.Sensor 1047 1048**Parameters** 1049 1050| Name | Type | Mandatory| Description | 1051| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1052| type | 'sensorStatusChange' | Yes | Event type. The value **sensorStatusChange** indicates the sensor status change event. | 1053| callback | Callback<[SensorStatusEvent](#sensorstatusevent19)> | Yes | Callback used to return the sensor status change event.| 1054 1055**Error codes** 1056 1057For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1058 1059| ID| Error Message | 1060| -------- | ------------------------------------------------------------ | 1061| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1062 1063**Example** 1064 1065```ts 1066import { sensor } from '@kit.SensorServiceKit'; 1067import { BusinessError } from '@kit.BasicServicesKit'; 1068 1069// Use try catch to capture possible exceptions. 1070try { 1071 sensor.on('sensorStatusChange', (data: sensor.SensorStatusEvent) => { 1072 console.info('sensorStatusChange : ' + JSON.stringify(data)); 1073 }); 1074 setTimeout(() => { 1075 sensor.off('sensorStatusChange'); 1076 }, 5000); 1077} catch (error) { 1078 let e: BusinessError = error as BusinessError; 1079 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 1080} 1081``` 1082 1083 1084## sensor.once<sup>9+</sup> 1085 1086### ACCELEROMETER<sup>9+</sup> 1087 1088once(type: SensorId.ACCELEROMETER, callback: Callback<AccelerometerResponse>): void 1089 1090Obtains data of the acceleration sensor once. 1091 1092**Required permissions**: ohos.permission.ACCELEROMETER 1093 1094**System capability**: SystemCapability.Sensors.Sensor 1095 1096**Parameters** 1097 1098| Name | Type | Mandatory| Description | 1099| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1100| type | [SensorId](#sensorid9).ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER**. | 1101| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | Yes | Callback used to report the sensor data, which is an **AccelerometerResponse** object.| 1102 1103**Error codes** 1104 1105For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1106 1107| ID| Error Message | 1108| -------- | ------------------------------------------------------------ | 1109| 201 | Permission denied. | 1110| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1111| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1112 1113**Example** 1114 1115```ts 1116import { sensor } from '@kit.SensorServiceKit'; 1117import { BusinessError } from '@kit.BasicServicesKit'; 1118 1119// Use try catch to capture possible exceptions. 1120try { 1121 sensor.once(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 1122 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1123 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1124 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1125 }); 1126} catch (error) { 1127 let e: BusinessError = error as BusinessError; 1128 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1129} 1130``` 1131 1132### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 1133 1134once(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback: Callback<AccelerometerUncalibratedResponse>): void 1135 1136Obtains data of the uncalibrated acceleration sensor once. 1137 1138**Required permissions**: ohos.permission.ACCELEROMETER 1139 1140**System capability**: SystemCapability.Sensors.Sensor 1141 1142**Parameters** 1143 1144| Name | Type | Mandatory| Description | 1145| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1146| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER_UNCALIBRATED**. | 1147| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | Yes | Callback used to report the sensor data, which is an **AccelerometerUncalibratedResponse** object.| 1148 1149**Error codes** 1150 1151For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1152 1153| ID| Error Message | 1154| -------- | ------------------------------------------------------------ | 1155| 201 | Permission denied. | 1156| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1157| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1158 1159**Example** 1160 1161```ts 1162import { sensor } from '@kit.SensorServiceKit'; 1163import { BusinessError } from '@kit.BasicServicesKit'; 1164 1165// Use try catch to capture possible exceptions. 1166try { 1167 sensor.once(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 1168 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1169 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1170 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1171 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1172 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1173 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1174 }); 1175} catch (error) { 1176 let e: BusinessError = error as BusinessError; 1177 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1178} 1179``` 1180 1181### AMBIENT_LIGHT<sup>9+</sup> 1182 1183once(type: SensorId.AMBIENT_LIGHT, callback: Callback<LightResponse>): void 1184 1185Obtains data of the ambient light sensor once. 1186 1187**System capability**: SystemCapability.Sensors.Sensor 1188 1189**Parameters** 1190 1191| Name | Type | Mandatory| Description | 1192| -------- | ----------------------------------------------- | ---- | --------------------------------------------------- | 1193| type | [SensorId](#sensorid9).AMBIENT_LIGHT | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_LIGHT**. | 1194| callback | Callback<[LightResponse](#lightresponse)> | Yes | Callback used to report the sensor data, which is a **LightResponse** object.| 1195 1196**Error codes** 1197 1198For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1199 1200| ID| Error Message | 1201| -------- | ------------------------------------------------------------ | 1202| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1203| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1204 1205**Example** 1206 1207```ts 1208import { sensor } from '@kit.SensorServiceKit'; 1209import { BusinessError } from '@kit.BasicServicesKit'; 1210 1211// Use try catch to capture possible exceptions. 1212try { 1213 sensor.once(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => { 1214 console.info('Succeeded in invoking once. the ambient light intensity: ' + data.intensity); 1215 }); 1216} catch (error) { 1217 let e: BusinessError = error as BusinessError; 1218 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1219} 1220``` 1221 1222### AMBIENT_TEMPERATURE<sup>9+</sup> 1223 1224once(type: SensorId.AMBIENT_TEMPERATURE, callback: Callback<AmbientTemperatureResponse>): void 1225 1226Obtains data of the temperature sensor once. 1227 1228**System capability**: SystemCapability.Sensors.Sensor 1229 1230**Parameters** 1231 1232| Name | Type | Mandatory| Description | 1233| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1234| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_TEMPERATURE**. | 1235| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | Yes | Callback used to report the sensor data, which is an **AmbientTemperatureResponse** object.| 1236 1237**Error codes** 1238 1239For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1240 1241| ID| Error Message | 1242| -------- | ------------------------------------------------------------ | 1243| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1244| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1245 1246**Example** 1247 1248```ts 1249import { sensor } from '@kit.SensorServiceKit'; 1250import { BusinessError } from '@kit.BasicServicesKit'; 1251 1252// Use try catch to capture possible exceptions. 1253try { 1254 sensor.once(sensor.SensorId.AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 1255 console.info('Succeeded in invoking once. Temperature: ' + data.temperature); 1256 }); 1257} catch (error) { 1258 let e: BusinessError = error as BusinessError; 1259 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1260} 1261``` 1262 1263### BAROMETER<sup>9+</sup> 1264 1265once(type: SensorId.BAROMETER, callback: Callback<BarometerResponse>): void 1266 1267Obtains data of the barometer sensor once. 1268 1269**System capability**: SystemCapability.Sensors.Sensor 1270 1271**Parameters** 1272 1273| Name | Type | Mandatory| Description | 1274| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1275| type | [SensorId](#sensorid9).BAROMETER | Yes | Sensor type. The value is fixed at **SensorId.BAROMETER**. | 1276| callback | Callback<[BarometerResponse](#barometerresponse)> | Yes | Callback used to report the sensor data, which is a **BarometerResponse** object.| 1277 1278**Error codes** 1279 1280For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1281 1282| ID| Error Message | 1283| -------- | ------------------------------------------------------------ | 1284| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1285| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1286 1287**Example** 1288 1289```ts 1290import { sensor } from '@kit.SensorServiceKit'; 1291import { BusinessError } from '@kit.BasicServicesKit'; 1292 1293// Use try catch to capture possible exceptions. 1294try { 1295 sensor.once(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => { 1296 console.info('Succeeded in invoking once. Atmospheric pressure: ' + data.pressure); 1297 }); 1298} catch (error) { 1299 let e: BusinessError = error as BusinessError; 1300 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1301} 1302``` 1303 1304### GRAVITY<sup>9+</sup> 1305 1306once(type: SensorId.GRAVITY, callback: Callback<GravityResponse>): void 1307 1308Obtains data of the gravity sensor once. 1309 1310**System capability**: SystemCapability.Sensors.Sensor 1311 1312**Parameters** 1313 1314| Name | Type | Mandatory| Description | 1315| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------- | 1316| type | [SensorId](#sensorid9).GRAVITY | Yes | Sensor type. The value is fixed at **SensorId.GRAVITY**. | 1317| callback | Callback<[GravityResponse](#gravityresponse)> | Yes | Callback used to report the sensor data, which is a **GravityResponse** object.| 1318 1319**Error codes** 1320 1321For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1322 1323| ID| Error Message | 1324| -------- | ------------------------------------------------------------ | 1325| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1326| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1327 1328**Example** 1329 1330```ts 1331import { sensor } from '@kit.SensorServiceKit'; 1332import { BusinessError } from '@kit.BasicServicesKit'; 1333 1334// Use try catch to capture possible exceptions. 1335try { 1336 sensor.once(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 1337 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1338 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1339 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1340 }); 1341} catch (error) { 1342 let e: BusinessError = error as BusinessError; 1343 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1344} 1345``` 1346 1347### GYROSCOPE<sup>9+</sup> 1348 1349once(type: SensorId.GYROSCOPE, callback: Callback<GyroscopeResponse>): void 1350 1351Obtains to data of the gyroscope sensor once. 1352 1353**Required permissions**: ohos.permission.GYROSCOPE 1354 1355**System capability**: SystemCapability.Sensors.Sensor 1356 1357**Parameters** 1358 1359| Name | Type | Mandatory| Description | 1360| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1361| type | [SensorId](#sensorid9).GYROSCOPE | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE**. | 1362| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | Yes | Callback used to report the sensor data, which is a **GyroscopeResponse** object.| 1363 1364**Error codes** 1365 1366For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1367 1368| ID| Error Message | 1369| -------- | ------------------------------------------------------------ | 1370| 201 | Permission denied. | 1371| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1372| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1373 1374**Example** 1375 1376```ts 1377import { sensor } from '@kit.SensorServiceKit'; 1378import { BusinessError } from '@kit.BasicServicesKit'; 1379 1380// Use try catch to capture possible exceptions. 1381try { 1382 sensor.once(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => { 1383 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1384 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1385 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1386 }); 1387} catch (error) { 1388 let e: BusinessError = error as BusinessError; 1389 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1390} 1391``` 1392 1393### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 1394 1395once(type: SensorId.GYROSCOPE_UNCALIBRATED, callback: Callback<GyroscopeUncalibratedResponse>): void 1396 1397Obtains data of the uncalibrated gyroscope sensor once. 1398 1399**Required permissions**: ohos.permission.GYROSCOPE 1400 1401**System capability**: SystemCapability.Sensors.Sensor 1402 1403**Parameters** 1404 1405| Name | Type | Mandatory| Description | 1406| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1407| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE_UNCALIBRATED**. | 1408| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | Yes | Callback used to report the sensor data, which is a **GyroscopeUncalibratedResponse** object.| 1409 1410**Error codes** 1411 1412For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1413 1414| ID| Error Message | 1415| -------- | ------------------------------------------------------------ | 1416| 201 | Permission denied. | 1417| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1418| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1419 1420**Example** 1421 1422```ts 1423import { sensor } from '@kit.SensorServiceKit'; 1424import { BusinessError } from '@kit.BasicServicesKit'; 1425 1426// Use try catch to capture possible exceptions. 1427try { 1428 sensor.once(sensor.SensorId.GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 1429 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1430 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1431 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1432 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1433 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1434 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1435 }); 1436} catch (error) { 1437 let e: BusinessError = error as BusinessError; 1438 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1439} 1440``` 1441 1442### HALL<sup>9+</sup> 1443 1444once(type: SensorId.HALL, callback: Callback<HallResponse>): void 1445 1446Obtains data of the Hall effect sensor once. 1447 1448**System capability**: SystemCapability.Sensors.Sensor 1449 1450**Parameters** 1451 1452| Name | Type | Mandatory| Description | 1453| -------- | --------------------------------------------- | ---- | -------------------------------------------------- | 1454| type | [SensorId](#sensorid9).HALL | Yes | Sensor type. The value is fixed at **SensorId.HALL**. | 1455| callback | Callback<[HallResponse](#hallresponse)> | Yes | Callback used to report the sensor data, which is a **HallResponse** object.| 1456 1457**Error codes** 1458 1459For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1460 1461| ID| Error Message | 1462| -------- | ------------------------------------------------------------ | 1463| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1464| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1465 1466**Example** 1467 1468```ts 1469import { sensor } from '@kit.SensorServiceKit'; 1470import { BusinessError } from '@kit.BasicServicesKit'; 1471 1472// Use try catch to capture possible exceptions. 1473try { 1474 sensor.once(sensor.SensorId.HALL, (data: sensor.HallResponse) => { 1475 console.info('Succeeded in invoking once. Status: ' + data.status); 1476 }); 1477} catch (error) { 1478 let e: BusinessError = error as BusinessError; 1479 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1480} 1481``` 1482 1483### HEART_RATE<sup>9+</sup> 1484 1485once(type: SensorId.HEART_RATE, callback: Callback<HeartRateResponse>): void 1486 1487Obtains data of the heart rate sensor once. 1488 1489**Required permissions**: ohos.permission.READ_HEALTH_DATA 1490 1491**System capability**: SystemCapability.Sensors.Sensor 1492 1493**Parameters** 1494 1495| Name | Type | Mandatory| Description | 1496| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1497| type | [SensorId](#sensorid9).HEART_RATE | Yes | Sensor type. The value is fixed at **SensorId.HEART_RATE**. | 1498| callback | Callback<[HeartRateResponse](#heartrateresponse)> | Yes | Callback used to report the sensor data, which is a **HeartRateResponse** object.| 1499 1500**Error codes** 1501 1502For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1503 1504| ID| Error Message | 1505| -------- | ------------------------------------------------------------ | 1506| 201 | Permission denied. | 1507| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1508| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1509 1510**Example** 1511 1512```ts 1513import { sensor } from '@kit.SensorServiceKit'; 1514import { BusinessError } from '@kit.BasicServicesKit'; 1515 1516// Use try catch to capture possible exceptions. 1517try { 1518 sensor.once(sensor.SensorId.HEART_RATE, (data: sensor.HeartRateResponse) => { 1519 console.info('Succeeded in invoking once. Heart rate: ' + data.heartRate); 1520 }); 1521} catch (error) { 1522 let e: BusinessError = error as BusinessError; 1523 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1524} 1525``` 1526 1527### HUMIDITY<sup>9+</sup> 1528 1529once(type: SensorId.HUMIDITY, callback: Callback<HumidityResponse>): void 1530 1531Obtains data of the humidity sensor once. 1532 1533**System capability**: SystemCapability.Sensors.Sensor 1534 1535**Parameters** 1536 1537| Name | Type | Mandatory| Description | 1538| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------ | 1539| type | [SensorId](#sensorid9).HUMIDITY | Yes | Sensor type. The value is fixed at **SensorId.HUMIDITY**. | 1540| callback | Callback<[HumidityResponse](#humidityresponse)> | Yes | Callback used to report the sensor data, which is a **HumidityResponse** object.| 1541 1542**Error codes** 1543 1544For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1545 1546| ID| Error Message | 1547| -------- | ------------------------------------------------------------ | 1548| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1549| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1550 1551**Example** 1552 1553```ts 1554import { sensor } from '@kit.SensorServiceKit'; 1555import { BusinessError } from '@kit.BasicServicesKit'; 1556 1557// Use try catch to capture possible exceptions. 1558try { 1559 sensor.once(sensor.SensorId.HUMIDITY, (data: sensor.HumidityResponse) => { 1560 console.info('Succeeded in invoking once. Humidity: ' + data.humidity); 1561 }); 1562} catch (error) { 1563 let e: BusinessError = error as BusinessError; 1564 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1565} 1566``` 1567 1568### LINEAR_ACCELEROMETER<sup>9+</sup> 1569 1570once(type: SensorId.LINEAR_ACCELEROMETER, callback: Callback<LinearAccelerometerResponse>): void 1571 1572Obtains data of the linear acceleration sensor once. 1573 1574**Required permissions**: ohos.permission.ACCELEROMETER 1575 1576**System capability**: SystemCapability.Sensors.Sensor 1577 1578**Parameters** 1579 1580| Name | Type | Mandatory| Description | 1581| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1582| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.LINEAR_ACCELEROMETER**. | 1583| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | Yes | Callback used to report the sensor data, which is a **LinearAccelerometerResponse** object.| 1584 1585**Error codes** 1586 1587For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1588 1589| ID| Error Message | 1590| -------- | ------------------------------------------------------------ | 1591| 201 | Permission denied. | 1592| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1593| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1594 1595**Example** 1596 1597```ts 1598import { sensor } from '@kit.SensorServiceKit'; 1599import { BusinessError } from '@kit.BasicServicesKit'; 1600 1601// Use try catch to capture possible exceptions. 1602try { 1603 sensor.once(sensor.SensorId.LINEAR_ACCELEROMETER, (data: sensor.LinearAccelerometerResponse) => { 1604 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1605 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1606 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1607 }); 1608} catch (error) { 1609 let e: BusinessError = error as BusinessError; 1610 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1611} 1612``` 1613 1614### MAGNETIC_FIELD<sup>9+</sup> 1615 1616once(type: SensorId.MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>): void 1617 1618Obtains data of the magnetic field sensor once. 1619 1620**System capability**: SystemCapability.Sensors.Sensor 1621 1622**Parameters** 1623 1624| Name | Type | Mandatory| Description | 1625| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1626| type | [SensorId](#sensorid9).MAGNETIC_FIELD | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD**. | 1627| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | Yes | Callback used to report the sensor data, which is a **MagneticFieldResponse** object.| 1628 1629**Error codes** 1630 1631For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1632 1633| ID| Error Message | 1634| -------- | ------------------------------------------------------------ | 1635| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1636| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1637 1638**Example** 1639 1640```ts 1641import { sensor } from '@kit.SensorServiceKit'; 1642import { BusinessError } from '@kit.BasicServicesKit'; 1643 1644// Use try catch to capture possible exceptions. 1645try { 1646 sensor.once(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 1647 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1648 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1649 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1650 }); 1651} catch (error) { 1652 let e: BusinessError = error as BusinessError; 1653 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1654} 1655``` 1656 1657### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 1658 1659once(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback: Callback<MagneticFieldUncalibratedResponse>): void 1660 1661Obtains data of the uncalibrated magnetic field sensor once. 1662 1663**System capability**: SystemCapability.Sensors.Sensor 1664 1665**Parameters** 1666 1667| Name | Type | Mandatory| Description | 1668| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1669| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD_UNCALIBRATED**.| 1670| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | Yes | Callback used to report the sensor data, which is a **MagneticFieldUncalibratedResponse** object.| 1671 1672**Error codes** 1673 1674For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1675 1676| ID| Error Message | 1677| -------- | ------------------------------------------------------------ | 1678| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1679| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1680 1681**Example** 1682 1683```ts 1684import { sensor } from '@kit.SensorServiceKit'; 1685import { BusinessError } from '@kit.BasicServicesKit'; 1686 1687// Use try catch to capture possible exceptions. 1688try { 1689 sensor.once(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 1690 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1691 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1692 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1693 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1694 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1695 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1696 }); 1697} catch (error) { 1698 let e: BusinessError = error as BusinessError; 1699 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1700} 1701``` 1702 1703### ORIENTATION<sup>9+</sup> 1704 1705once(type: SensorId.ORIENTATION, callback: Callback<OrientationResponse>): void 1706 1707Obtains data of the orientation sensor once. 1708 1709**System capability**: SystemCapability.Sensors.Sensor 1710 1711**Parameters** 1712 1713| Name | Type | Mandatory| Description | 1714| -------- | ----------------------------------------------------------- | ---- | --------------------------------------------------------- | 1715| type | [SensorId](#sensorid9).ORIENTATION | Yes | Sensor type. The value is fixed at **SensorId.ORIENTATION**. | 1716| callback | Callback<[OrientationResponse](#orientationresponse)> | Yes | Callback used to report the sensor data, which is a **OrientationResponse** object.| 1717 1718**Error codes** 1719 1720For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1721 1722| ID| Error Message | 1723| -------- | ------------------------------------------------------------ | 1724| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1725| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1726 1727**Example** 1728 1729```ts 1730import { sensor } from '@kit.SensorServiceKit'; 1731import { BusinessError } from '@kit.BasicServicesKit'; 1732 1733// Use try catch to capture possible exceptions. 1734try { 1735 sensor.once(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => { 1736 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 1737 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 1738 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 1739 }); 1740} catch (error) { 1741 let e: BusinessError = error as BusinessError; 1742 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1743} 1744``` 1745 1746### PEDOMETER<sup>9+</sup> 1747 1748once(type: SensorId.PEDOMETER, callback: Callback<PedometerResponse>): void 1749 1750Obtains data of the pedometer sensor once. The step counter sensor's data reporting is subject to some delay, and the delay is determined by specific product implementations. 1751 1752**Required permissions**: ohos.permission.ACTIVITY_MOTION 1753 1754**System capability**: SystemCapability.Sensors.Sensor 1755 1756**Parameters** 1757 1758| Name | Type | Mandatory| Description | 1759| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1760| type | [SensorId](#sensorid9).PEDOMETER | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER**. | 1761| callback | Callback<[PedometerResponse](#pedometerresponse)> | Yes | Callback used to report the sensor data, which is a **PedometerResponse** object.| 1762 1763**Error codes** 1764 1765For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1766 1767| ID| Error Message | 1768| -------- | ------------------------------------------------------------ | 1769| 201 | Permission denied. | 1770| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1771| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1772 1773**Example** 1774 1775```ts 1776import { sensor } from '@kit.SensorServiceKit'; 1777import { BusinessError } from '@kit.BasicServicesKit'; 1778 1779// Use try catch to capture possible exceptions. 1780try { 1781 sensor.once(sensor.SensorId.PEDOMETER, (data: sensor.PedometerResponse) => { 1782 console.info('Succeeded in invoking once. Step count: ' + data.steps); 1783 }); 1784} catch (error) { 1785 let e: BusinessError = error as BusinessError; 1786 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1787} 1788``` 1789 1790### PEDOMETER_DETECTION<sup>9+</sup> 1791 1792once(type: SensorId.PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>): void 1793 1794Obtains data of the pedometer sensor once. 1795 1796**Required permissions**: ohos.permission.ACTIVITY_MOTION 1797 1798**System capability**: SystemCapability.Sensors.Sensor 1799 1800**Parameters** 1801 1802| Name | Type | Mandatory| Description | 1803| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1804| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER_DETECTION**. | 1805| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | Yes | Callback used to report the sensor data, which is a **PedometerDetectionResponse** object.| 1806 1807**Error codes** 1808 1809For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1810 1811| ID| Error Message | 1812| -------- | ------------------------------------------------------------ | 1813| 201 | Permission denied. | 1814| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1815| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1816 1817**Example** 1818 1819```ts 1820import { sensor } from '@kit.SensorServiceKit'; 1821import { BusinessError } from '@kit.BasicServicesKit'; 1822 1823// Use try catch to capture possible exceptions. 1824try { 1825 sensor.once(sensor.SensorId.PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 1826 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 1827 }); 1828} catch (error) { 1829 let e: BusinessError = error as BusinessError; 1830 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1831} 1832``` 1833 1834### PROXIMITY<sup>9+</sup> 1835 1836once(type: SensorId.PROXIMITY, callback: Callback<ProximityResponse>): void 1837 1838Obtains data of the proximity sensor once. 1839 1840**System capability**: SystemCapability.Sensors.Sensor 1841 1842**Parameters** 1843 1844| Name | Type | Mandatory| Description | 1845| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1846| type | [SensorId](#sensorid9).PROXIMITY | Yes | Sensor type. The value is fixed at **SensorId.PROXIMITY**. | 1847| callback | Callback<[ProximityResponse](#proximityresponse)> | Yes | Callback used to report the sensor data, which is a **ProximityResponse** object.| 1848 1849**Error codes** 1850 1851For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1852 1853| ID| Error Message | 1854| -------- | ------------------------------------------------------------ | 1855| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1856| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1857 1858**Example** 1859 1860```ts 1861import { sensor } from '@kit.SensorServiceKit'; 1862import { BusinessError } from '@kit.BasicServicesKit'; 1863 1864// Use try catch to capture possible exceptions. 1865try { 1866 sensor.once(sensor.SensorId.PROXIMITY, (data: sensor.ProximityResponse) => { 1867 console.info('Succeeded in invoking once. Distance: ' + data.distance); 1868 }); 1869} catch (error) { 1870 let e: BusinessError = error as BusinessError; 1871 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1872} 1873``` 1874 1875### ROTATION_VECTOR<sup>9+</sup> 1876 1877once(type: SensorId.ROTATION_VECTOR, callback: Callback<RotationVectorResponse>): void 1878 1879Obtains data of the rotation vector sensor once. 1880 1881**System capability**: SystemCapability.Sensors.Sensor 1882 1883**Parameters** 1884 1885| Name | Type | Mandatory| Description | 1886| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1887| type | [SensorId](#sensorid9).ROTATION_VECTOR | Yes | Sensor type. The value is fixed at **SensorId.ROTATION_VECTOR**. | 1888| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | Yes | Callback used to report the sensor data, which is a **RotationVectorResponse** object.| 1889 1890**Error codes** 1891 1892For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1893 1894| ID| Error Message | 1895| -------- | ------------------------------------------------------------ | 1896| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1897| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1898 1899**Example** 1900 1901```ts 1902import { sensor } from '@kit.SensorServiceKit'; 1903import { BusinessError } from '@kit.BasicServicesKit'; 1904 1905// Use try catch to capture possible exceptions. 1906try { 1907 sensor.once(sensor.SensorId.ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 1908 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1909 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1910 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1911 console.info('Succeeded in invoking once. Scalar quantity: ' + data.w); 1912 }); 1913} catch (error) { 1914 let e: BusinessError = error as BusinessError; 1915 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1916} 1917``` 1918 1919### SIGNIFICANT_MOTION<sup>9+</sup> 1920 1921once(type: SensorId.SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>): void 1922 1923Obtains the significant motion sensor data once. 1924 1925**System capability**: SystemCapability.Sensors.Sensor 1926 1927**Parameters** 1928 1929| Name | Type | Mandatory| Description | 1930| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1931| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | Yes | Sensor type. The value is fixed at **SensorId.SIGNIFICANT_MOTION**. | 1932| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | Yes | Callback used to report the sensor data, which is a **SignificantMotionResponse** object.| 1933 1934**Error codes** 1935 1936For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1937 1938| ID| Error Message | 1939| -------- | ------------------------------------------------------------ | 1940| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1941| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1942 1943**Example** 1944 1945```ts 1946import { sensor } from '@kit.SensorServiceKit'; 1947import { BusinessError } from '@kit.BasicServicesKit'; 1948 1949// Use try catch to capture possible exceptions. 1950try { 1951 sensor.once(sensor.SensorId.SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 1952 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 1953 }); 1954} catch (error) { 1955 let e: BusinessError = error as BusinessError; 1956 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1957} 1958``` 1959 1960### WEAR_DETECTION<sup>9+</sup> 1961 1962once(type: SensorId.WEAR_DETECTION, callback: Callback<WearDetectionResponse>): void 1963 1964Obtains data of the wear detection sensor once. 1965 1966**System capability**: SystemCapability.Sensors.Sensor 1967 1968**Parameters** 1969 1970| Name | Type | Mandatory| Description | 1971| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1972| type | [SensorId](#sensorid9).WEAR_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.WEAR_DETECTION**. | 1973| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | Yes | Callback used to report the sensor data, which is a **WearDetectionResponse** object.| 1974 1975**Error codes** 1976 1977For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 1978 1979| ID| Error Message | 1980| -------- | ------------------------------------------------------------ | 1981| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1982| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1983 1984**Example** 1985 1986```ts 1987import { sensor } from '@kit.SensorServiceKit'; 1988import { BusinessError } from '@kit.BasicServicesKit'; 1989 1990// Use try catch to capture possible exceptions. 1991try { 1992 sensor.once(sensor.SensorId.WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 1993 console.info('Succeeded in invoking once. Wear status: ' + data.value); 1994 }); 1995} catch (error) { 1996 let e: BusinessError = error as BusinessError; 1997 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1998} 1999``` 2000 2001## sensor.off 2002 2003### ACCELEROMETER<sup>9+</sup> 2004 2005off(type: SensorId.ACCELEROMETER, callback?: Callback<AccelerometerResponse>): void 2006 2007Unsubscribes from data of the acceleration sensor. 2008 2009**Required permissions**: ohos.permission.ACCELEROMETER 2010 2011**Atomic service API**: This API can be used in atomic services since API version 11. 2012 2013**System capability**: SystemCapability.Sensors.Sensor 2014 2015**Parameters** 2016 2017| Name | Type | Mandatory| Description | 2018| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2019| type | [SensorId](#sensorid9).ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER**. | 2020| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2021 2022**Error codes** 2023 2024For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2025 2026| ID| Error Message | 2027| -------- | ------------------------------------------------------------ | 2028| 201 | Permission denied. | 2029| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2030 2031**Example** 2032 2033```ts 2034import { sensor } from '@kit.SensorServiceKit'; 2035import { BusinessError } from '@kit.BasicServicesKit'; 2036 2037function callback1(data: object) { 2038 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2039} 2040 2041function callback2(data: object) { 2042 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2043} 2044 2045// Use try catch to capture possible exceptions. 2046try { 2047 sensor.on(sensor.SensorId.ACCELEROMETER, callback1); 2048 sensor.on(sensor.SensorId.ACCELEROMETER, callback2); 2049 // Unsubscribe from callback1. 2050 sensor.off(sensor.SensorId.ACCELEROMETER, callback1); 2051 // Unsubscribe from all callbacks of the SensorId.ACCELEROMETER type. 2052 sensor.off(sensor.SensorId.ACCELEROMETER); 2053} catch (error) { 2054 let e: BusinessError = error as BusinessError; 2055 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2056} 2057``` 2058 2059### ACCELEROMETER<sup>19+</sup> 2060 2061off(type: SensorId.ACCELEROMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<AccelerometerResponse>): void 2062 2063Unsubscribes from data of the acceleration sensor. 2064 2065**Required permissions**: ohos.permission.ACCELEROMETER 2066 2067**Atomic service API**: This API can be used in atomic services since API version 19. 2068 2069**System capability**: SystemCapability.Sensors.Sensor 2070 2071**Parameters** 2072 2073| Name | Type | Mandatory| Description | 2074|--------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2075| type | [SensorId](#sensorid9).ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER**. | 2076| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2077| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2078 2079**Error codes** 2080 2081For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2082 2083| ID| Error Message | 2084| -------- | ------------------------------------------------------------ | 2085| 201 | Permission denied. | 2086| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2087 2088**Example** 2089 2090```ts 2091import { sensor } from '@kit.SensorServiceKit'; 2092import { BusinessError } from '@kit.BasicServicesKit'; 2093 2094enum Ret { OK, Failed = -1 } 2095 2096// Sensor callback 2097const sensorCallback = (response: sensor.AccelerometerResponse) => { 2098 console.log(`callback response: ${JSON.stringify(response)}`); 2099} 2100// Sensor type 2101const sensorType = sensor.SensorId.ACCELEROMETER; 2102const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2103 2104function sensorSubscribe(): Ret { 2105 let ret: Ret = Ret.OK; 2106 // Use try catch to capture possible exceptions. 2107 try { 2108 // Query all sensors. 2109 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2110 if (!sensorList.length) { 2111 return Ret.Failed; 2112 } 2113 // Obtain the target sensor based on the actual service logic. 2114 const targetSensor = sensorList 2115 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2116 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2117 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2118 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2119 if (!targetSensor) { 2120 return Ret.Failed; 2121 } 2122 sensorInfoParam.deviceId = targetSensor.deviceId; 2123 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2124 // Subscribe to sensor events. 2125 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2126 } catch (error) { 2127 let e: BusinessError = error as BusinessError; 2128 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2129 ret = Ret.Failed; 2130 } 2131 return ret; 2132} 2133 2134function sensorUnsubscribe(): Ret { 2135 let ret: Ret = Ret.OK; 2136 // Use try catch to capture possible exceptions. 2137 try { 2138 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2139 } catch (error) { 2140 let e: BusinessError = error as BusinessError; 2141 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2142 ret = Ret.Failed; 2143 } 2144 return ret; 2145} 2146``` 2147 2148### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 2149 2150off(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback?: Callback<AccelerometerUncalibratedResponse>): void 2151 2152Unsubscribes from data of the uncalibrated acceleration sensor. 2153 2154**Required permissions**: ohos.permission.ACCELEROMETER 2155 2156**System capability**: SystemCapability.Sensors.Sensor 2157 2158**Parameters** 2159 2160| Name | Type | Mandatory| Description | 2161| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2162| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER_UNCALIBRATED**. | 2163| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2164 2165**Error codes** 2166 2167For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2168 2169| ID| Error Message | 2170| -------- | ------------------------------------------------------------ | 2171| 201 | Permission denied. | 2172| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2173 2174**Example** 2175 2176```ts 2177import { sensor } from '@kit.SensorServiceKit'; 2178import { BusinessError } from '@kit.BasicServicesKit'; 2179 2180function callback1(data: object) { 2181 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2182} 2183 2184function callback2(data: object) { 2185 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2186} 2187 2188// Use try catch to capture possible exceptions. 2189try { 2190 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback1); 2191 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback2); 2192 // Unsubscribe from callback1. 2193 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback1); 2194 // Unsubscribe from all callbacks of the SensorId.ACCELEROMETER_UNCALIBRATED type. 2195 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED); 2196} catch (error) { 2197 let e: BusinessError = error as BusinessError; 2198 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2199} 2200``` 2201 2202### ACCELEROMETER_UNCALIBRATED<sup>19+</sup> 2203 2204off(type: SensorId.ACCELEROMETER_UNCALIBRATED, sensorInfoParam?: SensorInfoParam, callback?: Callback<AccelerometerUncalibratedResponse>): void 2205 2206Unsubscribes from data of the uncalibrated acceleration sensor. 2207 2208**Required permissions**: ohos.permission.ACCELEROMETER 2209 2210**System capability**: SystemCapability.Sensors.Sensor 2211 2212**Parameters** 2213 2214| Name | Type | Mandatory| Description | 2215|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2216| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.ACCELEROMETER_UNCALIBRATED**. | 2217| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2218| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2219 2220**Error codes** 2221 2222For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2223 2224| ID| Error Message | 2225| -------- | ------------------------------------------------------------ | 2226| 201 | Permission denied. | 2227| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2228 2229**Example** 2230 2231```ts 2232import { sensor } from '@kit.SensorServiceKit'; 2233import { BusinessError } from '@kit.BasicServicesKit'; 2234 2235enum Ret { OK, Failed = -1 } 2236 2237// Sensor callback 2238const sensorCallback = (response: sensor.AccelerometerUncalibratedResponse) => { 2239 console.log(`callback response: ${JSON.stringify(response)}`); 2240} 2241// Sensor type 2242const sensorType = sensor.SensorId.ACCELEROMETER_UNCALIBRATED; 2243const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2244 2245function sensorSubscribe(): Ret { 2246 let ret: Ret = Ret.OK; 2247 // Use try catch to capture possible exceptions. 2248 try { 2249 // Query all sensors. 2250 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2251 if (!sensorList.length) { 2252 return Ret.Failed; 2253 } 2254 // Obtain the target sensor based on the actual service logic. 2255 const targetSensor = sensorList 2256 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2257 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2258 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2259 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2260 if (!targetSensor) { 2261 return Ret.Failed; 2262 } 2263 sensorInfoParam.deviceId = targetSensor.deviceId; 2264 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2265 // Subscribe to sensor events. 2266 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2267 } catch (error) { 2268 let e: BusinessError = error as BusinessError; 2269 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2270 ret = Ret.Failed; 2271 } 2272 return ret; 2273} 2274 2275function sensorUnsubscribe(): Ret { 2276 let ret: Ret = Ret.OK; 2277 // Use try catch to capture possible exceptions. 2278 try { 2279 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2280 } catch (error) { 2281 let e: BusinessError = error as BusinessError; 2282 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2283 ret = Ret.Failed; 2284 } 2285 return ret; 2286} 2287``` 2288 2289### AMBIENT_LIGHT<sup>9+</sup> 2290 2291off(type: SensorId.AMBIENT_LIGHT, callback?: Callback<LightResponse>): void 2292 2293Unsubscribes from data of the ambient light sensor. 2294 2295**System capability**: SystemCapability.Sensors.Sensor 2296 2297**Parameters** 2298 2299| Name | Type | Mandatory| Description | 2300| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 2301| type | [SensorId](#sensorid9).AMBIENT_LIGHT | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_LIGHT**. | 2302| callback | Callback<[LightResponse](#lightresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2303 2304**Error codes** 2305 2306For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2307 2308| ID| Error Message | 2309| -------- | ------------------------------------------------------------ | 2310| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2311 2312**Example** 2313 2314```ts 2315import { sensor } from '@kit.SensorServiceKit'; 2316import { BusinessError } from '@kit.BasicServicesKit'; 2317 2318function callback1(data: object) { 2319 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2320} 2321 2322function callback2(data: object) { 2323 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2324} 2325 2326// Use try catch to capture possible exceptions. 2327try { 2328 sensor.on(sensor.SensorId.AMBIENT_LIGHT, callback1); 2329 sensor.on(sensor.SensorId.AMBIENT_LIGHT, callback2); 2330 // Unsubscribe from callback1. 2331 sensor.off(sensor.SensorId.AMBIENT_LIGHT, callback1); 2332 // Unsubscribe from all callbacks of the SensorId.AMBIENT_LIGHT type. 2333 sensor.off(sensor.SensorId.AMBIENT_LIGHT); 2334} catch (error) { 2335 let e: BusinessError = error as BusinessError; 2336 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2337} 2338``` 2339 2340### AMBIENT_LIGHT<sup>19+</sup> 2341 2342off(type: SensorId.AMBIENT_LIGHT, sensorInfoParam?: SensorInfoParam, callback?: Callback<LightResponse>): void 2343 2344Unsubscribes from data of the ambient light sensor. 2345 2346**System capability**: SystemCapability.Sensors.Sensor 2347 2348**Parameters** 2349 2350| Name | Type | Mandatory| Description | 2351|------------------| ----------------------------------------------- | ---- | ------------------------------------------------------------ | 2352| type | [SensorId](#sensorid9).AMBIENT_LIGHT | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_LIGHT**. | 2353| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2354| callback | Callback<[LightResponse](#lightresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2355 2356**Error codes** 2357 2358For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2359 2360| ID| Error Message | 2361| -------- | ------------------------------------------------------------ | 2362| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2363 2364**Example** 2365 2366```ts 2367import { sensor } from '@kit.SensorServiceKit'; 2368import { BusinessError } from '@kit.BasicServicesKit'; 2369 2370enum Ret { OK, Failed = -1 } 2371 2372// Sensor callback 2373const sensorCallback = (response: sensor.LightResponse) => { 2374 console.log(`callback response: ${JSON.stringify(response)}`); 2375} 2376// Sensor type 2377const sensorType = sensor.SensorId.AMBIENT_LIGHT; 2378const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2379 2380function sensorSubscribe(): Ret { 2381 let ret: Ret = Ret.OK; 2382 // Use try catch to capture possible exceptions. 2383 try { 2384 // Query all sensors. 2385 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2386 if (!sensorList.length) { 2387 return Ret.Failed; 2388 } 2389 // Obtain the target sensor based on the actual service logic. 2390 const targetSensor = sensorList 2391 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2392 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2393 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2394 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2395 if (!targetSensor) { 2396 return Ret.Failed; 2397 } 2398 sensorInfoParam.deviceId = targetSensor.deviceId; 2399 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2400 // Subscribe to sensor events. 2401 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2402 } catch (error) { 2403 let e: BusinessError = error as BusinessError; 2404 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2405 ret = Ret.Failed; 2406 } 2407 return ret; 2408} 2409 2410function sensorUnsubscribe(): Ret { 2411 let ret: Ret = Ret.OK; 2412 // Use try catch to capture possible exceptions. 2413 try { 2414 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2415 } catch (error) { 2416 let e: BusinessError = error as BusinessError; 2417 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2418 ret = Ret.Failed; 2419 } 2420 return ret; 2421} 2422``` 2423 2424### AMBIENT_TEMPERATURE<sup>9+</sup> 2425 2426off(type: SensorId.AMBIENT_TEMPERATURE, callback?: Callback<AmbientTemperatureResponse>): void 2427 2428Unsubscribes from data of the ambient temperature sensor. 2429 2430**System capability**: SystemCapability.Sensors.Sensor 2431 2432**Parameters** 2433 2434| Name | Type | Mandatory| Description | 2435| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2436| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_TEMPERATURE**. | 2437| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2438 2439**Error codes** 2440 2441For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2442 2443| ID| Error Message | 2444| -------- | ------------------------------------------------------------ | 2445| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2446 2447**Example** 2448 2449```ts 2450import { sensor } from '@kit.SensorServiceKit'; 2451import { BusinessError } from '@kit.BasicServicesKit'; 2452 2453function callback1(data: object) { 2454 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2455} 2456 2457function callback2(data: object) { 2458 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2459} 2460 2461// Use try catch to capture possible exceptions. 2462try { 2463 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, callback1); 2464 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, callback2); 2465 // Unsubscribe from callback1. 2466 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE, callback1); 2467 // Unsubscribe from all callbacks of the SensorId.AMBIENT_TEMPERATURE type. 2468 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE); 2469} catch (error) { 2470 let e: BusinessError = error as BusinessError; 2471 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2472} 2473``` 2474 2475### AMBIENT_TEMPERATURE<sup>19+</sup> 2476 2477off(type: SensorId.AMBIENT_TEMPERATURE, sensorInfoParam?: SensorInfoParam, callback?: Callback<AmbientTemperatureResponse>): void 2478 2479Unsubscribes from data of the ambient temperature sensor. 2480 2481**System capability**: SystemCapability.Sensors.Sensor 2482 2483**Parameters** 2484 2485| Name | Type | Mandatory| Description | 2486|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2487| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | Yes | Sensor type. The value is fixed at **SensorId.AMBIENT_TEMPERATURE**. | 2488| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2489| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2490 2491**Error codes** 2492 2493For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2494 2495| ID| Error Message | 2496| -------- | ------------------------------------------------------------ | 2497| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2498 2499**Example** 2500 2501```ts 2502import { sensor } from '@kit.SensorServiceKit'; 2503import { BusinessError } from '@kit.BasicServicesKit'; 2504 2505enum Ret { OK, Failed = -1 } 2506 2507// Sensor callback 2508const sensorCallback = (response: sensor.AmbientTemperatureResponse) => { 2509 console.log(`callback response: ${JSON.stringify(response)}`); 2510} 2511// Sensor type 2512const sensorType = sensor.SensorId.AMBIENT_TEMPERATURE; 2513const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2514 2515function sensorSubscribe(): Ret { 2516 let ret: Ret = Ret.OK; 2517 // Use try catch to capture possible exceptions. 2518 try { 2519 // Query all sensors. 2520 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2521 if (!sensorList.length) { 2522 return Ret.Failed; 2523 } 2524 // Obtain the target sensor based on the actual service logic. 2525 const targetSensor = sensorList 2526 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2527 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2528 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2529 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2530 if (!targetSensor) { 2531 return Ret.Failed; 2532 } 2533 sensorInfoParam.deviceId = targetSensor.deviceId; 2534 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2535 // Subscribe to sensor events. 2536 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2537 } catch (error) { 2538 let e: BusinessError = error as BusinessError; 2539 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2540 ret = Ret.Failed; 2541 } 2542 return ret; 2543} 2544 2545function sensorUnsubscribe(): Ret { 2546 let ret: Ret = Ret.OK; 2547 // Use try catch to capture possible exceptions. 2548 try { 2549 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2550 } catch (error) { 2551 let e: BusinessError = error as BusinessError; 2552 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2553 ret = Ret.Failed; 2554 } 2555 return ret; 2556} 2557``` 2558 2559 2560### BAROMETER<sup>9+</sup> 2561 2562off(type: SensorId.BAROMETER, callback?: Callback<BarometerResponse>): void 2563 2564Unsubscribes from data of the barometer sensor. 2565 2566**System capability**: SystemCapability.Sensors.Sensor 2567 2568**Parameters** 2569 2570| Name | Type | Mandatory| Description | 2571| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2572| type | [SensorId](#sensorid9).BAROMETER | Yes | Sensor type. The value is fixed at **SensorId.BAROMETER**. | 2573| callback | Callback<[BarometerResponse](#barometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2574 2575**Error codes** 2576 2577For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2578 2579| ID| Error Message | 2580| -------- | ------------------------------------------------------------ | 2581| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2582 2583**Example** 2584 2585```ts 2586import { sensor } from '@kit.SensorServiceKit'; 2587import { BusinessError } from '@kit.BasicServicesKit'; 2588 2589function callback1(data: object) { 2590 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2591} 2592 2593function callback2(data: object) { 2594 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2595} 2596 2597// Use try catch to capture possible exceptions. 2598try { 2599 sensor.on(sensor.SensorId.BAROMETER, callback1); 2600 sensor.on(sensor.SensorId.BAROMETER, callback2); 2601 // Unsubscribe from callback1. 2602 sensor.off(sensor.SensorId.BAROMETER, callback1); 2603 // Unsubscribe from all callbacks of the SensorId.BAROMETER type. 2604 sensor.off(sensor.SensorId.BAROMETER); 2605} catch (error) { 2606 let e: BusinessError = error as BusinessError; 2607 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2608} 2609``` 2610 2611### BAROMETER<sup>19+</sup> 2612 2613off(type: SensorId.BAROMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<BarometerResponse>): void 2614 2615Unsubscribes from data of the barometer sensor. 2616 2617**System capability**: SystemCapability.Sensors.Sensor 2618 2619**Parameters** 2620 2621| Name | Type | Mandatory| Description | 2622|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2623| type | [SensorId](#sensorid9).BAROMETER | Yes | Sensor type. The value is fixed at **SensorId.BAROMETER**. | 2624| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2625| callback | Callback<[BarometerResponse](#barometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2626 2627**Error codes** 2628 2629For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2630 2631| ID| Error Message | 2632| -------- | ------------------------------------------------------------ | 2633| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2634 2635**Example** 2636 2637```ts 2638import { sensor } from '@kit.SensorServiceKit'; 2639import { BusinessError } from '@kit.BasicServicesKit'; 2640 2641enum Ret { OK, Failed = -1 } 2642 2643// Sensor callback 2644const sensorCallback = (response: sensor.BarometerResponse) => { 2645 console.log(`callback response: ${JSON.stringify(response)}`); 2646} 2647// Sensor type 2648const sensorType = sensor.SensorId.BAROMETER; 2649const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2650 2651function sensorSubscribe(): Ret { 2652 let ret: Ret = Ret.OK; 2653 // Use try catch to capture possible exceptions. 2654 try { 2655 // Query all sensors. 2656 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2657 if (!sensorList.length) { 2658 return Ret.Failed; 2659 } 2660 // Obtain the target sensor based on the actual service logic. 2661 const targetSensor = sensorList 2662 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2663 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2664 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2665 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2666 if (!targetSensor) { 2667 return Ret.Failed; 2668 } 2669 sensorInfoParam.deviceId = targetSensor.deviceId; 2670 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2671 // Subscribe to sensor events. 2672 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2673 } catch (error) { 2674 let e: BusinessError = error as BusinessError; 2675 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2676 ret = Ret.Failed; 2677 } 2678 return ret; 2679} 2680 2681function sensorUnsubscribe(): Ret { 2682 let ret: Ret = Ret.OK; 2683 // Use try catch to capture possible exceptions. 2684 try { 2685 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2686 } catch (error) { 2687 let e: BusinessError = error as BusinessError; 2688 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2689 ret = Ret.Failed; 2690 } 2691 return ret; 2692} 2693``` 2694 2695### GRAVITY<sup>9+</sup> 2696 2697off(type: SensorId.GRAVITY, callback?: Callback<GravityResponse>): void 2698 2699Unsubscribes from data of the gravity sensor. 2700 2701**System capability**: SystemCapability.Sensors.Sensor 2702 2703**Parameters** 2704 2705| Name | Type | Mandatory| Description | 2706| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 2707| type | [SensorId](#sensorid9).GRAVITY | Yes | Sensor type. The value is fixed at **SensorId.GRAVITY**. | 2708| callback | Callback<[GravityResponse](#gravityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2709 2710**Error codes** 2711 2712For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2713 2714| ID| Error Message | 2715| -------- | ------------------------------------------------------------ | 2716| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2717 2718**Example** 2719 2720```ts 2721import { sensor } from '@kit.SensorServiceKit'; 2722import { BusinessError } from '@kit.BasicServicesKit'; 2723 2724function callback1(data: object) { 2725 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2726} 2727 2728function callback2(data: object) { 2729 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2730} 2731 2732// Use try catch to capture possible exceptions. 2733try { 2734 sensor.on(sensor.SensorId.GRAVITY, callback1); 2735 sensor.on(sensor.SensorId.GRAVITY, callback2); 2736 // Unsubscribe from callback1. 2737 sensor.off(sensor.SensorId.GRAVITY, callback1); 2738 // Unsubscribe from all callbacks of the SensorId.GRAVITY type. 2739 sensor.off(sensor.SensorId.GRAVITY); 2740} catch (error) { 2741 let e: BusinessError = error as BusinessError; 2742 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2743} 2744 2745``` 2746 2747### GRAVITY<sup>19+</sup> 2748 2749off(type: SensorId.GRAVITY, sensorInfoParam?: SensorInfoParam, callback?: Callback<GravityResponse>): void 2750 2751Unsubscribes from data of the gravity sensor. 2752 2753**System capability**: SystemCapability.Sensors.Sensor 2754 2755**Parameters** 2756 2757| Name | Type | Mandatory| Description | 2758|------------------| --------------------------------------------------- | ---- | ------------------------------------------------------------ | 2759| type | [SensorId](#sensorid9).GRAVITY | Yes | Sensor type. The value is fixed at **SensorId.GRAVITY**. | 2760| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2761| callback | Callback<[GravityResponse](#gravityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2762 2763**Error codes** 2764 2765For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2766 2767| ID| Error Message | 2768| -------- | ------------------------------------------------------------ | 2769| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2770 2771**Example** 2772 2773```ts 2774import { sensor } from '@kit.SensorServiceKit'; 2775import { BusinessError } from '@kit.BasicServicesKit'; 2776 2777enum Ret { OK, Failed = -1 } 2778 2779// Sensor callback 2780const sensorCallback = (response: sensor.GravityResponse) => { 2781 console.log(`callback response: ${JSON.stringify(response)}`); 2782} 2783// Sensor type 2784const sensorType = sensor.SensorId.GRAVITY; 2785const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2786 2787function sensorSubscribe(): Ret { 2788 let ret: Ret = Ret.OK; 2789 // Use try catch to capture possible exceptions. 2790 try { 2791 // Query all sensors. 2792 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2793 if (!sensorList.length) { 2794 return Ret.Failed; 2795 } 2796 // Obtain the target sensor based on the actual service logic. 2797 const targetSensor = sensorList 2798 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2799 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2800 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2801 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2802 if (!targetSensor) { 2803 return Ret.Failed; 2804 } 2805 sensorInfoParam.deviceId = targetSensor.deviceId; 2806 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2807 // Subscribe to sensor events. 2808 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2809 } catch (error) { 2810 let e: BusinessError = error as BusinessError; 2811 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2812 ret = Ret.Failed; 2813 } 2814 return ret; 2815} 2816 2817function sensorUnsubscribe(): Ret { 2818 let ret: Ret = Ret.OK; 2819 // Use try catch to capture possible exceptions. 2820 try { 2821 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2822 } catch (error) { 2823 let e: BusinessError = error as BusinessError; 2824 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2825 ret = Ret.Failed; 2826 } 2827 return ret; 2828} 2829``` 2830 2831### GYROSCOPE<sup>9+</sup> 2832 2833off(type: SensorId.GYROSCOPE, callback?: Callback<GyroscopeResponse>): void 2834 2835Unsubscribes from data of the gyroscope sensor. 2836 2837**Required permissions**: ohos.permission.GYROSCOPE 2838 2839**Atomic service API**: This API can be used in atomic services since API version 11. 2840 2841**System capability**: SystemCapability.Sensors.Sensor 2842 2843**Parameters** 2844 2845| Name | Type | Mandatory| Description | 2846| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2847| type | [SensorId](#sensorid9).GYROSCOPE | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE**. | 2848| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2849 2850**Error codes** 2851 2852For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2853 2854| ID| Error Message | 2855| -------- | ------------------------------------------------------------ | 2856| 201 | Permission denied. | 2857| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2858 2859**Example** 2860 2861```ts 2862import { sensor } from '@kit.SensorServiceKit'; 2863import { BusinessError } from '@kit.BasicServicesKit'; 2864 2865function callback1(data: object) { 2866 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2867} 2868 2869function callback2(data: object) { 2870 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2871} 2872 2873// Use try catch to capture possible exceptions. 2874try { 2875 sensor.on(sensor.SensorId.GYROSCOPE, callback1); 2876 sensor.on(sensor.SensorId.GYROSCOPE, callback2); 2877 // Unsubscribe from callback1. 2878 sensor.off(sensor.SensorId.GYROSCOPE, callback1); 2879 // Unsubscribe from all callbacks of the SensorId.GYROSCOPE type. 2880 sensor.off(sensor.SensorId.GYROSCOPE); 2881} catch (error) { 2882 let e: BusinessError = error as BusinessError; 2883 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2884} 2885``` 2886 2887### GYROSCOPE<sup>19+</sup> 2888 2889off(type: SensorId.GYROSCOPE, sensorInfoParam?: SensorInfoParam, callback?: Callback<GyroscopeResponse>): void 2890 2891Unsubscribes from data of the gyroscope sensor. 2892 2893**Required permissions**: ohos.permission.GYROSCOPE 2894 2895**Atomic service API**: This API can be used in atomic services since API version 19. 2896 2897**System capability**: SystemCapability.Sensors.Sensor 2898 2899**Parameters** 2900 2901| Name | Type | Mandatory| Description | 2902|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2903| type | [SensorId](#sensorid9).GYROSCOPE | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE**. | 2904| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 2905| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2906 2907**Error codes** 2908 2909For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2910 2911| ID| Error Message | 2912| -------- | ------------------------------------------------------------ | 2913| 201 | Permission denied. | 2914| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2915 2916**Example** 2917 2918```ts 2919import { sensor } from '@kit.SensorServiceKit'; 2920import { BusinessError } from '@kit.BasicServicesKit'; 2921 2922enum Ret { OK, Failed = -1 } 2923 2924// Sensor callback 2925const sensorCallback = (response: sensor.GyroscopeResponse) => { 2926 console.log(`callback response: ${JSON.stringify(response)}`); 2927} 2928// Sensor type 2929const sensorType = sensor.SensorId.GYROSCOPE; 2930const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2931 2932function sensorSubscribe(): Ret { 2933 let ret: Ret = Ret.OK; 2934 // Use try catch to capture possible exceptions. 2935 try { 2936 // Query all sensors. 2937 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2938 if (!sensorList.length) { 2939 return Ret.Failed; 2940 } 2941 // Obtain the target sensor based on the actual service logic. 2942 const targetSensor = sensorList 2943 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 2944 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2945 // Select the sensor with sensorIndex 0 among all sensors of the same type. 2946 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2947 if (!targetSensor) { 2948 return Ret.Failed; 2949 } 2950 sensorInfoParam.deviceId = targetSensor.deviceId; 2951 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2952 // Subscribe to sensor events. 2953 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2954 } catch (error) { 2955 let e: BusinessError = error as BusinessError; 2956 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2957 ret = Ret.Failed; 2958 } 2959 return ret; 2960} 2961 2962function sensorUnsubscribe(): Ret { 2963 let ret: Ret = Ret.OK; 2964 // Use try catch to capture possible exceptions. 2965 try { 2966 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2967 } catch (error) { 2968 let e: BusinessError = error as BusinessError; 2969 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2970 ret = Ret.Failed; 2971 } 2972 return ret; 2973} 2974``` 2975 2976### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 2977 2978off(type: SensorId.GYROSCOPE_UNCALIBRATED, callback?: Callback<GyroscopeUncalibratedResponse>): void 2979 2980 Unsubscribes from data of the uncalibrated gyroscope sensor. 2981 2982**Required permissions**: ohos.permission.GYROSCOPE 2983 2984**System capability**: SystemCapability.Sensors.Sensor 2985 2986**Parameters** 2987 2988| Name | Type | Mandatory| Description | 2989| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2990| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE_UNCALIBRATED**. | 2991| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 2992 2993**Error codes** 2994 2995For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 2996 2997| ID| Error Message | 2998| -------- | ------------------------------------------------------------ | 2999| 201 | Permission denied. | 3000| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3001 3002**Example** 3003 3004```ts 3005import { sensor } from '@kit.SensorServiceKit'; 3006import { BusinessError } from '@kit.BasicServicesKit'; 3007 3008function callback1(data: object) { 3009 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3010} 3011 3012function callback2(data: object) { 3013 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3014} 3015 3016// Use try catch to capture possible exceptions. 3017try { 3018 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback1); 3019 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback2); 3020 // Unsubscribe from callback1. 3021 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback1); 3022 // Unsubscribe from all callbacks of the SensorId.GYROSCOPE_UNCALIBRATED type. 3023 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED); 3024} catch (error) { 3025 let e: BusinessError = error as BusinessError; 3026 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3027} 3028``` 3029 3030### GYROSCOPE_UNCALIBRATED<sup>19+</sup> 3031 3032off(type: SensorId.GYROSCOPE_UNCALIBRATED, sensorInfoParam?: SensorInfoParam, callback?: Callback<GyroscopeUncalibratedResponse>): void 3033 3034Unsubscribes from data of the uncalibrated gyroscope sensor. 3035 3036**Required permissions**: ohos.permission.GYROSCOPE 3037 3038**System capability**: SystemCapability.Sensors.Sensor 3039 3040**Parameters** 3041 3042| Name | Type | Mandatory| Description | 3043|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3044| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.GYROSCOPE_UNCALIBRATED**. | 3045| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3046| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3047 3048**Error codes** 3049 3050For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3051 3052| ID| Error Message | 3053| -------- | ------------------------------------------------------------ | 3054| 201 | Permission denied. | 3055| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3056 3057**Example** 3058 3059```ts 3060import { sensor } from '@kit.SensorServiceKit'; 3061import { BusinessError } from '@kit.BasicServicesKit'; 3062 3063enum Ret { OK, Failed = -1 } 3064 3065// Sensor callback 3066const sensorCallback = (response: sensor.GyroscopeUncalibratedResponse) => { 3067 console.log(`callback response: ${JSON.stringify(response)}`); 3068} 3069// Sensor type 3070const sensorType = sensor.SensorId.GYROSCOPE_UNCALIBRATED; 3071const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3072 3073function sensorSubscribe(): Ret { 3074 let ret: Ret = Ret.OK; 3075 // Use try catch to capture possible exceptions. 3076 try { 3077 // Query all sensors. 3078 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3079 if (!sensorList.length) { 3080 return Ret.Failed; 3081 } 3082 // Obtain the target sensor based on the actual service logic. 3083 const targetSensor = sensorList 3084 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3085 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3086 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3087 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3088 if (!targetSensor) { 3089 return Ret.Failed; 3090 } 3091 sensorInfoParam.deviceId = targetSensor.deviceId; 3092 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3093 // Subscribe to sensor events. 3094 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3095 } catch (error) { 3096 let e: BusinessError = error as BusinessError; 3097 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3098 ret = Ret.Failed; 3099 } 3100 return ret; 3101} 3102 3103function sensorUnsubscribe(): Ret { 3104 let ret: Ret = Ret.OK; 3105 // Use try catch to capture possible exceptions. 3106 try { 3107 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3108 } catch (error) { 3109 let e: BusinessError = error as BusinessError; 3110 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3111 ret = Ret.Failed; 3112 } 3113 return ret; 3114} 3115``` 3116 3117### HALL<sup>9+</sup> 3118 3119off(type: SensorId.HALL, callback?: Callback<HallResponse>): void 3120 3121Unsubscribes from data of the Hall effect sensor. 3122 3123**System capability**: SystemCapability.Sensors.Sensor 3124 3125**Parameters** 3126 3127| Name | Type | Mandatory| Description | 3128| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 3129| type | [SensorId](#sensorid9).HALL | Yes | Sensor type. The value is fixed at **SensorId.HALL**. | 3130| callback | Callback<[HallResponse](#hallresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3131 3132**Error codes** 3133 3134For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3135 3136| ID| Error Message | 3137| -------- | ------------------------------------------------------------ | 3138| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3139 3140**Example** 3141 3142```ts 3143import { sensor } from '@kit.SensorServiceKit'; 3144import { BusinessError } from '@kit.BasicServicesKit'; 3145 3146function callback1(data: object) { 3147 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3148} 3149 3150function callback2(data: object) { 3151 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3152} 3153 3154// Use try catch to capture possible exceptions. 3155try { 3156 sensor.on(sensor.SensorId.HALL, callback1); 3157 sensor.on(sensor.SensorId.HALL, callback2); 3158 // Unsubscribe from callback1. 3159 sensor.off(sensor.SensorId.HALL, callback1); 3160 // Unsubscribe from all callbacks of the SensorId.HALL type. 3161 sensor.off(sensor.SensorId.HALL); 3162} catch (error) { 3163 let e: BusinessError = error as BusinessError; 3164 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3165} 3166``` 3167 3168### HALL<sup>19+</sup> 3169 3170off(type: SensorId.HALL, sensorInfoParam?: SensorInfoParam, callback?: Callback<HallResponse>): void 3171 3172Unsubscribes from data of the Hall effect sensor. 3173 3174**System capability**: SystemCapability.Sensors.Sensor 3175 3176**Parameters** 3177 3178| Name | Type | Mandatory| Description | 3179|------------------| --------------------------------------------- | ---- | ------------------------------------------------------------ | 3180| type | [SensorId](#sensorid9).HALL | Yes | Sensor type. The value is fixed at **SensorId.HALL**. | 3181| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3182| callback | Callback<[HallResponse](#hallresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3183 3184**Error codes** 3185 3186For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3187 3188| ID| Error Message | 3189| -------- | ------------------------------------------------------------ | 3190| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3191 3192**Example** 3193 3194```ts 3195import { sensor } from '@kit.SensorServiceKit'; 3196import { BusinessError } from '@kit.BasicServicesKit'; 3197 3198enum Ret { OK, Failed = -1 } 3199 3200// Sensor callback 3201const sensorCallback = (response: sensor.HallResponse) => { 3202 console.log(`callback response: ${JSON.stringify(response)}`); 3203} 3204// Sensor type 3205const sensorType = sensor.SensorId.HALL; 3206const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3207 3208function sensorSubscribe(): Ret { 3209 let ret: Ret = Ret.OK; 3210 // Use try catch to capture possible exceptions. 3211 try { 3212 // Query all sensors. 3213 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3214 if (!sensorList.length) { 3215 return Ret.Failed; 3216 } 3217 // Obtain the target sensor based on the actual service logic. 3218 const targetSensor = sensorList 3219 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3220 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3221 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3222 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3223 if (!targetSensor) { 3224 return Ret.Failed; 3225 } 3226 sensorInfoParam.deviceId = targetSensor.deviceId; 3227 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3228 // Subscribe to sensor events. 3229 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3230 } catch (error) { 3231 let e: BusinessError = error as BusinessError; 3232 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3233 ret = Ret.Failed; 3234 } 3235 return ret; 3236} 3237 3238function sensorUnsubscribe(): Ret { 3239 let ret: Ret = Ret.OK; 3240 // Use try catch to capture possible exceptions. 3241 try { 3242 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3243 } catch (error) { 3244 let e: BusinessError = error as BusinessError; 3245 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3246 ret = Ret.Failed; 3247 } 3248 return ret; 3249} 3250``` 3251 3252### HEART_RATE<sup>9+</sup> 3253 3254off(type: SensorId.HEART_RATE, callback?: Callback<HeartRateResponse>): void 3255 3256Unsubscribes from data of the heart rate sensor. 3257 3258**Required permissions**: ohos.permission.READ_HEALTH_DATA 3259 3260**System capability**: SystemCapability.Sensors.Sensor 3261 3262**Parameters** 3263 3264| Name | Type | Mandatory| Description | 3265| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3266| type | [SensorId](#sensorid9).HEART_RATE | Yes | Sensor type. The value is fixed at **SensorId.HEART_RATE**. | 3267| callback | Callback<[HeartRateResponse](#heartrateresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3268 3269**Error codes** 3270 3271For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3272 3273| ID| Error Message | 3274| -------- | ------------------------------------------------------------ | 3275| 201 | Permission denied. | 3276| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3277 3278**Example** 3279 3280```ts 3281import { sensor } from '@kit.SensorServiceKit'; 3282import { BusinessError } from '@kit.BasicServicesKit'; 3283 3284function callback1(data: object) { 3285 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3286} 3287 3288function callback2(data: object) { 3289 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3290} 3291 3292// Use try catch to capture possible exceptions. 3293try { 3294 sensor.on(sensor.SensorId.HEART_RATE, callback1); 3295 sensor.on(sensor.SensorId.HEART_RATE, callback2); 3296 // Unsubscribe from callback1. 3297 sensor.off(sensor.SensorId.HEART_RATE, callback1); 3298 // Unsubscribe from all callbacks of the SensorId.HEART_RATE type. 3299 sensor.off(sensor.SensorId.HEART_RATE); 3300} catch (error) { 3301 let e: BusinessError = error as BusinessError; 3302 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3303} 3304``` 3305 3306### HEART_RATE<sup>19+</sup> 3307 3308off(type: SensorId.HEART_RATE, sensorInfoParam?: SensorInfoParam, callback?: Callback<HeartRateResponse>): void 3309 3310Unsubscribes from data of the heart rate sensor. 3311 3312**Required permissions**: ohos.permission.READ_HEALTH_DATA 3313 3314**System capability**: SystemCapability.Sensors.Sensor 3315 3316**Parameters** 3317 3318| Name | Type | Mandatory| Description | 3319|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3320| type | [SensorId](#sensorid9).HEART_RATE | Yes | Sensor type. The value is fixed at **SensorId.HEART_RATE**. | 3321| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3322| callback | Callback<[HeartRateResponse](#heartrateresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3323 3324**Error codes** 3325 3326For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3327 3328| ID| Error Message | 3329| -------- | ------------------------------------------------------------ | 3330| 201 | Permission denied. | 3331| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3332 3333**Example** 3334 3335```ts 3336import { sensor } from '@kit.SensorServiceKit'; 3337import { BusinessError } from '@kit.BasicServicesKit'; 3338 3339enum Ret { OK, Failed = -1 } 3340 3341// Sensor callback 3342const sensorCallback = (response: sensor.HeartRateResponse) => { 3343 console.log(`callback response: ${JSON.stringify(response)}`); 3344} 3345// Sensor type 3346const sensorType = sensor.SensorId.HEART_RATE; 3347const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3348 3349function sensorSubscribe(): Ret { 3350 let ret: Ret = Ret.OK; 3351 // Use try catch to capture possible exceptions. 3352 try { 3353 // Query all sensors. 3354 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3355 if (!sensorList.length) { 3356 return Ret.Failed; 3357 } 3358 // Obtain the target sensor based on the actual service logic. 3359 const targetSensor = sensorList 3360 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3361 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3362 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3363 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3364 if (!targetSensor) { 3365 return Ret.Failed; 3366 } 3367 sensorInfoParam.deviceId = targetSensor.deviceId; 3368 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3369 // Subscribe to sensor events. 3370 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3371 } catch (error) { 3372 let e: BusinessError = error as BusinessError; 3373 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3374 ret = Ret.Failed; 3375 } 3376 return ret; 3377} 3378 3379function sensorUnsubscribe(): Ret { 3380 let ret: Ret = Ret.OK; 3381 // Use try catch to capture possible exceptions. 3382 try { 3383 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3384 } catch (error) { 3385 let e: BusinessError = error as BusinessError; 3386 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3387 ret = Ret.Failed; 3388 } 3389 return ret; 3390} 3391``` 3392 3393### HUMIDITY<sup>9+</sup> 3394 3395off(type: SensorId.HUMIDITY, callback?: Callback<HumidityResponse>): void 3396 3397Unsubscribes from data of the humidity sensor. 3398 3399**System capability**: SystemCapability.Sensors.Sensor 3400 3401**Parameters** 3402 3403| Name | Type | Mandatory| Description | 3404| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 3405| type | [SensorId](#sensorid9).HUMIDITY | Yes | Sensor type. The value is fixed at **SensorId.HUMIDITY**. | 3406| callback | Callback<[HumidityResponse](#humidityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3407 3408**Error codes** 3409 3410For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3411 3412| ID| Error Message | 3413| -------- | ------------------------------------------------------------ | 3414| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3415 3416**Example** 3417 3418```ts 3419import { sensor } from '@kit.SensorServiceKit'; 3420import { BusinessError } from '@kit.BasicServicesKit'; 3421 3422function callback1(data: object) { 3423 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3424} 3425 3426function callback2(data: object) { 3427 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3428} 3429 3430// Use try catch to capture possible exceptions. 3431try { 3432 sensor.on(sensor.SensorId.HUMIDITY, callback1); 3433 sensor.on(sensor.SensorId.HUMIDITY, callback2); 3434 // Unsubscribe from callback1. 3435 sensor.off(sensor.SensorId.HUMIDITY, callback1); 3436 // Unsubscribe from all callbacks of the SensorId.HUMIDITY type. 3437 sensor.off(sensor.SensorId.HUMIDITY); 3438} catch (error) { 3439 let e: BusinessError = error as BusinessError; 3440 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3441} 3442``` 3443 3444### HUMIDITY<sup>19+</sup> 3445 3446off(type: SensorId.HUMIDITY, sensorInfoParam?: SensorInfoParam, callback?: Callback<HumidityResponse>): void 3447 3448Unsubscribes from data of the humidity sensor. 3449 3450**System capability**: SystemCapability.Sensors.Sensor 3451 3452**Parameters** 3453 3454| Name | Type | Mandatory| Description | 3455|------------------| ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 3456| type | [SensorId](#sensorid9).HUMIDITY | Yes | Sensor type. The value is fixed at **SensorId.HUMIDITY**. | 3457| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3458| callback | Callback<[HumidityResponse](#humidityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3459 3460**Error codes** 3461 3462For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3463 3464| ID| Error Message | 3465| -------- | ------------------------------------------------------------ | 3466| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3467 3468**Example** 3469 3470```ts 3471import { sensor } from '@kit.SensorServiceKit'; 3472import { BusinessError } from '@kit.BasicServicesKit'; 3473 3474enum Ret { OK, Failed = -1 } 3475 3476// Sensor callback 3477const sensorCallback = (response: sensor.HumidityResponse) => { 3478 console.log(`callback response: ${JSON.stringify(response)}`); 3479} 3480// Sensor type 3481const sensorType = sensor.SensorId.HUMIDITY; 3482const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3483 3484function sensorSubscribe(): Ret { 3485 let ret: Ret = Ret.OK; 3486 // Use try catch to capture possible exceptions. 3487 try { 3488 // Query all sensors. 3489 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3490 if (!sensorList.length) { 3491 return Ret.Failed; 3492 } 3493 // Obtain the target sensor based on the actual service logic. 3494 const targetSensor = sensorList 3495 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3496 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3497 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3498 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3499 if (!targetSensor) { 3500 return Ret.Failed; 3501 } 3502 sensorInfoParam.deviceId = targetSensor.deviceId; 3503 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3504 // Subscribe to sensor events. 3505 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3506 } catch (error) { 3507 let e: BusinessError = error as BusinessError; 3508 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3509 ret = Ret.Failed; 3510 } 3511 return ret; 3512} 3513 3514function sensorUnsubscribe(): Ret { 3515 let ret: Ret = Ret.OK; 3516 // Use try catch to capture possible exceptions. 3517 try { 3518 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3519 } catch (error) { 3520 let e: BusinessError = error as BusinessError; 3521 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3522 ret = Ret.Failed; 3523 } 3524 return ret; 3525} 3526``` 3527 3528### LINEAR_ACCELEROMETER<sup>9+</sup> 3529 3530off(type: SensorId.LINEAR_ACCELEROMETER, callback?: Callback<LinearAccelerometerResponse>): void 3531 3532Unsubscribes from data of the linear acceleration sensor. 3533 3534**Required permissions**: ohos.permission.ACCELEROMETER 3535 3536**System capability**: SystemCapability.Sensors.Sensor 3537 3538**Parameters** 3539 3540| Name | Type | Mandatory| Description | 3541| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3542| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.LINEAR_ACCELERATION**. | 3543| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3544 3545**Error codes** 3546 3547For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3548 3549| ID| Error Message | 3550| -------- | ------------------------------------------------------------ | 3551| 201 | Permission denied. | 3552| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3553 3554**Example** 3555 3556```ts 3557import { sensor } from '@kit.SensorServiceKit'; 3558import { BusinessError } from '@kit.BasicServicesKit'; 3559 3560function callback1(data: object) { 3561 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3562} 3563 3564function callback2(data: object) { 3565 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3566} 3567 3568// Use try catch to capture possible exceptions. 3569try { 3570 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, callback1); 3571 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, callback2); 3572 // Unsubscribe from callback1. 3573 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER, callback1); 3574 // Unsubscribe from all callbacks of the SensorId.LINEAR_ACCELEROMETER type. 3575 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER); 3576} catch (error) { 3577 let e: BusinessError = error as BusinessError; 3578 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3579} 3580``` 3581 3582### LINEAR_ACCELEROMETER<sup>19+</sup> 3583 3584off(type: SensorId.LINEAR_ACCELEROMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<LinearAccelerometerResponse>): void 3585 3586Unsubscribes from data of the linear acceleration sensor. 3587 3588**Required permissions**: ohos.permission.ACCELEROMETER 3589 3590**System capability**: SystemCapability.Sensors.Sensor 3591 3592**Parameters** 3593 3594| Name | Type | Mandatory| Description | 3595|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3596| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | Yes | Sensor type. The value is fixed at **SensorId.LINEAR_ACCELERATION**. | 3597| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3598| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3599 3600**Error codes** 3601 3602For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3603 3604| ID| Error Message | 3605| -------- | ------------------------------------------------------------ | 3606| 201 | Permission denied. | 3607| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3608 3609**Example** 3610 3611```ts 3612import { sensor } from '@kit.SensorServiceKit'; 3613import { BusinessError } from '@kit.BasicServicesKit'; 3614 3615enum Ret { OK, Failed = -1 } 3616 3617// Sensor callback 3618const sensorCallback = (response: sensor.LinearAccelerometerResponse) => { 3619 console.log(`callback response: ${JSON.stringify(response)}`); 3620} 3621// Sensor type 3622const sensorType = sensor.SensorId.LINEAR_ACCELEROMETER; 3623const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3624 3625function sensorSubscribe(): Ret { 3626 let ret: Ret = Ret.OK; 3627 // Use try catch to capture possible exceptions. 3628 try { 3629 // Query all sensors. 3630 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3631 if (!sensorList.length) { 3632 return Ret.Failed; 3633 } 3634 // Obtain the target sensor based on the actual service logic. 3635 const targetSensor = sensorList 3636 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3637 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3638 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3639 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3640 if (!targetSensor) { 3641 return Ret.Failed; 3642 } 3643 sensorInfoParam.deviceId = targetSensor.deviceId; 3644 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3645 // Subscribe to sensor events. 3646 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3647 } catch (error) { 3648 let e: BusinessError = error as BusinessError; 3649 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3650 ret = Ret.Failed; 3651 } 3652 return ret; 3653} 3654 3655function sensorUnsubscribe(): Ret { 3656 let ret: Ret = Ret.OK; 3657 // Use try catch to capture possible exceptions. 3658 try { 3659 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3660 } catch (error) { 3661 let e: BusinessError = error as BusinessError; 3662 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3663 ret = Ret.Failed; 3664 } 3665 return ret; 3666} 3667``` 3668 3669### MAGNETIC_FIELD<sup>9+</sup> 3670 3671off(type: SensorId.MAGNETIC_FIELD, callback?: Callback<MagneticFieldResponse>): void 3672 3673Unsubscribes from data of the magnetic field sensor. 3674 3675**System capability**: SystemCapability.Sensors.Sensor 3676 3677**Parameters** 3678 3679| Name | Type | Mandatory| Description | 3680| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3681| type | [SensorId](#sensorid9).MAGNETIC_FIELD | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD**. | 3682| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3683 3684**Error codes** 3685 3686For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3687 3688| ID| Error Message | 3689| -------- | ------------------------------------------------------------ | 3690| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3691 3692**Example** 3693 3694```ts 3695import { sensor } from '@kit.SensorServiceKit'; 3696import { BusinessError } from '@kit.BasicServicesKit'; 3697 3698function callback1(data: object) { 3699 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3700} 3701 3702function callback2(data: object) { 3703 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3704} 3705 3706// Use try catch to capture possible exceptions. 3707try { 3708 sensor.on(sensor.SensorId.MAGNETIC_FIELD, callback1); 3709 sensor.on(sensor.SensorId.MAGNETIC_FIELD, callback2); 3710 // Unsubscribe from callback1. 3711 sensor.off(sensor.SensorId.MAGNETIC_FIELD, callback1); 3712 // Unsubscribe from all callbacks of the SensorId.MAGNETIC_FIELD type. 3713 sensor.off(sensor.SensorId.MAGNETIC_FIELD); 3714} catch (error) { 3715 let e: BusinessError = error as BusinessError; 3716 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3717} 3718``` 3719 3720### MAGNETIC_FIELD<sup>19+</sup> 3721 3722off(type: SensorId.MAGNETIC_FIELD, sensorInfoParam?: SensorInfoParam, callback?: Callback<MagneticFieldResponse>): void 3723 3724Unsubscribes from data of the magnetic field sensor. 3725 3726**System capability**: SystemCapability.Sensors.Sensor 3727 3728**Parameters** 3729 3730| Name | Type | Mandatory| Description | 3731|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3732| type | [SensorId](#sensorid9).MAGNETIC_FIELD | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD**. | 3733| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3734| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3735 3736**Error codes** 3737 3738For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3739 3740| ID| Error Message | 3741| -------- | ------------------------------------------------------------ | 3742| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3743 3744**Example** 3745 3746```ts 3747import { sensor } from '@kit.SensorServiceKit'; 3748import { BusinessError } from '@kit.BasicServicesKit'; 3749 3750enum Ret { OK, Failed = -1 } 3751 3752// Sensor callback 3753const sensorCallback = (response: sensor.MagneticFieldResponse) => { 3754 console.log(`callback response: ${JSON.stringify(response)}`); 3755} 3756// Sensor type 3757const sensorType = sensor.SensorId.MAGNETIC_FIELD; 3758const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3759 3760function sensorSubscribe(): Ret { 3761 let ret: Ret = Ret.OK; 3762 // Use try catch to capture possible exceptions. 3763 try { 3764 // Query all sensors. 3765 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3766 if (!sensorList.length) { 3767 return Ret.Failed; 3768 } 3769 // Obtain the target sensor based on the actual service logic. 3770 const targetSensor = sensorList 3771 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3772 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3773 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3774 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3775 if (!targetSensor) { 3776 return Ret.Failed; 3777 } 3778 sensorInfoParam.deviceId = targetSensor.deviceId; 3779 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3780 // Subscribe to sensor events. 3781 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3782 } catch (error) { 3783 let e: BusinessError = error as BusinessError; 3784 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3785 ret = Ret.Failed; 3786 } 3787 return ret; 3788} 3789 3790function sensorUnsubscribe(): Ret { 3791 let ret: Ret = Ret.OK; 3792 // Use try catch to capture possible exceptions. 3793 try { 3794 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3795 } catch (error) { 3796 let e: BusinessError = error as BusinessError; 3797 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3798 ret = Ret.Failed; 3799 } 3800 return ret; 3801} 3802``` 3803 3804### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 3805 3806off(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback?: Callback<MagneticFieldUncalibratedResponse>): void 3807 3808Unsubscribes from data of the uncalibrated magnetic field sensor. 3809 3810**System capability**: SystemCapability.Sensors.Sensor 3811 3812**Parameters** 3813 3814| Name | Type | Mandatory| Description | 3815| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3816| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD_UNCALIBRATED**.| 3817| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3818 3819**Error codes** 3820 3821For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3822 3823| ID| Error Message | 3824| -------- | ------------------------------------------------------------ | 3825| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3826 3827**Example** 3828 3829```ts 3830import { sensor } from '@kit.SensorServiceKit'; 3831import { BusinessError } from '@kit.BasicServicesKit'; 3832 3833function callback1(data: object) { 3834 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3835} 3836 3837function callback2(data: object) { 3838 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3839} 3840 3841// Use try catch to capture possible exceptions. 3842try { 3843 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback1); 3844 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback2); 3845 // Unsubscribe from callback1. 3846 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback1); 3847 // Unsubscribe from all callbacks of the SensorId.MAGNETIC_FIELD_UNCALIBRATED type. 3848 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED); 3849} catch (error) { 3850 let e: BusinessError = error as BusinessError; 3851 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3852} 3853``` 3854 3855### MAGNETIC_FIELD_UNCALIBRATED<sup>19+</sup> 3856 3857off(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, sensorInfoParam?: SensorInfoParam, callback?: Callback<MagneticFieldUncalibratedResponse>): void 3858 3859Unsubscribes from data of the uncalibrated magnetic field sensor. 3860 3861**System capability**: SystemCapability.Sensors.Sensor 3862 3863**Parameters** 3864 3865| Name | Type | Mandatory| Description | 3866|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3867| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | Yes | Sensor type. The value is fixed at **SensorId.MAGNETIC_FIELD_UNCALIBRATED**.| 3868| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 3869| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3870 3871**Error codes** 3872 3873For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3874 3875| ID| Error Message | 3876| -------- | ------------------------------------------------------------ | 3877| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3878 3879**Example** 3880 3881```ts 3882import { sensor } from '@kit.SensorServiceKit'; 3883import { BusinessError } from '@kit.BasicServicesKit'; 3884 3885enum Ret { OK, Failed = -1 } 3886 3887// Sensor callback 3888const sensorCallback = (response: sensor.MagneticFieldUncalibratedResponse) => { 3889 console.log(`callback response: ${JSON.stringify(response)}`); 3890} 3891// Sensor type 3892const sensorType = sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED; 3893const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3894 3895function sensorSubscribe(): Ret { 3896 let ret: Ret = Ret.OK; 3897 // Use try catch to capture possible exceptions. 3898 try { 3899 // Query all sensors. 3900 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3901 if (!sensorList.length) { 3902 return Ret.Failed; 3903 } 3904 // Obtain the target sensor based on the actual service logic. 3905 const targetSensor = sensorList 3906 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 3907 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3908 // Select the sensor with sensorIndex 0 among all sensors of the same type. 3909 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3910 if (!targetSensor) { 3911 return Ret.Failed; 3912 } 3913 sensorInfoParam.deviceId = targetSensor.deviceId; 3914 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3915 // Subscribe to sensor events. 3916 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3917 } catch (error) { 3918 let e: BusinessError = error as BusinessError; 3919 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3920 ret = Ret.Failed; 3921 } 3922 return ret; 3923} 3924 3925function sensorUnsubscribe(): Ret { 3926 let ret: Ret = Ret.OK; 3927 // Use try catch to capture possible exceptions. 3928 try { 3929 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3930 } catch (error) { 3931 let e: BusinessError = error as BusinessError; 3932 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3933 ret = Ret.Failed; 3934 } 3935 return ret; 3936} 3937``` 3938 3939### ORIENTATION<sup>9+</sup> 3940 3941off(type: SensorId.ORIENTATION, callback?: Callback<OrientationResponse>): void 3942 3943Unsubscribes from data of the orientation sensor. 3944 3945**Atomic service API**: This API can be used in atomic services since API version 11. 3946 3947**System capability**: SystemCapability.Sensors.Sensor 3948 3949**Parameters** 3950 3951| Name | Type | Mandatory| Description | 3952| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3953| type | [SensorId](#sensorid9).ORIENTATION | Yes | Sensor type. The value is fixed at **SensorId.ORIENTATION**. | 3954| callback | Callback<[OrientationResponse](#orientationresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 3955 3956**Error codes** 3957 3958For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 3959 3960| ID| Error Message | 3961| -------- | ------------------------------------------------------------ | 3962| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3963 3964**Example** 3965 3966```ts 3967import { sensor } from '@kit.SensorServiceKit'; 3968import { BusinessError } from '@kit.BasicServicesKit'; 3969 3970function callback1(data: object) { 3971 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3972} 3973 3974function callback2(data: object) { 3975 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3976} 3977 3978// Use try catch to capture possible exceptions. 3979try { 3980 sensor.on(sensor.SensorId.ORIENTATION, callback1); 3981 sensor.on(sensor.SensorId.ORIENTATION, callback2); 3982 // Unsubscribe from callback1. 3983 sensor.off(sensor.SensorId.ORIENTATION, callback1); 3984 // Unsubscribe from all callbacks of the SensorId.ORIENTATION type. 3985 sensor.off(sensor.SensorId.ORIENTATION); 3986} catch (error) { 3987 let e: BusinessError = error as BusinessError; 3988 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3989} 3990``` 3991 3992### ORIENTATION<sup>19+</sup> 3993 3994off(type: SensorId.ORIENTATION, sensorInfoParam?: SensorInfoParam, callback?: Callback<OrientationResponse>): void 3995 3996Unsubscribes from data of the orientation sensor. 3997 3998**Atomic service API**: This API can be used in atomic services since API version 19. 3999 4000**System capability**: SystemCapability.Sensors.Sensor 4001 4002**Parameters** 4003 4004| Name | Type | Mandatory| Description | 4005| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4006| type | [SensorId](#sensorid9).ORIENTATION | Yes | Sensor type. The value is fixed at **SensorId.ORIENTATION**. | 4007| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4008| callback | Callback<[OrientationResponse](#orientationresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4009 4010**Error codes** 4011 4012For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4013 4014| ID| Error Message | 4015| -------- | ------------------------------------------------------------ | 4016| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4017 4018**Example** 4019 4020```ts 4021import { sensor } from '@kit.SensorServiceKit'; 4022import { BusinessError } from '@kit.BasicServicesKit'; 4023 4024enum Ret { OK, Failed = -1 } 4025 4026// Sensor callback 4027const sensorCallback = (response: sensor.OrientationResponse) => { 4028 console.log(`callback response: ${JSON.stringify(response)}`); 4029} 4030// Sensor type 4031const sensorType = sensor.SensorId.ORIENTATION; 4032const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4033 4034function sensorSubscribe(): Ret { 4035 let ret: Ret = Ret.OK; 4036 // Use try catch to capture possible exceptions. 4037 try { 4038 // Query all sensors. 4039 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4040 if (!sensorList.length) { 4041 return Ret.Failed; 4042 } 4043 // Obtain the target sensor based on the actual service logic. 4044 const targetSensor = sensorList 4045 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4046 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4047 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4048 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4049 if (!targetSensor) { 4050 return Ret.Failed; 4051 } 4052 sensorInfoParam.deviceId = targetSensor.deviceId; 4053 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4054 // Subscribe to sensor events. 4055 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4056 } catch (error) { 4057 let e: BusinessError = error as BusinessError; 4058 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4059 ret = Ret.Failed; 4060 } 4061 return ret; 4062} 4063 4064function sensorUnsubscribe(): Ret { 4065 let ret: Ret = Ret.OK; 4066 // Use try catch to capture possible exceptions. 4067 try { 4068 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4069 } catch (error) { 4070 let e: BusinessError = error as BusinessError; 4071 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4072 ret = Ret.Failed; 4073 } 4074 return ret; 4075} 4076``` 4077 4078### PEDOMETER<sup>9+</sup> 4079 4080off(type: SensorId.PEDOMETER, callback?: Callback<PedometerResponse>): void 4081 4082Unsubscribes from data of the pedometer sensor. 4083 4084**Required permissions**: ohos.permission.ACTIVITY_MOTION 4085 4086**System capability**: SystemCapability.Sensors.Sensor 4087 4088**Parameters** 4089 4090| Name | Type | Mandatory| Description | 4091| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4092| type | [SensorId](#sensorid9).PEDOMETER | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER**. | 4093| callback | Callback<[PedometerResponse](#pedometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4094 4095**Error codes** 4096 4097For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4098 4099| ID| Error Message | 4100| -------- | ------------------------------------------------------------ | 4101| 201 | Permission denied. | 4102| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4103 4104**Example** 4105 4106```ts 4107import { sensor } from '@kit.SensorServiceKit'; 4108import { BusinessError } from '@kit.BasicServicesKit'; 4109 4110function callback1(data: object) { 4111 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4112} 4113 4114function callback2(data: object) { 4115 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4116} 4117 4118// Use try catch to capture possible exceptions. 4119try { 4120 sensor.on(sensor.SensorId.PEDOMETER, callback1); 4121 sensor.on(sensor.SensorId.PEDOMETER, callback2); 4122 // Unsubscribe from callback1. 4123 sensor.off(sensor.SensorId.PEDOMETER, callback1); 4124 // Unsubscribe from all callbacks of the SensorId.PEDOMETER type. 4125 sensor.off(sensor.SensorId.PEDOMETER); 4126} catch (error) { 4127 let e: BusinessError = error as BusinessError; 4128 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4129} 4130``` 4131 4132### PEDOMETER<sup>19+</sup> 4133 4134off(type: SensorId.PEDOMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<PedometerResponse>): void 4135 4136Unsubscribes from data of the pedometer sensor. 4137 4138**Required permissions**: ohos.permission.ACTIVITY_MOTION 4139 4140**System capability**: SystemCapability.Sensors.Sensor 4141 4142**Parameters** 4143 4144| Name | Type | Mandatory| Description | 4145|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4146| type | [SensorId](#sensorid9).PEDOMETER | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER**. | 4147| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4148| callback | Callback<[PedometerResponse](#pedometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4149 4150**Error codes** 4151 4152For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4153 4154| ID| Error Message | 4155| -------- | ------------------------------------------------------------ | 4156| 201 | Permission denied. | 4157| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4158 4159**Example** 4160 4161```ts 4162import { sensor } from '@kit.SensorServiceKit'; 4163import { BusinessError } from '@kit.BasicServicesKit'; 4164 4165enum Ret { OK, Failed = -1 } 4166 4167// Sensor callback 4168const sensorCallback = (response: sensor.PedometerResponse) => { 4169 console.log(`callback response: ${JSON.stringify(response)}`); 4170} 4171// Sensor type 4172const sensorType = sensor.SensorId.PEDOMETER; 4173const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4174 4175function sensorSubscribe(): Ret { 4176 let ret: Ret = Ret.OK; 4177 // Use try catch to capture possible exceptions. 4178 try { 4179 // Query all sensors. 4180 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4181 if (!sensorList.length) { 4182 return Ret.Failed; 4183 } 4184 // Obtain the target sensor based on the actual service logic. 4185 const targetSensor = sensorList 4186 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4187 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4188 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4189 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4190 if (!targetSensor) { 4191 return Ret.Failed; 4192 } 4193 sensorInfoParam.deviceId = targetSensor.deviceId; 4194 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4195 // Subscribe to sensor events. 4196 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4197 } catch (error) { 4198 let e: BusinessError = error as BusinessError; 4199 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4200 ret = Ret.Failed; 4201 } 4202 return ret; 4203} 4204 4205function sensorUnsubscribe(): Ret { 4206 let ret: Ret = Ret.OK; 4207 // Use try catch to capture possible exceptions. 4208 try { 4209 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4210 } catch (error) { 4211 let e: BusinessError = error as BusinessError; 4212 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4213 ret = Ret.Failed; 4214 } 4215 return ret; 4216} 4217``` 4218 4219### PEDOMETER_DETECTION<sup>9+</sup> 4220 4221off(type: SensorId.PEDOMETER_DETECTION, callback?: Callback<PedometerDetectionResponse>): void 4222 4223Unsubscribes from data of the pedometer detection sensor. 4224 4225**Required permissions**: ohos.permission.ACTIVITY_MOTION 4226 4227**System capability**: SystemCapability.Sensors.Sensor 4228 4229**Parameters** 4230 4231| Name | Type | Mandatory| Description | 4232| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4233| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER_DETECTION**. | 4234| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4235 4236**Error codes** 4237 4238For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4239 4240| ID| Error Message | 4241| -------- | ------------------------------------------------------------ | 4242| 201 | Permission denied. | 4243| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4244 4245**Example** 4246 4247```ts 4248import { sensor } from '@kit.SensorServiceKit'; 4249import { BusinessError } from '@kit.BasicServicesKit'; 4250 4251function callback1(data: object) { 4252 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4253} 4254 4255function callback2(data: object) { 4256 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4257} 4258 4259// Use try catch to capture possible exceptions. 4260try { 4261 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, callback1); 4262 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, callback2); 4263 // Unsubscribe from callback1. 4264 sensor.off(sensor.SensorId.PEDOMETER_DETECTION, callback1); 4265 // Unsubscribe from all callbacks of the SensorId.PEDOMETER_DETECTION type. 4266 sensor.off(sensor.SensorId.PEDOMETER_DETECTION); 4267} catch (error) { 4268 let e: BusinessError = error as BusinessError; 4269 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4270} 4271``` 4272 4273### PEDOMETER_DETECTION<sup>19+</sup> 4274 4275off(type: SensorId.PEDOMETER_DETECTION, sensorInfoParam?: SensorInfoParam, callback?: Callback<PedometerDetectionResponse>): void 4276 4277Unsubscribes from data of the pedometer detection sensor. 4278 4279**Required permissions**: ohos.permission.ACTIVITY_MOTION 4280 4281**System capability**: SystemCapability.Sensors.Sensor 4282 4283**Parameters** 4284 4285| Name | Type | Mandatory| Description | 4286|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4287| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.PEDOMETER_DETECTION**. | 4288| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4289| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4290 4291**Error codes** 4292 4293For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4294 4295| ID| Error Message | 4296| -------- | ------------------------------------------------------------ | 4297| 201 | Permission denied. | 4298| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4299 4300**Example** 4301 4302```ts 4303import { sensor } from '@kit.SensorServiceKit'; 4304import { BusinessError } from '@kit.BasicServicesKit'; 4305 4306enum Ret { OK, Failed = -1 } 4307 4308// Sensor callback 4309const sensorCallback = (response: sensor.PedometerDetectionResponse) => { 4310 console.log(`callback response: ${JSON.stringify(response)}`); 4311} 4312// Sensor type 4313const sensorType = sensor.SensorId.PEDOMETER_DETECTION; 4314const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4315 4316function sensorSubscribe(): Ret { 4317 let ret: Ret = Ret.OK; 4318 // Use try catch to capture possible exceptions. 4319 try { 4320 // Query all sensors. 4321 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4322 if (!sensorList.length) { 4323 return Ret.Failed; 4324 } 4325 // Obtain the target sensor based on the actual service logic. 4326 const targetSensor = sensorList 4327 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4328 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4329 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4330 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4331 if (!targetSensor) { 4332 return Ret.Failed; 4333 } 4334 sensorInfoParam.deviceId = targetSensor.deviceId; 4335 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4336 // Subscribe to sensor events. 4337 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4338 } catch (error) { 4339 let e: BusinessError = error as BusinessError; 4340 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4341 ret = Ret.Failed; 4342 } 4343 return ret; 4344} 4345 4346function sensorUnsubscribe(): Ret { 4347 let ret: Ret = Ret.OK; 4348 // Use try catch to capture possible exceptions. 4349 try { 4350 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4351 } catch (error) { 4352 let e: BusinessError = error as BusinessError; 4353 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4354 ret = Ret.Failed; 4355 } 4356 return ret; 4357} 4358``` 4359 4360### PROXIMITY<sup>9+</sup> 4361 4362off(type: SensorId.PROXIMITY, callback?: Callback<ProximityResponse>): void 4363 4364Unsubscribes from data of the proximity sensor. 4365 4366**System capability**: SystemCapability.Sensors.Sensor 4367 4368**Parameters** 4369 4370| Name | Type | Mandatory| Description | 4371| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4372| type | [SensorId](#sensorid9).PROXIMITY | Yes | Sensor type. The value is fixed at **SensorId.PROXIMITY**. | 4373| callback | Callback<[ProximityResponse](#proximityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4374 4375**Error codes** 4376 4377For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4378 4379| ID| Error Message | 4380| -------- | ------------------------------------------------------------ | 4381| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4382 4383**Example** 4384 4385```ts 4386import { sensor } from '@kit.SensorServiceKit'; 4387import { BusinessError } from '@kit.BasicServicesKit'; 4388 4389function callback1(data: object) { 4390 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4391} 4392 4393function callback2(data: object) { 4394 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4395} 4396 4397// Use try catch to capture possible exceptions. 4398try { 4399 sensor.on(sensor.SensorId.PROXIMITY, callback1); 4400 sensor.on(sensor.SensorId.PROXIMITY, callback2); 4401 // Unsubscribe from callback1. 4402 sensor.off(sensor.SensorId.PROXIMITY, callback1); 4403 // Unsubscribe from all callbacks of the SensorId.PROXIMITY type. 4404 sensor.off(sensor.SensorId.PROXIMITY); 4405} catch (error) { 4406 let e: BusinessError = error as BusinessError; 4407 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4408} 4409``` 4410 4411### PROXIMITY<sup>19+</sup> 4412 4413off(type: SensorId.PROXIMITY, sensorInfoParam?: SensorInfoParam, callback?: Callback<ProximityResponse>): void 4414 4415Unsubscribes from data of the proximity sensor. 4416 4417**System capability**: SystemCapability.Sensors.Sensor 4418 4419**Parameters** 4420 4421| Name | Type | Mandatory| Description | 4422|-----------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4423| type | [SensorId](#sensorid9).PROXIMITY | Yes | Sensor type. The value is fixed at **SensorId.PROXIMITY**. | 4424| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4425| callback | Callback<[ProximityResponse](#proximityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4426 4427**Error codes** 4428 4429For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4430 4431| ID| Error Message | 4432| -------- | ------------------------------------------------------------ | 4433| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4434 4435**Example** 4436 4437```ts 4438import { sensor } from '@kit.SensorServiceKit'; 4439import { BusinessError } from '@kit.BasicServicesKit'; 4440 4441enum Ret { OK, Failed = -1 } 4442 4443// Sensor callback 4444const sensorCallback = (response: sensor.ProximityResponse) => { 4445 console.log(`callback response: ${JSON.stringify(response)}`); 4446} 4447// Sensor type 4448const sensorType = sensor.SensorId.PROXIMITY; 4449const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4450 4451function sensorSubscribe(): Ret { 4452 let ret: Ret = Ret.OK; 4453 // Use try catch to capture possible exceptions. 4454 try { 4455 // Query all sensors. 4456 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4457 if (!sensorList.length) { 4458 return Ret.Failed; 4459 } 4460 // Obtain the target sensor based on the actual service logic. 4461 const targetSensor = sensorList 4462 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4463 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4464 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4465 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4466 if (!targetSensor) { 4467 return Ret.Failed; 4468 } 4469 sensorInfoParam.deviceId = targetSensor.deviceId; 4470 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4471 // Subscribe to sensor events. 4472 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4473 } catch (error) { 4474 let e: BusinessError = error as BusinessError; 4475 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4476 ret = Ret.Failed; 4477 } 4478 return ret; 4479} 4480 4481function sensorUnsubscribe(): Ret { 4482 let ret: Ret = Ret.OK; 4483 // Use try catch to capture possible exceptions. 4484 try { 4485 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4486 } catch (error) { 4487 let e: BusinessError = error as BusinessError; 4488 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4489 ret = Ret.Failed; 4490 } 4491 return ret; 4492} 4493``` 4494 4495### ROTATION_VECTOR<sup>9+</sup> 4496 4497off(type: SensorId.ROTATION_VECTOR, callback?: Callback<RotationVectorResponse>): void 4498 4499Unsubscribes from data of the rotation vector sensor. 4500 4501**System capability**: SystemCapability.Sensors.Sensor 4502 4503**Parameters** 4504 4505| Name | Type | Mandatory| Description | 4506| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4507| type | [SensorId](#sensorid9).ROTATION_VECTOR | Yes | Sensor type. The value is fixed at **SensorId.ROTATION_VECTOR**. | 4508| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4509 4510**Error codes** 4511 4512For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4513 4514| ID| Error Message | 4515| -------- | ------------------------------------------------------------ | 4516| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4517 4518**Example** 4519 4520```ts 4521import { sensor } from '@kit.SensorServiceKit'; 4522import { BusinessError } from '@kit.BasicServicesKit'; 4523 4524function callback1(data: object) { 4525 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4526} 4527 4528function callback2(data: object) { 4529 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4530} 4531 4532// Use try catch to capture possible exceptions. 4533try { 4534 sensor.on(sensor.SensorId.ROTATION_VECTOR, callback1); 4535 sensor.on(sensor.SensorId.ROTATION_VECTOR, callback2); 4536 // Unsubscribe from callback1. 4537 sensor.off(sensor.SensorId.ROTATION_VECTOR, callback1); 4538 // Unsubscribe from all callbacks of the SensorId.ROTATION_VECTOR type. 4539 sensor.off(sensor.SensorId.ROTATION_VECTOR); 4540} catch (error) { 4541 let e: BusinessError = error as BusinessError; 4542 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4543} 4544``` 4545 4546### ROTATION_VECTOR<sup>19+</sup> 4547 4548off(type: SensorId.ROTATION_VECTOR, sensorInfoParam?: SensorInfoParam, callback?: Callback<RotationVectorResponse>): void 4549 4550Unsubscribes from data of the rotation vector sensor. 4551 4552**System capability**: SystemCapability.Sensors.Sensor 4553 4554**Parameters** 4555 4556| Name | Type | Mandatory| Description | 4557|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4558| type | [SensorId](#sensorid9).ROTATION_VECTOR | Yes | Sensor type. The value is fixed at **SensorId.ROTATION_VECTOR**. | 4559| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4560| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4561 4562**Error codes** 4563 4564For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4565 4566| ID| Error Message | 4567| -------- | ------------------------------------------------------------ | 4568| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4569 4570**Example** 4571 4572```ts 4573import { sensor } from '@kit.SensorServiceKit'; 4574import { BusinessError } from '@kit.BasicServicesKit'; 4575 4576enum Ret { OK, Failed = -1 } 4577 4578// Sensor callback 4579const sensorCallback = (response: sensor.RotationVectorResponse) => { 4580 console.log(`callback response: ${JSON.stringify(response)}`); 4581} 4582// Sensor type 4583const sensorType = sensor.SensorId.ROTATION_VECTOR; 4584const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4585 4586function sensorSubscribe(): Ret { 4587 let ret: Ret = Ret.OK; 4588 // Use try catch to capture possible exceptions. 4589 try { 4590 // Query all sensors. 4591 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4592 if (!sensorList.length) { 4593 return Ret.Failed; 4594 } 4595 // Obtain the target sensor based on the actual service logic. 4596 const targetSensor = sensorList 4597 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4598 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4599 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4600 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4601 if (!targetSensor) { 4602 return Ret.Failed; 4603 } 4604 sensorInfoParam.deviceId = targetSensor.deviceId; 4605 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4606 // Subscribe to sensor events. 4607 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4608 } catch (error) { 4609 let e: BusinessError = error as BusinessError; 4610 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4611 ret = Ret.Failed; 4612 } 4613 return ret; 4614} 4615 4616function sensorUnsubscribe(): Ret { 4617 let ret: Ret = Ret.OK; 4618 // Use try catch to capture possible exceptions. 4619 try { 4620 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4621 } catch (error) { 4622 let e: BusinessError = error as BusinessError; 4623 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4624 ret = Ret.Failed; 4625 } 4626 return ret; 4627} 4628``` 4629 4630### SIGNIFICANT_MOTION<sup>9+</sup> 4631 4632off(type: SensorId.SIGNIFICANT_MOTION, callback?: Callback<SignificantMotionResponse>): void 4633 4634Unsubscribes from valid motion sensor data. 4635 4636**System capability**: SystemCapability.Sensors.Sensor 4637 4638**Parameters** 4639 4640| Name | Type | Mandatory| Description | 4641| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4642| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | Yes | Sensor type. The value is fixed at **SensorId.SIGNIFICANT_MOTION**. | 4643| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4644 4645**Error codes** 4646 4647For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4648 4649| ID| Error Message | 4650| -------- | ------------------------------------------------------------ | 4651| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4652 4653**Example** 4654 4655```ts 4656import { sensor } from '@kit.SensorServiceKit'; 4657import { BusinessError } from '@kit.BasicServicesKit'; 4658 4659function callback1(data: object) { 4660 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4661} 4662 4663function callback2(data: object) { 4664 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4665} 4666 4667// Use try catch to capture possible exceptions. 4668try { 4669 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, callback1); 4670 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, callback2); 4671 // Unsubscribe from callback1. 4672 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION, callback1); 4673 // Unsubscribe from all callbacks of the SensorId.SIGNIFICANT_MOTION type. 4674 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION); 4675} catch (error) { 4676 let e: BusinessError = error as BusinessError; 4677 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4678} 4679``` 4680 4681### SIGNIFICANT_MOTION<sup>19+</sup> 4682 4683off(type: SensorId.SIGNIFICANT_MOTION, sensorInfoParam?: SensorInfoParam, callback?: Callback<SignificantMotionResponse>): void 4684 4685Unsubscribes from valid motion sensor data. 4686 4687**System capability**: SystemCapability.Sensors.Sensor 4688 4689**Parameters** 4690 4691| Name | Type | Mandatory| Description | 4692|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4693| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | Yes | Sensor type. The value is fixed at **SensorId.SIGNIFICANT_MOTION**. | 4694| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4695| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4696 4697**Error codes** 4698 4699For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4700 4701| ID| Error Message | 4702| -------- | ------------------------------------------------------------ | 4703| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4704 4705**Example** 4706 4707```ts 4708import { sensor } from '@kit.SensorServiceKit'; 4709import { BusinessError } from '@kit.BasicServicesKit'; 4710 4711enum Ret { OK, Failed = -1 } 4712 4713// Sensor callback 4714const sensorCallback = (response: sensor.SignificantMotionResponse) => { 4715 console.log(`callback response: ${JSON.stringify(response)}`); 4716} 4717// Sensor type 4718const sensorType = sensor.SensorId.SIGNIFICANT_MOTION; 4719const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4720 4721function sensorSubscribe(): Ret { 4722 let ret: Ret = Ret.OK; 4723 // Use try catch to capture possible exceptions. 4724 try { 4725 // Query all sensors. 4726 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4727 if (!sensorList.length) { 4728 return Ret.Failed; 4729 } 4730 // Obtain the target sensor based on the actual service logic. 4731 const targetSensor = sensorList 4732 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4733 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4734 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4735 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4736 if (!targetSensor) { 4737 return Ret.Failed; 4738 } 4739 sensorInfoParam.deviceId = targetSensor.deviceId; 4740 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4741 // Subscribe to sensor events. 4742 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4743 } catch (error) { 4744 let e: BusinessError = error as BusinessError; 4745 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4746 ret = Ret.Failed; 4747 } 4748 return ret; 4749} 4750 4751function sensorUnsubscribe(): Ret { 4752 let ret: Ret = Ret.OK; 4753 // Use try catch to capture possible exceptions. 4754 try { 4755 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4756 } catch (error) { 4757 let e: BusinessError = error as BusinessError; 4758 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4759 ret = Ret.Failed; 4760 } 4761 return ret; 4762} 4763``` 4764 4765### WEAR_DETECTION<sup>9+</sup> 4766 4767off(type: SensorId.WEAR_DETECTION, callback?: Callback<WearDetectionResponse>): void 4768 4769Unsubscribes from data of the wear detection sensor. 4770 4771**System capability**: SystemCapability.Sensors.Sensor 4772 4773**Parameters** 4774 4775| Name | Type | Mandatory| Description | 4776| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4777| type | [SensorId](#sensorid9).WEAR_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.WEAR_DETECTION**. | 4778| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4779 4780**Error codes** 4781 4782For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4783 4784| ID| Error Message | 4785| -------- | ------------------------------------------------------------ | 4786| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4787 4788**Example** 4789 4790```ts 4791import { sensor } from '@kit.SensorServiceKit'; 4792import { BusinessError } from '@kit.BasicServicesKit'; 4793 4794function callback1(data: object) { 4795 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4796} 4797 4798function callback2(data: object) { 4799 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4800} 4801 4802// Use try catch to capture possible exceptions. 4803try { 4804 sensor.on(sensor.SensorId.WEAR_DETECTION, callback1); 4805 sensor.on(sensor.SensorId.WEAR_DETECTION, callback2); 4806 // Unsubscribe from callback1. 4807 sensor.off(sensor.SensorId.WEAR_DETECTION, callback1); 4808 // Unsubscribe from all callbacks of the SensorId.WEAR_DETECTION type. 4809 sensor.off(sensor.SensorId.WEAR_DETECTION); 4810} catch (error) { 4811 let e: BusinessError = error as BusinessError; 4812 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4813} 4814``` 4815 4816### WEAR_DETECTION<sup>19+</sup> 4817 4818off(type: SensorId.WEAR_DETECTION, sensorInfoParam?: SensorInfoParam, callback?: Callback<WearDetectionResponse>): void 4819 4820Unsubscribes from data of the wear detection sensor. 4821 4822**System capability**: SystemCapability.Sensors.Sensor 4823 4824**Parameters** 4825 4826| Name | Type | Mandatory| Description | 4827|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4828| type | [SensorId](#sensorid9).WEAR_DETECTION | Yes | Sensor type. The value is fixed at **SensorId.WEAR_DETECTION**. | 4829| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | No| Sensor parameters, including **deviceId** and **sensorIndex**.| 4830| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 4831 4832**Error codes** 4833 4834For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4835 4836| ID| Error Message | 4837| -------- | ------------------------------------------------------------ | 4838| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4839 4840**Example** 4841 4842```ts 4843import { sensor } from '@kit.SensorServiceKit'; 4844import { BusinessError } from '@kit.BasicServicesKit'; 4845 4846enum Ret { OK, Failed = -1 } 4847 4848// Sensor callback 4849const sensorCallback = (response: sensor.WearDetectionResponse) => { 4850 console.log(`callback response: ${JSON.stringify(response)}`); 4851} 4852// Sensor type 4853const sensorType = sensor.SensorId.WEAR_DETECTION; 4854const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4855 4856function sensorSubscribe(): Ret { 4857 let ret: Ret = Ret.OK; 4858 // Use try catch to capture possible exceptions. 4859 try { 4860 // Query all sensors. 4861 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4862 if (!sensorList.length) { 4863 return Ret.Failed; 4864 } 4865 // Obtain the target sensor based on the actual service logic. 4866 const targetSensor = sensorList 4867 // Filter all sensors with deviceId 1 and sensorId 2 as required. This example is for reference only. You need to adjust the filtering logic accordingly. 4868 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4869 // Select the sensor with sensorIndex 0 among all sensors of the same type. 4870 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4871 if (!targetSensor) { 4872 return Ret.Failed; 4873 } 4874 sensorInfoParam.deviceId = targetSensor.deviceId; 4875 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4876 // Subscribe to sensor events. 4877 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4878 } catch (error) { 4879 let e: BusinessError = error as BusinessError; 4880 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4881 ret = Ret.Failed; 4882 } 4883 return ret; 4884} 4885 4886function sensorUnsubscribe(): Ret { 4887 let ret: Ret = Ret.OK; 4888 // Use try catch to capture possible exceptions. 4889 try { 4890 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4891 } catch (error) { 4892 let e: BusinessError = error as BusinessError; 4893 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4894 ret = Ret.Failed; 4895 } 4896 return ret; 4897} 4898``` 4899 4900### sensorStatusChange<sup>19+<sup> 4901 4902off(type: 'sensorStatusChange', callback?: Callback<SensorStatusEvent>): void 4903 4904Disables listening for sensor status changes. 4905 4906**System capability**: SystemCapability.Sensors.Sensor 4907 4908**Parameters** 4909 4910| Name | Type | Mandatory| Description | 4911| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 4912| type | 'sensorStatusChange' | Yes | Event type. The value **sensorStatusChange** indicates the sensor status change event. | 4913| callback | Callback<[SensorStatusEvent](#sensorstatusevent19)> | No | Callback passed to **sensor.on**. If this parameter is left unspecified, listening will be disabled for all callbacks.| 4914 4915**Error codes** 4916 4917For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 4918 4919| ID| Error Message | 4920| -------- | ------------------------------------------------------------ | 4921| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4922 4923**Example** 4924 4925```ts 4926import { sensor } from '@kit.SensorServiceKit'; 4927import { BusinessError } from '@kit.BasicServicesKit'; 4928 4929// Use try catch to capture possible exceptions. 4930try { 4931 const statusChangeCallback = (data: sensor.SensorStatusEvent) => { 4932 console.info('sensorStatusChange : ' + JSON.stringify(data)); 4933 } 4934 const statusChangeCallback2 = (data: sensor.SensorStatusEvent) => { 4935 console.info('sensorStatusChange2 : ' + JSON.stringify(data)); 4936 } 4937 // Register two callback listeners for device online events. 4938 sensor.on('sensorStatusChange', statusChangeCallback); 4939 sensor.on('sensorStatusChange', statusChangeCallback2); 4940 4941 // Unregister the first listener after 3 seconds. 4942 setTimeout(() => { 4943 sensor.off('sensorStatusChange', statusChangeCallback); 4944 }, 3000); 4945 // Unregister the other listener after 5 seconds. 4946 setTimeout(() => { 4947 sensor.off('sensorStatusChange'); 4948 }, 5000); 4949} catch (error) { 4950 let e: BusinessError = error as BusinessError; 4951 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 4952} 4953``` 4954 4955 4956## sensor.getSensorListByDeviceSync<sup>19+</sup> 4957 4958getSensorListByDeviceSync(deviceId?: number): Array<Sensor> 4959 4960Obtains the information about all sensors on the device. 4961 4962**System capability**: SystemCapability.Sensors.Sensor 4963 4964**Parameters** 4965 4966| Name | Type | Mandatory| Description | 4967| --------------- | ------------------------------------------------------------ | ---- |--------| 4968| deviceId | number | No | Device ID. The default value is **-1**, indicating the local device. To query the ID of other devices, call [getSensorListByDeviceSync](#sensorgetsensorlistbydevicesync19).| 4969 4970 4971**Return value** 4972 4973| Type | Description | 4974| ---------------------------------------------------------- | -------------- | 4975| Array<[Sensor](#sensor9)> | Sensor attribute list. | 4976 4977 4978**Example** 4979 4980```ts 4981import { sensor } from '@kit.SensorServiceKit'; 4982import { BusinessError } from '@kit.BasicServicesKit'; 4983 4984try { 4985 const deviceId = 1; 4986 // The first deviceId is optional. By default, it is set to the ID of the local device. 4987 const sensorList: sensor.Sensor[] = sensor.getSensorListByDeviceSync(deviceId); 4988 console.log(`sensorList length: ${sensorList.length}`); 4989 console.log(`sensorList: ${JSON.stringify(sensorList)}`); 4990} catch (error) { 4991 let e: BusinessError = error as BusinessError; 4992 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 4993} 4994``` 4995 4996 4997## sensor.getSingleSensorByDeviceSync<sup>19+</sup> 4998 4999getSingleSensorByDeviceSync(type: SensorId, deviceId?: number): Array<Sensor> 5000 5001Obtains information about the sensor of a specific type. 5002 5003**System capability**: SystemCapability.Sensors.Sensor 5004 5005**Parameters** 5006 5007| Name | Type | Mandatory| Description | 5008| --------------- | ------------------------------------------------------------ | ---- |----------| 5009| type | [SensorId](#sensorid9) | Yes | Sensor type.| 5010| deviceId | number | No | Device ID. The default value is **-1**, indicating the local device. To query the ID of other devices, call [getSensorListByDeviceSync](#sensorgetsensorlistbydevicesync19).| 5011 5012 5013**Return value** 5014 5015| Type | Description | 5016| ---------------------------------------------------------- | -------------- | 5017| Array<[Sensor](#sensor9)> | Sensor attribute list. | 5018 5019**Example** 5020 5021```ts 5022import { sensor } from '@kit.SensorServiceKit'; 5023import { BusinessError } from '@kit.BasicServicesKit'; 5024 5025try { 5026 const deviceId = 1; 5027 // The second deviceId is optional. 5028 const sensorList: sensor.Sensor[] = sensor.getSingleSensorByDeviceSync(sensor.SensorId.ACCELEROMETER, deviceId); 5029 console.log(`sensorList length: ${sensorList.length}`); 5030 console.log(`sensorList Json: ${JSON.stringify(sensorList)}`); 5031} catch (error) { 5032 let e: BusinessError = error as BusinessError; 5033 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 5034} 5035``` 5036 5037 5038## sensor.getGeomagneticInfo<sup>9+</sup> 5039 5040getGeomagneticInfo(locationOptions: LocationOptions, timeMillis: number, callback: AsyncCallback<GeomagneticResponse>): void 5041 5042Obtains the geomagnetic field of a geographic location at a certain time. This API uses an asynchronous callback to return the result. 5043 5044**System capability**: SystemCapability.Sensors.Sensor 5045 5046**Parameters** 5047 5048| Name | Type | Mandatory| Description | 5049| --------------- | ------------------------------------------------------------ | ---- | ---------------------------------- | 5050| locationOptions | [LocationOptions](#locationoptions) | Yes | Geographic location, including the longitude, latitude, and altitude. | 5051| timeMillis | number | Yes | Time when the magnetic declination is obtained. The value is a Unix timestamp, in ms.| 5052| callback | AsyncCallback<[GeomagneticResponse](#geomagneticresponse)> | Yes | Callback used to return the geomagnetic field. | 5053 5054**Error codes** 5055 5056For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5057 5058| ID| Error Message | 5059| -------- | ------------------------------------------------------------ | 5060| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5061| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5062 5063**Example** 5064 5065```ts 5066import { sensor } from '@kit.SensorServiceKit'; 5067import { BusinessError } from '@kit.BasicServicesKit'; 5068 5069// Use try catch to capture possible exceptions. 5070try { 5071 sensor.getGeomagneticInfo({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000, 5072 (err: BusinessError, data: sensor.GeomagneticResponse) => { 5073 if (err) { 5074 console.error(`Failed to get geomagneticInfo. Code: ${err.code}, message: ${err.message}`); 5075 return; 5076 } 5077 console.info("Succeeded in getting geomagneticInfo x" + data.x); 5078 console.info("Succeeded in getting geomagneticInfo y" + data.y); 5079 console.info("Succeeded in getting geomagneticInfo z" + data.z); 5080 console.info("Succeeded in getting geomagneticInfo geomagneticDip" + data.geomagneticDip); 5081 console.info("Succeeded in getting geomagneticInfo deflectionAngle" + data.deflectionAngle); 5082 console.info("Succeeded in getting geomagneticInfo levelIntensity" + data.levelIntensity); 5083 console.info("Succeeded in getting geomagneticInfo totalIntensity" + data.totalIntensity); 5084 }); 5085} catch (error) { 5086 let e: BusinessError = error as BusinessError; 5087 console.error(`Failed to get geomagneticInfo. Code: ${e.code}, message: ${e.message}`); 5088} 5089``` 5090 5091## sensor.getGeomagneticInfo<sup>9+</sup> 5092 5093getGeomagneticInfo(locationOptions: LocationOptions, timeMillis: number): Promise<GeomagneticResponse> 5094 5095Obtains the geomagnetic field of a geographic location at a certain time. This API uses a promise to return the result. 5096 5097**System capability**: SystemCapability.Sensors.Sensor 5098 5099**Parameters** 5100 5101| Name | Type | Mandatory| Description | 5102| --------------- | ----------------------------------- | ---- | ---------------------------------- | 5103| locationOptions | [LocationOptions](#locationoptions) | Yes | Geographic location, including the longitude, latitude, and altitude. | 5104| timeMillis | number | Yes | Time when the magnetic declination is obtained. The value is a Unix timestamp, in ms.| 5105 5106**Return value** 5107 5108| Type | Description | 5109| ---------------------------------------------------------- | -------------- | 5110| Promise<[GeomagneticResponse](#geomagneticresponse)> | Promise used to return the geomagnetic field.| 5111 5112**Error codes** 5113 5114For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5115 5116| ID| Error Message | 5117| -------- | ------------------------------------------------------------ | 5118| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5119| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5120 5121**Example** 5122 5123```ts 5124import { sensor } from '@kit.SensorServiceKit'; 5125import { BusinessError } from '@kit.BasicServicesKit'; 5126 5127// Use try catch to capture possible exceptions. 5128try { 5129 const promise = sensor.getGeomagneticInfo({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000); 5130 promise.then((data: sensor.GeomagneticResponse) => { 5131 console.info("Succeeded in getting geomagneticInfo x" + data.x); 5132 console.info("Succeeded in getting geomagneticInfo y" + data.y); 5133 console.info("Succeeded in getting geomagneticInfo z" + data.z); 5134 console.info("Succeeded in getting geomagneticInfo geomagneticDip" + data.geomagneticDip); 5135 console.info("Succeeded in getting geomagneticInfo deflectionAngle" + data.deflectionAngle); 5136 console.info("Succeeded in getting geomagneticInfo levelIntensity" + data.levelIntensity); 5137 console.info("Succeeded in getting geomagneticInfo totalIntensity" + data.totalIntensity); 5138 }, (err: BusinessError) => { 5139 console.error(`Failed to get geomagneticInfo. Code: ${err.code}, message: ${err.message}`); 5140 }); 5141} catch (error) { 5142 let e: BusinessError = error as BusinessError; 5143 console.error(`Failed to get geomagneticInfo. Code: ${e.code}, message: ${e.message}`); 5144} 5145``` 5146 5147## sensor.getDeviceAltitude<sup>9+</sup> 5148 5149getDeviceAltitude(seaPressure: number, currentPressure: number, callback: AsyncCallback<number>): void 5150 5151Obtains the altitude based on the atmospheric pressure. This API uses an asynchronous callback to return the result. 5152 5153**System capability**: SystemCapability.Sensors.Sensor 5154 5155**Parameters** 5156 5157| Name | Type | Mandatory| Description | 5158| --------------- | --------------------------- | ---- | ------------------------------------- | 5159| seaPressure | number | Yes | Sea-level atmospheric pressure, in hPa. | 5160| currentPressure | number | Yes | Specified atmospheric pressure, in hPa.| 5161| callback | AsyncCallback<number> | Yes | Callback used to return the altitude, in meters. | 5162 5163**Error codes** 5164 5165For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5166 5167| ID| Error Message | 5168| -------- | ------------------------------------------------------------ | 5169| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5170| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5171 5172**Example** 5173 5174```ts 5175import { sensor } from '@kit.SensorServiceKit'; 5176import { BusinessError } from '@kit.BasicServicesKit'; 5177 5178// Use try catch to capture possible exceptions. 5179try { 5180 let seaPressure = 1013.2; 5181 let currentPressure = 1500.0; 5182 sensor.getDeviceAltitude(seaPressure, currentPressure, (err: BusinessError, data: number) => { 5183 if (err) { 5184 console.error(`Failed to get altitude. Code: ${err.code}, message: ${err.message}`); 5185 return; 5186 } 5187 console.info('Succeeded in getting altitude: ' + data); 5188 }); 5189} catch (error) { 5190 let e: BusinessError = error as BusinessError; 5191 console.error(`Failed to get altitude. Code: ${e.code}, message: ${e.message}`); 5192} 5193``` 5194 5195## sensor.getDeviceAltitude<sup>9+</sup> 5196 5197getDeviceAltitude(seaPressure: number, currentPressure: number): Promise<number> 5198 5199Obtains the altitude based on the atmospheric pressure. This API uses a promise to return the result. 5200 5201**System capability**: SystemCapability.Sensors.Sensor 5202 5203**Parameters** 5204 5205| Name | Type | Mandatory| Description | 5206| --------------- | ------ | ---- | ------------------------------------- | 5207| seaPressure | number | Yes | Sea-level atmospheric pressure, in hPa. | 5208| currentPressure | number | Yes | Specified atmospheric pressure, in hPa.| 5209 5210**Return value** 5211 5212| Type | Description | 5213| --------------------- | ------------------------------------ | 5214| Promise<number> | Promise used to return the altitude, in meters.| 5215 5216**Error codes** 5217 5218For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5219 5220| ID| Error Message | 5221| -------- | ------------------------------------------------------------ | 5222| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5223| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5224 5225**Example** 5226 5227```ts 5228import { sensor } from '@kit.SensorServiceKit'; 5229import { BusinessError } from '@kit.BasicServicesKit'; 5230 5231// Use try catch to capture possible exceptions. 5232try { 5233 let seaPressure = 1013.2; 5234 let currentPressure = 1500.0; 5235 const promise = sensor.getDeviceAltitude(seaPressure, currentPressure); 5236 promise.then((data: number) => { 5237 console.info('Succeeded in getting sensor_getDeviceAltitude_Promise', data); 5238 }, (err: BusinessError) => { 5239 console.error(`Failed to get altitude. Code: ${err.code}, message: ${err.message}`); 5240 }); 5241} catch (error) { 5242 let e: BusinessError = error as BusinessError; 5243 console.error(`Failed to get altitude. Code: ${e.code}, message: ${e.message}`); 5244} 5245``` 5246 5247## sensor.getInclination<sup>9+</sup> 5248 5249getInclination(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void 5250 5251Obtains the magnetic dip based on the inclination matrix. This API uses an asynchronous callback to return the result. 5252 5253**System capability**: SystemCapability.Sensors.Sensor 5254 5255**Parameters** 5256 5257| Name | Type | Mandatory| Description | 5258| ----------------- | --------------------------- | ---- | ---------------------------- | 5259| inclinationMatrix | Array<number> | Yes | Inclination matrix. | 5260| callback | AsyncCallback<number> | Yes | Callback used to return the magnetic dip, in radians.| 5261 5262**Error codes** 5263 5264For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5265 5266| ID| Error Message | 5267| -------- | ------------------------------------------------------------ | 5268| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5269| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5270 5271**Example** 5272 5273```ts 5274import { sensor } from '@kit.SensorServiceKit'; 5275import { BusinessError } from '@kit.BasicServicesKit'; 5276 5277// Use try catch to capture possible exceptions. 5278try { 5279 // inclinationMatrix can be 3*3 or 4*4. 5280 let inclinationMatrix = [ 5281 1, 0, 0, 5282 0, 1, 0, 5283 0, 0, 1 5284 ] 5285 sensor.getInclination(inclinationMatrix, (err: BusinessError, data: number) => { 5286 if (err) { 5287 console.error(`Failed to get inclination. Code: ${err.code}, message: ${err.message}`); 5288 return; 5289 } 5290 console.info('Succeeded in getting inclination: ' + data); 5291 }) 5292} catch (error) { 5293 let e: BusinessError = error as BusinessError; 5294 console.error(`Failed to get inclination. Code: ${e.code}, message: ${e.message}`); 5295} 5296``` 5297 5298## sensor.getInclination<sup>9+</sup> 5299 5300 getInclination(inclinationMatrix: Array<number>): Promise<number> 5301 5302Obtains the magnetic dip based on the inclination matrix. This API uses a promise to return the result. 5303 5304**System capability**: SystemCapability.Sensors.Sensor 5305 5306**Parameters** 5307 5308| Name | Type | Mandatory| Description | 5309| ----------------- | ------------------- | ---- | -------------- | 5310| inclinationMatrix | Array<number> | Yes | Inclination matrix.| 5311 5312**Return value** 5313 5314| Type | Description | 5315| --------------------- | ---------------------------- | 5316| Promise<number> | Promise used to return the magnetic dip, in radians.| 5317 5318**Error codes** 5319 5320For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5321 5322| ID| Error Message | 5323| -------- | ------------------------------------------------------------ | 5324| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5325| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5326 5327**Example** 5328 5329```ts 5330import { sensor } from '@kit.SensorServiceKit'; 5331import { BusinessError } from '@kit.BasicServicesKit'; 5332 5333// Use try catch to capture possible exceptions. 5334try { 5335 // inclinationMatrix can be 3*3 or 4*4. 5336 let inclinationMatrix = [ 5337 1, 0, 0, 5338 0, 1, 0, 5339 0, 0, 1 5340 ] 5341 const promise = sensor.getInclination(inclinationMatrix); 5342 promise.then((data: number) => { 5343 console.info('Succeeded in getting inclination: ' + data); 5344 }, (err: BusinessError) => { 5345 console.error(`Failed to get inclination. Code: ${err.code}, message: ${err.message}`); 5346 }); 5347} catch (error) { 5348 let e: BusinessError = error as BusinessError; 5349 console.error(`Failed to get inclination. Code: ${e.code}, message: ${e.message}`); 5350} 5351``` 5352 5353## sensor.getAngleVariation<sup>9+</sup> 5354 5355 getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>, 5356 callback: AsyncCallback<Array<number>>): void 5357 5358Obtains the angle change between two rotation matrices. This API uses an asynchronous callback to return the result. 5359 5360**System capability**: SystemCapability.Sensors.Sensor 5361 5362**Parameters** 5363 5364| Name | Type | Mandatory| Description | 5365| --------------------- | ---------------------------------------- | ---- | --------------------------------- | 5366| currentRotationMatrix | Array<number> | Yes | Current rotation matrix. | 5367| preRotationMatrix | Array<number> | Yes | The other rotation matrix. | 5368| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the angle change around the z, x, and y axes.| 5369 5370**Error codes** 5371 5372For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5373 5374| ID| Error Message | 5375| -------- | ------------------------------------------------------------ | 5376| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5377| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5378 5379**Example** 5380 5381```ts 5382import { sensor } from '@kit.SensorServiceKit'; 5383import { BusinessError } from '@kit.BasicServicesKit'; 5384 5385// Use try catch to capture possible exceptions. 5386try { 5387 // The rotation matrix can be 3*3 or 4*4. 5388 let currentRotationMatrix = [ 5389 1, 0, 0, 5390 0, 1, 0, 5391 0, 0, 1 5392 ]; 5393 let preRotationMatrix = [ 5394 1, 0, 0, 5395 0, 0.87, -0.50, 5396 0, 0.50, 0.87 5397 ]; 5398 sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix, (err: BusinessError, data: Array<number>) => { 5399 if (err) { 5400 console.error(`Failed to get angle variation. Code: ${err.code}, message: ${err.message}`); 5401 return; 5402 } 5403 if (data.length < 3) { 5404 console.error("Failed to get angle variation, length" + data.length); 5405 return; 5406 } 5407 console.info("Z: " + data[0]); 5408 console.info("X: " + data[1]); 5409 console.info("Y: " + data[2]); 5410 }) 5411} catch (error) { 5412 let e: BusinessError = error as BusinessError; 5413 console.error(`Failed to get angle variation. Code: ${e.code}, message: ${e.message}`); 5414} 5415``` 5416 5417## sensor.getAngleVariation<sup>9+</sup> 5418 5419getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>> 5420 5421Obtains the angle change between two rotation matrices. This API uses a promise to return the result. 5422 5423**System capability**: SystemCapability.Sensors.Sensor 5424 5425**Parameters** 5426 5427| Name | Type | Mandatory| Description | 5428| --------------------- | ------------------- | ---- | ------------------ | 5429| currentRotationMatrix | Array<number> | Yes | Current rotation matrix.| 5430| preRotationMatrix | Array<number> | Yes | The other rotation matrix. | 5431 5432**Return value** 5433 5434| Type | Description | 5435| ---------------------------------- | --------------------------------- | 5436| Promise<Array<number>> | Promise used to return the angle change around the z, x, and y axes.| 5437 5438**Error codes** 5439 5440For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5441 5442| ID| Error Message | 5443| -------- | ------------------------------------------------------------ | 5444| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5445| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5446 5447**Example** 5448 5449```ts 5450import { sensor } from '@kit.SensorServiceKit'; 5451import { BusinessError } from '@kit.BasicServicesKit'; 5452 5453// Use try catch to capture possible exceptions. 5454try { 5455 // The rotation matrix can be 3*3 or 4*4. 5456 let currentRotationMatrix = [ 5457 1, 0, 0, 5458 0, 1, 0, 5459 0, 0, 1 5460 ]; 5461 let preRotationMatrix = [ 5462 1, 0, 0, 5463 0, 0.87, -0.50, 5464 0, 0.50, 0.87 5465 ]; 5466 const promise = sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix); 5467 promise.then((data: Array<number>) => { 5468 if (data.length < 3) { 5469 console.error("Failed to get angle variation, length" + data.length); 5470 return; 5471 } 5472 console.info("Z: " + data[0]); 5473 console.info("X: " + data[1]); 5474 console.info("Y: " + data[2]); 5475 }, (err: BusinessError) => { 5476 console.error(`Failed to get angle variation. Code: ${err.code}, message: ${err.message}`); 5477 }); 5478} catch (error) { 5479 let e: BusinessError = error as BusinessError; 5480 console.error(`Failed to get angle variation. Code: ${e.code}, message: ${e.message}`); 5481} 5482``` 5483 5484## sensor.getRotationMatrix<sup>9+</sup> 5485 5486getRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 5487 5488Obtains the rotation matrix from a rotation vector. This API uses an asynchronous callback to return the result. 5489 5490**System capability**: SystemCapability.Sensors.Sensor 5491 5492**Parameters** 5493 5494| Name | Type | Mandatory| Description | 5495| -------------- | ---------------------------------------- | ---- | -------------- | 5496| rotationVector | Array<number> | Yes | Rotation vector.| 5497| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the rotation matrix.| 5498 5499**Error codes** 5500 5501For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5502 5503| ID| Error Message | 5504| -------- | ------------------------------------------------------------ | 5505| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5506| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5507 5508**Example** 5509 5510```ts 5511import { sensor } from '@kit.SensorServiceKit'; 5512import { BusinessError } from '@kit.BasicServicesKit'; 5513 5514// Use try catch to capture possible exceptions. 5515try { 5516 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5517 sensor.getRotationMatrix(rotationVector, (err: BusinessError, data: Array<number>) => { 5518 if (err) { 5519 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5520 return; 5521 } 5522 for (let i = 0; i < data.length; i++) { 5523 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5524 } 5525 }) 5526} catch (error) { 5527 let e: BusinessError = error as BusinessError; 5528 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5529} 5530``` 5531 5532## sensor.getRotationMatrix<sup>9+</sup> 5533 5534getRotationMatrix(rotationVector: Array<number>): Promise<Array<number>> 5535 5536Obtains the rotation matrix from a rotation vector. This API uses a promise to return the result. 5537 5538**System capability**: SystemCapability.Sensors.Sensor 5539 5540**Parameters** 5541 5542| Name | Type | Mandatory| Description | 5543| -------------- | ------------------- | ---- | -------------- | 5544| rotationVector | Array<number> | Yes | Rotation vector.| 5545 5546**Return value** 5547 5548| Type | Description | 5549| ---------------------------------- | -------------- | 5550| Promise<Array<number>> | Promise used to return the rotation matrix.| 5551 5552**Error codes** 5553 5554For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5555 5556| ID| Error Message | 5557| -------- | ------------------------------------------------------------ | 5558| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5559| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5560 5561**Example** 5562 5563```ts 5564import { sensor } from '@kit.SensorServiceKit'; 5565import { BusinessError } from '@kit.BasicServicesKit'; 5566 5567// Use try catch to capture possible exceptions. 5568try { 5569 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5570 const promise = sensor.getRotationMatrix(rotationVector); 5571 promise.then((data: Array<number>) => { 5572 for (let i = 0; i < data.length; i++) { 5573 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5574 } 5575 }, (err: BusinessError) => { 5576 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5577 }); 5578} catch (error) { 5579 let e: BusinessError = error as BusinessError; 5580 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5581} 5582``` 5583 5584## sensor.transformRotationMatrix<sup>9+</sup> 5585 5586transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions, 5587 callback: AsyncCallback<Array<number>>): void 5588 5589Transforms a rotation vector based on the coordinate system. This API uses an asynchronous callback to return the result. 5590 5591**System capability**: SystemCapability.Sensors.Sensor 5592 5593**Parameters** 5594 5595| Name | Type | Mandatory| Description | 5596| ---------------- | ----------------------------------------- | ---- | ---------------------- | 5597| inRotationVector | Array<number> | Yes | Rotation vector. | 5598| coordinates | [CoordinatesOptions](#coordinatesoptions) | Yes | Rotation vector to transform. | 5599| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the rotation vector after being transformed.| 5600 5601**Error codes** 5602 5603For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5604 5605| ID| Error Message | 5606| -------- | ------------------------------------------------------------ | 5607| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5608| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5609 5610**Example** 5611 5612```ts 5613import { sensor } from '@kit.SensorServiceKit'; 5614import { BusinessError } from '@kit.BasicServicesKit'; 5615 5616// Use try catch to capture possible exceptions. 5617try { 5618 let rotationMatrix = [ 5619 1, 0, 0, 5620 0, 0.87, -0.50, 5621 0, 0.50, 0.87 5622 ]; 5623 sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }, (err: BusinessError, data: Array<number>) => { 5624 if (err) { 5625 console.error(`Failed to transform rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5626 return; 5627 } 5628 for (let i = 0; i < data.length; i++) { 5629 console.info('Succeeded in getting data[' + i + '] = ' + data[i]); 5630 } 5631 }) 5632} catch (error) { 5633 let e: BusinessError = error as BusinessError; 5634 console.error(`Failed to transform rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5635} 5636``` 5637 5638## sensor.transformRotationMatrix<sup>9+</sup> 5639 5640transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>> 5641 5642Transforms a rotation vector based on the coordinate system. This API uses a promise to return the result. 5643 5644**System capability**: SystemCapability.Sensors.Sensor 5645 5646**Parameters** 5647 5648| Name | Type | Mandatory| Description | 5649| ---------------- | ----------------------------------------- | ---- | ---------------- | 5650| inRotationVector | Array<number> | Yes | Rotation vector. | 5651| coordinates | [CoordinatesOptions](#coordinatesoptions) | Yes | Rotation vector to transform.| 5652 5653**Return value** 5654 5655| Type | Description | 5656| ---------------------------------- | ---------------------- | 5657| Promise<Array<number>> | Promise used to return the rotation vector after being transformed.| 5658 5659**Error codes** 5660 5661For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5662 5663| ID| Error Message | 5664| -------- | ------------------------------------------------------------ | 5665| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5666| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5667 5668**Example** 5669 5670```ts 5671import { sensor } from '@kit.SensorServiceKit'; 5672import { BusinessError } from '@kit.BasicServicesKit'; 5673 5674// Use try catch to capture possible exceptions. 5675try { 5676 let rotationMatrix = [ 5677 1, 0, 0, 5678 0, 0.87, -0.50, 5679 0, 0.50, 0.87 5680 ]; 5681 const promise = sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }); 5682 promise.then((data: Array<number>) => { 5683 for (let i = 0; i < data.length; i++) { 5684 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5685 } 5686 }, (err: BusinessError) => { 5687 console.error(`Failed to transform rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5688 }); 5689} catch (error) { 5690 let e: BusinessError = error as BusinessError; 5691 console.error(`Failed to transform rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5692} 5693``` 5694 5695## sensor.getQuaternion<sup>9+</sup> 5696 5697getQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 5698 5699Obtains the quaternion from a rotation vector. This API uses an asynchronous callback to return the result. 5700 5701**System capability**: SystemCapability.Sensors.Sensor 5702 5703**Parameters** 5704 5705| Name | Type | Mandatory| Description | 5706| -------------- | ---------------------------------------- | ---- | -------------- | 5707| rotationVector | Array<number> | Yes | Rotation vector.| 5708| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the quaternion.| 5709 5710**Error codes** 5711 5712For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5713 5714| ID| Error Message | 5715| -------- | ------------------------------------------------------------ | 5716| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5717| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5718 5719**Example** 5720 5721```ts 5722import { sensor } from '@kit.SensorServiceKit'; 5723import { BusinessError } from '@kit.BasicServicesKit'; 5724 5725// Use try catch to capture possible exceptions. 5726try { 5727 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5728 sensor.getQuaternion(rotationVector, (err: BusinessError, data: Array<number>) => { 5729 if (err) { 5730 console.error(`Failed to get quaternion. Code: ${err.code}, message: ${err.message}`); 5731 return; 5732 } 5733 for (let i = 0; i < data.length; i++) { 5734 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5735 } 5736 }) 5737} catch (error) { 5738 let e: BusinessError = error as BusinessError; 5739 console.error(`Failed to get quaternion. Code: ${e.code}, message: ${e.message}`); 5740} 5741``` 5742 5743## sensor.getQuaternion<sup>9+</sup> 5744 5745getQuaternion(rotationVector: Array<number>): Promise<Array<number>> 5746 5747Obtains the quaternion from a rotation vector. This API uses a promise to return the result. 5748 5749**System capability**: SystemCapability.Sensors.Sensor 5750 5751**Parameters** 5752 5753| Name | Type | Mandatory| Description | 5754| -------------- | ------------------- | ---- | -------------- | 5755| rotationVector | Array<number> | Yes | Rotation vector.| 5756 5757**Return value** 5758 5759| Type | Description | 5760| ---------------------------------- | ------------ | 5761| Promise<Array<number>> | Promise used to return the quaternion.| 5762 5763**Error codes** 5764 5765For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5766 5767| ID| Error Message | 5768| -------- | ------------------------------------------------------------ | 5769| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5770| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5771 5772**Example** 5773 5774```ts 5775import { sensor } from '@kit.SensorServiceKit'; 5776import { BusinessError } from '@kit.BasicServicesKit'; 5777 5778// Use try catch to capture possible exceptions. 5779try { 5780 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5781 const promise = sensor.getQuaternion(rotationVector); 5782 promise.then((data: Array<number>) => { 5783 for (let i = 0; i < data.length; i++) { 5784 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5785 } 5786 }, (err: BusinessError) => { 5787 console.error(`Failed to get quaternion. Code: ${err.code}, message: ${err.message}`); 5788 }); 5789} catch (error) { 5790 let e: BusinessError = error as BusinessError; 5791 console.error(`Failed to get quaternion. Code: ${e.code}, message: ${e.message}`); 5792} 5793``` 5794 5795## sensor.getOrientation<sup>9+</sup> 5796 5797getOrientation(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 5798 5799Obtains the device direction based on the rotation matrix. This API uses an asynchronous callback to return the result. 5800 5801**System capability**: SystemCapability.Sensors.Sensor 5802 5803**Parameters** 5804 5805| Name | Type | Mandatory| Description | 5806| -------------- | ---------------------------------------- | ---- | --------------------------------- | 5807| rotationMatrix | Array<number> | Yes | Rotation matrix. | 5808| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the rotation angle around the z, x, and y axes.| 5809 5810**Error codes** 5811 5812For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5813 5814| ID| Error Message | 5815| -------- | ------------------------------------------------------------ | 5816| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5817| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5818 5819**Example** 5820 5821```ts 5822import { sensor } from '@kit.SensorServiceKit'; 5823import { BusinessError } from '@kit.BasicServicesKit'; 5824 5825// Use try catch to capture possible exceptions. 5826try { 5827 let preRotationMatrix = [ 5828 1, 0, 0, 5829 0, 0.87, -0.50, 5830 0, 0.50, 0.87 5831 ]; 5832 sensor.getOrientation(preRotationMatrix, (err: BusinessError, data: Array<number>) => { 5833 if (err) { 5834 console.error(`Failed to get orientation. Code: ${err.code}, message: ${err.message}`); 5835 return; 5836 } 5837 if (data.length < 3) { 5838 console.error("Failed to get orientation, length" + data.length); 5839 } 5840 console.info("Succeeded in getting data. Z: " + data[0]); 5841 console.info("Succeeded in getting data. X: " + data[1]); 5842 console.info("Succeeded in getting data. Y: " + data[2]); 5843 }) 5844} catch (error) { 5845 let e: BusinessError = error as BusinessError; 5846 console.error(`Failed to get orientation. Code: ${e.code}, message: ${e.message}`); 5847} 5848``` 5849 5850## sensor.getOrientation<sup>9+</sup> 5851 5852getOrientation(rotationMatrix: Array<number>): Promise<Array<number>> 5853 5854Obtains the device direction based on the rotation matrix. This API uses a promise to return the result. 5855 5856**System capability**: SystemCapability.Sensors.Sensor 5857 5858**Parameters** 5859 5860| Name | Type | Mandatory| Description | 5861| -------------- | ------------------- | ---- | -------------- | 5862| rotationMatrix | Array<number> | Yes | Rotation vector.| 5863 5864**Return value** 5865 5866| Type | Description | 5867| ---------------------------------- | --------------------------------- | 5868| Promise<Array<number>> | Promise used to return the rotation angle around the z, x, and y axes.| 5869 5870**Error codes** 5871 5872For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5873 5874| ID| Error Message | 5875| -------- | ------------------------------------------------------------ | 5876| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5877| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5878 5879**Example** 5880 5881```ts 5882import { sensor } from '@kit.SensorServiceKit'; 5883import { BusinessError } from '@kit.BasicServicesKit'; 5884 5885// Use try catch to capture possible exceptions. 5886try { 5887 let preRotationMatrix = [ 5888 1, 0, 0, 5889 0, 0.87, -0.50, 5890 0, 0.50, 0.87 5891 ]; 5892 const promise = sensor.getOrientation(preRotationMatrix); 5893 promise.then((data: Array<number>) => { 5894 for (let i = 0; i < data.length; i++) { 5895 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5896 } 5897 }, (err: BusinessError) => { 5898 console.error(`Failed to getOrientation. Code: ${err.code}, message: ${err.message}`); 5899 }); 5900} catch (error) { 5901 let e: BusinessError = error as BusinessError; 5902 console.error(`Failed to getOrientation Code: ${e.code}, message: ${e.message}`); 5903} 5904``` 5905 5906## sensor.getRotationMatrix<sup>9+</sup> 5907 5908getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void 5909 5910Obtains the rotation matrix based on a gravity vector and geomagnetic vector. This API uses an asynchronous callback to return the result. 5911 5912**System capability**: SystemCapability.Sensors.Sensor 5913 5914**Parameters** 5915 5916| Name | Type | Mandatory| Description | 5917| ----------- | ------------------------------------------------------------ | ---- | -------------- | 5918| gravity | Array<number> | Yes | Gravity vector.| 5919| geomagnetic | Array<number> | Yes | Geomagnetic vector.| 5920| callback | AsyncCallback<[RotationMatrixResponse](#rotationmatrixresponse)> | Yes | Callback used to return the rotation matrix.| 5921 5922**Error codes** 5923 5924For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5925 5926| ID| Error Message | 5927| -------- | ------------------------------------------------------------ | 5928| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5929| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5930 5931**Example** 5932 5933```ts 5934import { sensor } from '@kit.SensorServiceKit'; 5935import { BusinessError } from '@kit.BasicServicesKit'; 5936 5937// Use try catch to capture possible exceptions. 5938try { 5939 let gravity = [-0.27775216, 0.5351276, 9.788099]; 5940 let geomagnetic = [210.87253, -78.6096, -111.44444]; 5941 sensor.getRotationMatrix(gravity, geomagnetic, (err: BusinessError, data: sensor.RotationMatrixResponse) => { 5942 if (err) { 5943 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5944 return; 5945 } 5946 console.info('Succeeded in getting rotationMatrix' + JSON.stringify(data)); 5947 }) 5948} catch (error) { 5949 let e: BusinessError = error as BusinessError; 5950 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5951} 5952``` 5953 5954## sensor.getRotationMatrix<sup>9+</sup> 5955 5956getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>): Promise<RotationMatrixResponse> 5957 5958Obtains the rotation matrix based on a gravity vector and geomagnetic vector. This API uses a promise to return the result. 5959 5960**System capability**: SystemCapability.Sensors.Sensor 5961 5962**Parameters** 5963 5964| Name | Type | Mandatory| Description | 5965| ----------- | ------------------- | ---- | -------------- | 5966| gravity | Array<number> | Yes | Gravity vector.| 5967| geomagnetic | Array<number> | Yes | Geomagnetic vector.| 5968 5969**Return value** 5970 5971| Type | Description | 5972| ------------------------------------------------------------ | -------------- | 5973| Promise<[RotationMatrixResponse](#rotationmatrixresponse)> | Promise used to return the rotation matrix.| 5974 5975**Error codes** 5976 5977For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 5978 5979| ID| Error Message | 5980| -------- | ------------------------------------------------------------ | 5981| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5982| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5983 5984**Example** 5985 5986```ts 5987import { sensor } from '@kit.SensorServiceKit'; 5988import { BusinessError } from '@kit.BasicServicesKit'; 5989 5990// Use try catch to capture possible exceptions. 5991try { 5992 let gravity = [-0.27775216, 0.5351276, 9.788099]; 5993 let geomagnetic = [210.87253, -78.6096, -111.44444]; 5994 const promise = sensor.getRotationMatrix(gravity, geomagnetic); 5995 promise.then((data: sensor.RotationMatrixResponse) => { 5996 console.info('Succeeded in getting rotationMatrix' + JSON.stringify(data)); 5997 }, (err: BusinessError) => { 5998 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5999 }); 6000} catch (error) { 6001 let e: BusinessError = error as BusinessError; 6002 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 6003} 6004``` 6005 6006## sensor.getSensorList<sup>9+</sup> 6007 6008getSensorList(callback: AsyncCallback<Array<Sensor>>): void 6009 6010Obtains information about all sensors on the device. This API uses an asynchronous callback to return the result. 6011 6012**System capability**: SystemCapability.Sensors.Sensor 6013 6014**Parameters** 6015 6016| Name | Type | Mandatory| Description | 6017| -------- | ---------------------------------------------- | ---- | ---------------- | 6018| callback | AsyncCallback<Array<[Sensor](#sensor9)>> | Yes | Callback used to return the sensor list.| 6019 6020**Error codes** 6021 6022For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 6023 6024| ID| Error Message | 6025| -------- | ------------------------------------------------------------ | 6026| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6027| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6028 6029**Example** 6030 6031```ts 6032import { sensor } from '@kit.SensorServiceKit'; 6033import { BusinessError } from '@kit.BasicServicesKit'; 6034 6035// Use try catch to capture possible exceptions. 6036try { 6037 sensor.getSensorList((err: BusinessError, data: Array<sensor.Sensor>) => { 6038 if (err) { 6039 console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); 6040 return; 6041 } 6042 for (let i = 0; i < data.length; i++) { 6043 console.info('Succeeded in getting data[' + i + ']: ' + JSON.stringify(data[i])); 6044 } 6045 }); 6046} catch (error) { 6047 let e: BusinessError = error as BusinessError; 6048 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 6049} 6050``` 6051 6052## sensor.getSensorList<sup>9+</sup> 6053 6054 getSensorList(): Promise<Array<Sensor>> 6055 6056Obtains information about all sensors on the device. This API uses a promise to return the result. 6057 6058**System capability**: SystemCapability.Sensors.Sensor 6059 6060**Return value** 6061 6062| Type | Description | 6063| ---------------------------------------- | ---------------- | 6064| Promise<Array<[Sensor](#sensor9)>> | Promise used to return the sensor list.| 6065 6066**Error codes** 6067 6068For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 6069 6070| ID| Error Message | 6071| -------- | ------------------------------------------------------------ | 6072| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6073| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6074 6075**Example** 6076 6077```ts 6078import { sensor } from '@kit.SensorServiceKit'; 6079import { BusinessError } from '@kit.BasicServicesKit'; 6080 6081// Use try catch to capture possible exceptions. 6082try { 6083 sensor.getSensorList().then((data: Array<sensor.Sensor>) => { 6084 for (let i = 0; i < data.length; i++) { 6085 console.info('Succeeded in getting data[' + i + ']: ' + JSON.stringify(data[i])); 6086 } 6087 }, (err: BusinessError) => { 6088 console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); 6089 }); 6090} catch (error) { 6091 let e: BusinessError = error as BusinessError; 6092 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 6093} 6094``` 6095 6096## sensor.getSensorListSync<sup>12+</sup> 6097 6098getSensorListSync(): Array<Sensor> 6099 6100Obtains information about all sensors on the device. This API returns the result synchronously. 6101 6102**System capability**: SystemCapability.Sensors.Sensor 6103 6104**Return value** 6105 6106| Type | Description | 6107| --------------------------------------- | -------------------------------- | 6108| Array<[Sensor](#sensor9)> | List of sensor attributes.| 6109 6110**Error codes** 6111 6112For details about the following error codes, see [Sensor Error Codes](errorcode-sensor.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 6113 6114| ID| Error Message | 6115| -------- | ------------------ | 6116| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6117 6118**Example** 6119 6120```ts 6121import { sensor } from '@kit.SensorServiceKit'; 6122import { BusinessError } from '@kit.BasicServicesKit'; 6123 6124// Use try catch to capture possible exceptions. 6125try { 6126 let ret = sensor.getSensorListSync() 6127 for (let i = 0; i < ret.length; i++) { 6128 console.info('Succeeded in getting sensor: ' + JSON.stringify(ret[i])); 6129 } 6130} catch(error) { 6131 let e: BusinessError = error as BusinessError; 6132 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 6133} 6134``` 6135 6136## sensor.getSingleSensor<sup>9+</sup> 6137 6138getSingleSensor(type: SensorId, callback: AsyncCallback<Sensor>): void 6139 6140Obtains information about the sensor of a specific type. This API uses an asynchronous callback to return the result. 6141 6142**System capability**: SystemCapability.Sensors.Sensor 6143 6144**Parameters** 6145 6146| Name | Type | Mandatory| Description | 6147| -------- | --------------------------------------- | ---- | ---------------- | 6148| type | [SensorId](#sensorid9) | Yes | Sensor type. | 6149| callback | AsyncCallback<[Sensor](#sensor9)> | Yes | Callback used to return the sensor information.| 6150 6151**Error codes** 6152 6153For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 6154 6155| ID| Error Message | 6156| -------- | ------------------------------------------------------------ | 6157| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6158| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6159| 14500102 | The sensor is not supported by the device. | 6160 6161**Example** 6162 6163```ts 6164import { sensor } from '@kit.SensorServiceKit'; 6165import { BusinessError } from '@kit.BasicServicesKit'; 6166 6167// Use try catch to capture possible exceptions. 6168try { 6169 sensor.getSingleSensor(sensor.SensorId.ACCELEROMETER, (err: BusinessError, data: sensor.Sensor) => { 6170 if (err) { 6171 console.error(`Failed to get singleSensor. Code: ${err.code}, message: ${err.message}`); 6172 return; 6173 } 6174 console.info('Succeeded in getting sensor: ' + JSON.stringify(data)); 6175 }); 6176} catch (error) { 6177 let e: BusinessError = error as BusinessError; 6178 console.error(`Failed to get singleSensor. Code: ${e.code}, message: ${e.message}`); 6179} 6180``` 6181 6182## sensor.getSingleSensor<sup>9+</sup> 6183 6184 getSingleSensor(type: SensorId): Promise<Sensor> 6185 6186Obtains information about the sensor of a specific type. This API uses a promise to return the result. 6187 6188**System capability**: SystemCapability.Sensors.Sensor 6189 6190**Parameters** 6191 6192| Name| Type | Mandatory| Description | 6193| ------ | ---------------------- | ---- | ------------ | 6194| type | [SensorId](#sensorid9) | Yes | Sensor type.| 6195 6196**Return value** 6197 6198| Type | Description | 6199| --------------------------------- | ---------------------------- | 6200| Promise<[Sensor](#sensor9)> | Promise used to return the sensor information.| 6201 6202**Error codes** 6203 6204For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 6205 6206| ID| Error Message | 6207| -------- | ------------------------------------------------------------ | 6208| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6209| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6210| 14500102 | The sensor is not supported by the device. | 6211 6212**Example** 6213 6214```ts 6215import { sensor } from '@kit.SensorServiceKit'; 6216import { BusinessError } from '@kit.BasicServicesKit'; 6217 6218// Use try catch to capture possible exceptions. 6219try { 6220 sensor.getSingleSensor(sensor.SensorId.ACCELEROMETER).then((data: sensor.Sensor) => { 6221 console.info('Succeeded in getting sensor: ' + JSON.stringify(data)); 6222 }, (err: BusinessError) => { 6223 console.error(`Failed to get singleSensor . Code: ${err.code}, message: ${err.message}`); 6224 }); 6225} catch (error) { 6226 let e: BusinessError = error as BusinessError; 6227 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 6228} 6229``` 6230 6231## sensor.getSingleSensorSync<sup>12+</sup> 6232 6233getSingleSensorSync(type: SensorId): Sensor 6234 6235Obtains information about the sensor of a specific type. This API returns the result synchronously. 6236 6237**System capability**: SystemCapability.Sensors.Sensor 6238 6239**Parameters** 6240 6241| Name| Type | Mandatory| Description | 6242| ------ | ---------------------- | ---- | ------------ | 6243| type | [SensorId](#sensorid9) | Yes | Sensor type.| 6244 6245**Return value** 6246 6247| Type | Description | 6248| ------ | ---------------------------- | 6249| Sensor | Sensor information.| 6250 6251**Error codes** 6252 6253For details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). Error codes and error information are reported as exceptions. You need to use **try catch** to capture the exceptions that may occur during an API call. 6254 6255| ID| Error Message | 6256| -------- | ------------------------------------------------------------ | 6257| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6258| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6259| 14500102 | The sensor is not supported by the device. | 6260 6261**Example** 6262 6263```ts 6264import { sensor } from '@kit.SensorServiceKit'; 6265import { BusinessError } from '@kit.BasicServicesKit'; 6266 6267// Use try catch to capture possible exceptions. 6268try { 6269 let ret = sensor.getSingleSensorSync(sensor.SensorId.ACCELEROMETER); 6270 console.info('Succeeded in getting sensor: ' + JSON.stringify(ret)); 6271} catch (error) { 6272 let e: BusinessError = error as BusinessError; 6273 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 6274} 6275``` 6276 6277## SensorId<sup>9+</sup> 6278 6279Enumerates the sensor types. 6280 6281**System capability**: SystemCapability.Sensors.Sensor 6282 6283| Name | Value | Description | 6284| --------------------------- | ---- | ------------------------------------------------------------ | 6285| ACCELEROMETER | 1 | Acceleration sensor.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 6286| GYROSCOPE | 2 | Gyroscope sensor.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 6287| AMBIENT_LIGHT | 5 | Ambient light sensor. | 6288| MAGNETIC_FIELD | 6 | Magnetic field sensor. | 6289| BAROMETER | 8 | Barometer sensor. | 6290| HALL | 10 | Hall effect sensor. | 6291| PROXIMITY | 12 | Proximity sensor. | 6292| HUMIDITY | 13 | Humidity sensor. | 6293| ORIENTATION | 256 | Orientation sensor.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 6294| GRAVITY | 257 | Gravity sensor. | 6295| LINEAR_ACCELEROMETER | 258 | Linear acceleration sensor. | 6296| ROTATION_VECTOR | 259 | Rotation vector sensor. | 6297| AMBIENT_TEMPERATURE | 260 | Ambient temperature sensor. | 6298| MAGNETIC_FIELD_UNCALIBRATED | 261 | Uncalibrated magnetic field sensor. | 6299| GYROSCOPE_UNCALIBRATED | 263 | Uncalibrated gyroscope sensor. | 6300| SIGNIFICANT_MOTION | 264 | Significant motion sensor. | 6301| PEDOMETER_DETECTION | 265 | Pedometer detection sensor. | 6302| PEDOMETER | 266 | Pedometer sensor. | 6303| HEART_RATE | 278 | Heart rate sensor. | 6304| WEAR_DETECTION | 280 | Wear detection sensor. | 6305| ACCELEROMETER_UNCALIBRATED | 281 | Uncalibrated acceleration sensor. | 6306 6307 6308## SensorInfoParam<sup>19+</sup> 6309 6310Defines sensor parameters, including **deviceId** and **sensorIndex**. 6311 6312**System capability**: SystemCapability.Sensors.Sensor 6313 6314**Atomic service API**: This API can be used in atomic services since API version 19. 6315 6316 6317| Name | Type | Mandatory| Description | 6318|-------------|--------|----|--------------------------------------------------------------------------------------------------------------------------------------------------------| 6319| deviceId | number | No | Device ID. The default value is **-1**, which indicates the local device. You can use [getSensorListByDeviceSync](#sensorgetsensorlistbydevicesync19) to query other device IDs.<br>**Atomic service API**: This API can be used in atomic services since API version 19. | 6320| sensorIndex | number | No | Sensor index. The default value is **0**, which indicates the default sensor on the device. You can use [getSensorListByDeviceSync](#sensorgetsensorlistbydevicesync19) to query other sensor IDs.<br>**Atomic service API**: This API can be used in atomic services since API version 19.| 6321 6322 6323## SensorStatusEvent<sup>19+</sup> 6324 6325Defines a device status change event. 6326 6327**System capability**: SystemCapability.Sensors.Sensor 6328 6329| Name | Type | Read-Only| Optional| Description | 6330|----------------|---------|----|----|-----------------------------| 6331| timestamp | number | Yes | No | Timestamp when an event occurs. | 6332| sensorId | number | Yes | No | Sensor ID. | 6333| sensorIndex | number | Yes | No | Sensor index. | 6334| isSensorOnline | boolean | Yes | No | Sensor status. The value **true** indicates that the sensor is online, and the value **false** indicates the opposite.| 6335| deviceId | number | Yes | No | Device ID. | 6336| deviceName | string | Yes | No | Device name. | 6337 6338## SensorType<sup>(deprecated)</sup> 6339 6340Enumerates the sensor types. 6341 6342**System capability**: SystemCapability.Sensors.Sensor 6343 6344 6345| Name | Value | Description | 6346| ------------------------------------------ | ---- | ---------------------- | 6347| SENSOR_TYPE_ID_ACCELEROMETER | 1 | Acceleration sensor. | 6348| SENSOR_TYPE_ID_GYROSCOPE | 2 | Gyroscope sensor. | 6349| SENSOR_TYPE_ID_AMBIENT_LIGHT | 5 | Ambient light sensor. | 6350| SENSOR_TYPE_ID_MAGNETIC_FIELD | 6 | Magnetic field sensor. | 6351| SENSOR_TYPE_ID_BAROMETER | 8 | Barometer sensor. | 6352| SENSOR_TYPE_ID_HALL | 10 | Hall effect sensor. | 6353| SENSOR_TYPE_ID_PROXIMITY | 12 | Proximity sensor. | 6354| SENSOR_TYPE_ID_HUMIDITY | 13 | Humidity sensor. | 6355| SENSOR_TYPE_ID_ORIENTATION | 256 | Orientation sensor. | 6356| SENSOR_TYPE_ID_GRAVITY | 257 | Gravity sensor. | 6357| SENSOR_TYPE_ID_LINEAR_ACCELERATION | 258 | Linear acceleration sensor. | 6358| SENSOR_TYPE_ID_ROTATION_VECTOR | 259 | Rotation vector sensor. | 6359| SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 260 | Ambient temperature sensor. | 6360| SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 261 | Uncalibrated magnetic field sensor. | 6361| SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 263 | Uncalibrated gyroscope sensor. | 6362| SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 264 | Significant motion sensor. | 6363| SENSOR_TYPE_ID_PEDOMETER_DETECTION | 265 | Pedometer detection sensor. | 6364| SENSOR_TYPE_ID_PEDOMETER | 266 | Pedometer sensor. | 6365| SENSOR_TYPE_ID_HEART_RATE | 278 | Heart rate sensor. | 6366| SENSOR_TYPE_ID_WEAR_DETECTION | 280 | Wear detection sensor. | 6367| SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 281 | Uncalibrated acceleration sensor.| 6368 6369## SensorAccuracy<sup>11+</sup> 6370 6371Enumerates the accuracy levels of sensor data. 6372 6373**Atomic service API**: This API can be used in atomic services since API version 11. 6374 6375**System capability**: SystemCapability.Sensors.Sensor 6376 6377| Name | Value| Description | 6378| --------- | ---- | ------------------------ | 6379| ACCURACY_UNRELIABLE | 0 | The sensor data is unreliable.| 6380| ACCURACY_LOW | 1 | The sensor data is at a low accuracy level.| 6381| ACCURACY_MEDIUM | 2 | The sensor data is at a medium accuracy level.| 6382| ACCURACY_HIGH | 3 | The sensor data is at a high accuracy level.| 6383 6384## Response 6385 6386Describes the timestamp of the sensor data. 6387 6388**Atomic service API**: This API can be used in atomic services since API version 11. 6389 6390**System capability**: SystemCapability.Sensors.Sensor 6391 6392| Name | Type | Read-Only| Optional| Description | 6393| --------- | ------ | ---- | ---- | ------------------------ | 6394| timestamp | number | Yes | Yes | Timestamp when the sensor reports data. Time from device startup to data reporting, in nanoseconds.| 6395| accuracy<sup>11+</sup> | [SensorAccuracy](#sensoraccuracy11)<sup>11+</sup> | Yes | No | Accuracy of the sensor data.| 6396 6397## Sensor<sup>9+</sup> 6398 6399Describes the sensor information. 6400 6401**System capability**: SystemCapability.Sensors.Sensor 6402 6403| Name | Type | Read-Only| Optional| Description | 6404|-----------------------------|---------|----|----|------------------| 6405| sensorName | string | Yes | No | Sensor name. | 6406| vendorName | string | Yes | No | Vendor of the sensor. | 6407| firmwareVersion | string | Yes | No | Firmware version of the sensor. | 6408| hardwareVersion | string | Yes | No | Hardware version of the sensor. | 6409| sensorId | number | Yes | No | Sensor type ID. | 6410| maxRange | number | Yes | No | Maximum measurement range of the sensor. | 6411| minSamplePeriod | number | Yes | No | Minimum sampling period. | 6412| maxSamplePeriod | number | Yes | No | Maximum sampling period. | 6413| precision | number | Yes | No | Precision of the sensor. | 6414| power | number | Yes | No | Estimated sensor power, in mA.| 6415| sensorIndex<sup>19+</sup> | number | Yes | Yes | Sensor index. | 6416| deviceId<sup>19+</sup> | number | Yes | Yes | Device ID. | 6417| deviceName<sup>19+</sup> | string | Yes | Yes | Device name. | 6418| isLocalSensor<sup>19+</sup> | boolean | Yes | Yes | Whether the sensor is a local sensor. | 6419 6420## AccelerometerResponse 6421 6422Describes the acceleration sensor data. It extends from [Response](#response). 6423 6424**Atomic service API**: This API can be used in atomic services since API version 11. 6425 6426**System capability**: SystemCapability.Sensors.Sensor 6427 6428 6429| Name| Type | Read-Only| Optional| Description | 6430| ---- | ------ | ---- | ---- | ---------------------------------------------------------- | 6431| x | number | Yes | Yes | Acceleration along the x-axis of the device, in m/s². The value is equal to the reported physical quantity.| 6432| y | number | Yes | Yes | Acceleration along the y-axis of the device, in m/s². The value is equal to the reported physical quantity.| 6433| z | number | Yes | Yes | Acceleration along the z-axis of the device, in m/s². The value is equal to the reported physical quantity.| 6434 6435 6436## LinearAccelerometerResponse 6437 6438Describes the linear acceleration sensor data. It extends from [Response](#response). 6439 6440**System capability**: SystemCapability.Sensors.Sensor 6441 6442 6443| Name| Type | Read-Only| Optional| Description | 6444| ---- | ------ | ---- | ---- | ---------------------------------------- | 6445| x | number | Yes | Yes | Linear acceleration along the x-axis of the device, in m/s².| 6446| y | number | Yes | Yes | Linear acceleration along the y-axis of the device, in m/s².| 6447| z | number | Yes | Yes | Linear acceleration along the z-axis of the device, in m/s².| 6448 6449 6450## AccelerometerUncalibratedResponse 6451 6452Describes the uncalibrated acceleration sensor data. It extends from [Response](#response). 6453 6454**System capability**: SystemCapability.Sensors.Sensor 6455 6456 6457| Name | Type | Read-Only| Optional| Description | 6458| ----- | ------ | ---- | ---- | ---------------------------------------------- | 6459| x | number | Yes | Yes | Uncalibrated acceleration along the x-axis of the device, in m/s². | 6460| y | number | Yes | Yes | Uncalibrated acceleration along the y-axis of the device, in m/s². | 6461| z | number | Yes | Yes | Uncalibrated acceleration along the z-axis of the device, in m/s². | 6462| biasX | number | Yes | Yes | Uncalibrated acceleration bias along the x-axis of the device, in m/s².| 6463| biasY | number | Yes | Yes | Uncalibrated acceleration bias along the y-axis of the device, in m/s².| 6464| biasZ | number | Yes | Yes | Uncalibrated acceleration bias along the z-axis of the device, in m/s².| 6465 6466 6467## GravityResponse 6468 6469Describes the gravity sensor data. It extends from [Response](#response). 6470 6471**System capability**: SystemCapability.Sensors.Sensor 6472 6473 6474| Name| Type | Read-Only| Optional| Description | 6475| ---- | ------ | ---- | ---- | ---------------------------------------- | 6476| x | number | Yes | Yes | Gravitational acceleration along the x-axis of the device, in m/s².| 6477| y | number | Yes | Yes | Gravitational acceleration along the y-axis of the device, in m/s².| 6478| z | number | Yes | Yes | Gravitational acceleration along the z-axis of the device, in m/s².| 6479 6480 6481## OrientationResponse 6482 6483Describes the orientation sensor data. It extends from [Response](#response). 6484 6485**Atomic service API**: This API can be used in atomic services since API version 11. 6486 6487**System capability**: SystemCapability.Sensors.Sensor 6488 6489 6490| Name | Type | Read-Only| Optional| Description | 6491| ----- | ------ | ---- | ---- | ----------------------------------------------------- | 6492| alpha | number | Yes | Yes | Rotation angle of the device around the z-axis, in degrees. The value ranges from 0 to 360. | 6493| beta | number | Yes | Yes | Rotation angle of the device around the x-axis, in degrees. The value ranges from 0 to ±180.| 6494| gamma | number | Yes | Yes | Rotation angle of the device around the y-axis, in degrees. The value ranges from 0 to ±90. | 6495 6496 6497## RotationVectorResponse 6498 6499Describes the rotation vector sensor data. It extends from [Response](#response). 6500 6501**System capability**: SystemCapability.Sensors.Sensor 6502 6503 6504| Name| Type | Read-Only| Optional| Description | 6505| ---- | ------ | ---- | ---- | ----------------- | 6506| x | number | Yes | Yes | X-component of the rotation vector.| 6507| y | number | Yes | Yes | Y-component of the rotation vector.| 6508| z | number | Yes | Yes | Z-component of the rotation vector.| 6509| w | number | Yes | Yes | Scalar, which describes the rotation status of the device relative to a reference direction, in radians | 6510 6511 6512## GyroscopeResponse 6513 6514Describes the gyroscope sensor data. It extends from [Response](#response). 6515 6516**Atomic service API**: This API can be used in atomic services since API version 11. 6517 6518**System capability**: SystemCapability.Sensors.Sensor 6519 6520 6521| Name| Type | Read-Only| Optional| Description | 6522| ---- | ------ | ---- | ---- | ------------------------------------------------------ | 6523| x | number | Yes | Yes | Angular velocity of rotation around the x-axis of the device, in rad/s. The value is equal to the reported physical quantity.| 6524| y | number | Yes | Yes | Angular velocity of rotation around the y-axis of the device, in rad/s. The value is equal to the reported physical quantity.| 6525| z | number | Yes | Yes | Angular velocity of rotation around the z-axis of the device, in rad/s. The value is equal to the reported physical quantity.| 6526 6527 6528## GyroscopeUncalibratedResponse 6529 6530Describes the uncalibrated gyroscope sensor data. It extends from [Response](#response). 6531 6532**System capability**: SystemCapability.Sensors.Sensor 6533 6534 6535| Name | Type | Read-Only| Optional| Description | 6536| ----- | ------ | ---- | ---- | ------------------------------------------ | 6537| x | number | Yes | Yes | Uncalibrated angular velocity of rotation around the x-axis of the device, in rad/s. | 6538| y | number | Yes | Yes | Uncalibrated angular velocity of rotation around the y-axis of the device, in rad/s. | 6539| z | number | Yes | Yes | Uncalibrated angular velocity of rotation around the z-axis of the device, in rad/s. | 6540| biasX | number | Yes | Yes | Uncalibrated angular velocity bias of rotation around the x-axis of the device, in rad/s.| 6541| biasY | number | Yes | Yes | Uncalibrated angular velocity bias of rotation around the y-axis of the device, in rad/s.| 6542| biasZ | number | Yes | Yes | Uncalibrated angular velocity bias of rotation around the z-axis of the device, in rad/s.| 6543 6544 6545## SignificantMotionResponse 6546 6547Describes the significant motion sensor data. It extends from [Response](#response). 6548 6549**System capability**: SystemCapability.Sensors.Sensor 6550 6551 6552| Name | Type | Read-Only| Optional| Description | 6553| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 6554| scalar | number | Yes | Yes | Intensity of a motion. This parameter specifies whether a device has a significant motion on three physical axes (X, Y, and Z). The value **1** is reported when the device has a significant motion.| 6555 6556 6557## ProximityResponse 6558 6559Describes the proximity sensor data. It extends from [Response](#response). 6560 6561**System capability**: SystemCapability.Sensors.Sensor 6562 6563 6564| Name | Type | Read-Only| Optional| Description | 6565| -------- | ------ | ---- | ---- | ---------------------------------------------------------- | 6566| distance | number | Yes | Yes | Proximity between the visible object and the device monitor. The value **0** means the two are close to each other, and a value greater than 0 means that they are far away from each other.| 6567 6568 6569## LightResponse 6570 6571Describes the ambient light sensor data. It extends from [Response](#response). 6572 6573**System capability**: SystemCapability.Sensors.Sensor 6574 6575 6576| Name | Type | Read-Only| Optional| Description | 6577| ------------------------------- | ------ | ---- | ---- | ------------------------------------------------------------ | 6578| intensity | number | Yes | Yes | Illumination, in lux. | 6579| colorTemperature<sup>12+</sup> | number | Yes | Yes | Color temperature, in Kelvin. This parameter is optional. If this parameter is not supported, **undefined** is returned. If this parameter is supported, a normal value is returned.| 6580| infraredLuminance<sup>12+</sup> | number | Yes | Yes | IR luminance, in cd/m2. This parameter is optional. If this parameter is not supported, **undefined** is returned. If this parameter is supported, a normal value is returned.| 6581 6582 6583## HallResponse 6584 6585Describes the Hall effect sensor data. It extends from [Response](#response). 6586 6587**System capability**: SystemCapability.Sensors.Sensor 6588 6589 6590| Name | Type | Read-Only| Optional| Description | 6591| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 6592| status | number | Yes | Yes | Hall effect sensor status. This parameter specifies whether a magnetic field exists around a device. The value **0** means that a magnetic field does not exist, and a value greater than **0** means the opposite.| 6593 6594 6595## MagneticFieldResponse 6596 6597Describes the magnetic field sensor data. It extends from [Response](#response). 6598 6599**System capability**: SystemCapability.Sensors.Sensor 6600 6601 6602| Name| Type | Read-Only| Optional| Description | 6603| ---- | ------ | ---- | ---- | ---------------------------- | 6604| x | number | Yes | Yes | Magnetic field strength on the x-axis, in μT.| 6605| y | number | Yes | Yes | Magnetic field strength on the y-axis, in μT.| 6606| z | number | Yes | Yes | Magnetic field strength on the z-axis, in μT.| 6607 6608 6609## MagneticFieldUncalibratedResponse 6610 6611Describes the uncalibrated magnetic field sensor data. It extends from [Response](#response). 6612 6613**System capability**: SystemCapability.Sensors.Sensor 6614 6615 6616| Name | Type | Read-Only| Optional| Description | 6617| ----- | ------ | ---- | ---- | -------------------------------------- | 6618| x | number | Yes | Yes | Uncalibrated magnetic field strength on the x-axis, in μT. | 6619| y | number | Yes | Yes | Uncalibrated magnetic field strength on the y-axis, in μT. | 6620| z | number | Yes | Yes | Uncalibrated magnetic field strength on the z-axis, in μT. | 6621| biasX | number | Yes | Yes | Bias of the uncalibrated magnetic field strength on the x-axis, in μT.| 6622| biasY | number | Yes | Yes | Bias of the uncalibrated magnetic field strength on the y-axis, in μT.| 6623| biasZ | number | Yes | Yes | Bias of the uncalibrated magnetic field strength on the z-axis, in μT.| 6624 6625 6626## PedometerResponse 6627 6628Describes the pedometer sensor data. It extends from [Response](#response). 6629 6630**System capability**: SystemCapability.Sensors.Sensor 6631 6632 6633| Name | Type | Read-Only| Optional| Description | 6634| ----- | ------ | ---- | ---- | ---------------- | 6635| steps | number | Yes | Yes | Number of steps a user has walked.| 6636 6637 6638## HumidityResponse 6639 6640Describes the humidity sensor data. It extends from [Response](#response). 6641 6642**System capability**: SystemCapability.Sensors.Sensor 6643 6644 6645| Name | Type | Read-Only| Optional| Description | 6646| -------- | ------ | ---- | ---- | --------------------------------------------------------- | 6647| humidity | number | Yes | Yes | Ambient relative humidity, in a percentage (%).| 6648 6649 6650## PedometerDetectionResponse 6651 6652Describes the pedometer detection sensor data. It extends from [Response](#response). 6653 6654**System capability**: SystemCapability.Sensors.Sensor 6655 6656 6657| Name | Type | Read-Only| Optional| Description | 6658| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 6659| scalar | number | Yes | Yes | Pedometer detection. This parameter specifies whether a user takes a step. The value **0** means that the user does not take a step, and **1** means that the user takes a step.| 6660 6661 6662## AmbientTemperatureResponse 6663 6664Describes the ambient temperature sensor data. It extends from [Response](#response). 6665 6666**System capability**: SystemCapability.Sensors.Sensor 6667 6668 6669| Name | Type | Read-Only| Optional| Description | 6670| ----------- | ------ | ---- | ---- | -------------------------- | 6671| temperature | number | Yes | Yes | Ambient temperature, in degree Celsius.| 6672 6673 6674## BarometerResponse 6675 6676Describes the barometer sensor data. It extends from [Response](#response). 6677 6678**System capability**: SystemCapability.Sensors.Sensor 6679 6680 6681| Name | Type | Read-Only| Optional| Description | 6682| -------- | ------ | ---- | ---- | ---------------------- | 6683| pressure | number | Yes | Yes | Atmospheric pressure, in units of hPa.| 6684 6685 6686## HeartRateResponse 6687 6688Describes the heart rate sensor data. It extends from [Response](#response). 6689 6690**System capability**: SystemCapability.Sensors.Sensor 6691 6692 6693| Name | Type | Read-Only| Optional| Description | 6694| --------- | ------ | ---- | ---- | --------------------------------------- | 6695| heartRate | number | Yes | Yes | Heart rate, in beats per minute (bpm).| 6696 6697 6698## WearDetectionResponse 6699 6700Describes the wear detection sensor data. It extends from [Response](#response). 6701 6702**System capability**: SystemCapability.Sensors.Sensor 6703 6704 6705| Name | Type | Read-Only| Optional| Description | 6706| ----- | ------ | ---- | ---- | ------------------------------------------------ | 6707| value | number | Yes | Yes | Whether the device is being worn. The value **1** means that the device is being worn, and **0** means the opposite.| 6708 6709 6710## Options 6711 6712Describes the sensor data reporting frequency. 6713 6714**Atomic service API**: This API can be used in atomic services since API version 11. 6715 6716**System capability**: SystemCapability.Sensors.Sensor 6717 6718| Name | Type | Read-Only| Optional| Description | 6719| -------- | ----------------------------------------------------------- | ---- | ---- |--------------------------------------------------------------------------------------------| 6720| interval | number\|[SensorFrequency](#sensorfrequency11)<sup>11+</sup> | Yes | Yes | Frequency at which a sensor reports data. The default value is 200,000,000 ns. The maximum and minimum values of this parameter are determined by the reporting frequency supported by the hardware. If the configured frequency is greater than the maximum value, the maximum value is used for data reporting. If the configured frequency is less than the minimum value, the minimum value is used for data reporting.| 6721| sensorInfoParam<sup>19+</sup> | [SensorInfoParam](#sensorinfoparam19) | Yes| Yes| Sensor parameters, including **deviceId** and **sensorIndex**.<br>**Atomic service API**: This API can be used in atomic services since API version 19. | 6722 6723## SensorFrequency<sup>11+</sup> 6724 6725type SensorFrequency = 'game' | 'ui' | 'normal' 6726 6727Defines the reporting frequency mode of the sensor. 6728 6729**Atomic service API**: This API can be used in atomic services since API version 11. 6730 6731**System capability**: SystemCapability.Sensors.Sensor 6732 6733| Type | Description | 6734| -------- | ------------------------------------------------------------ | 6735| 'game' | Game mode, which specifies a sensor data reporting frequency of 20,000,000 ns. This parameter takes effect only when the frequency is within the frequency range supported by the hardware.| 6736| 'ui' | UI mode, which specifies a sensor data reporting frequency of 60,000,000 ns. This parameter takes effect only when the frequency is within the frequency range supported by the hardware.| 6737| 'normal' | Normal mode, which specifies a sensor data reporting frequency of 200,000,000 ns. This parameter takes effect only when the frequency is within the frequency range supported by the hardware.| 6738 6739## RotationMatrixResponse 6740 6741Describes the response for setting the rotation matrix. 6742 6743**System capability**: SystemCapability.Sensors.Sensor 6744 6745| Name | Type | Read-Only| Optional| Description | 6746| ----------- | ------------------- | ---- | ---- | ---------- | 6747| rotation | Array<number> | Yes | Yes | Rotation matrix.| 6748| inclination | Array<number> | Yes | Yes | Inclination matrix.| 6749 6750 6751## CoordinatesOptions 6752 6753Describes the coordinate options. 6754 6755**System capability**: SystemCapability.Sensors.Sensor 6756 6757| Name| Type | Read-Only| Optional| Description | 6758| ---- | ------ | ---- | ---- | ----------- | 6759| x | number | Yes | Yes | X coordinate direction.| 6760| y | number | Yes | Yes | Y coordinate direction.| 6761 6762 6763## GeomagneticResponse 6764 6765Describes a geomagnetic response object. 6766 6767**System capability**: SystemCapability.Sensors.Sensor 6768 6769| Name | Type | Read-Only| Optional| Description | 6770| --------------- | ------ | ---- | ---- | -------------------------------------------------- | 6771| x | number | Yes | Yes | North component of the geomagnetic field. | 6772| y | number | Yes | Yes | East component of the geomagnetic field. | 6773| z | number | Yes | Yes | Vertical component of the geomagnetic field. | 6774| geomagneticDip | number | Yes | Yes | Magnetic dip, also called magnetic inclination, which is the angle measured from the horizontal plane to the magnetic field vector. | 6775| deflectionAngle | number | Yes | Yes | Magnetic declination, which is the angle between true north (geographic north) and the magnetic north (the horizontal component of the field).| 6776| levelIntensity | number | Yes | Yes | Horizontal intensity of the magnetic field vector field. | 6777| totalIntensity | number | Yes | Yes | Total intensity of the magnetic field vector. | 6778 6779## LocationOptions 6780 6781Describes the geographical location. 6782 6783**System capability**: SystemCapability.Sensors.Sensor 6784 6785| Name | Type | Read-Only| Optional| Description | 6786| --------- | ------ | ---- | ---- | ---------- | 6787| latitude | number | Yes | Yes | Latitude. | 6788| longitude | number | Yes | Yes | Longitude. | 6789| altitude | number | Yes | Yes | Altitude.| 6790 6791## sensor.on<sup>(deprecated)</sup> 6792 6793### ACCELEROMETER<sup>(deprecated)</sup> 6794 6795on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>,options?: Options): void 6796 6797Subscribes to data changes of the acceleration sensor. If this API is called multiple times for the same application, the last call takes effect. 6798 6799> **NOTE** 6800> 6801> This API is deprecated since API version 9. You are advised to use [sensor.on.ACCELEROMETER](#accelerometer9)<sup>9+</sup> instead. 6802 6803**Required permissions**: ohos.permission.ACCELEROMETER 6804 6805**System capability**: SystemCapability.Sensors.Sensor 6806 6807**Parameters** 6808 6809| Name | Type | Mandatory| Description | 6810| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6811| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ACCELEROMETER**. | 6812| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | Yes | Callback used to return the acceleration sensor data. The reported data type in the callback is **AccelerometerResponse**.| 6813| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 6814 6815**Example** 6816 6817```ts 6818import { sensor } from '@kit.SensorServiceKit'; 6819 6820sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 6821 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6822 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6823 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6824}, 6825 { interval: 100000000 } 6826); 6827``` 6828 6829### LINEAR_ACCELERATION<sup>(deprecated)</sup> 6830 6831on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>, options?: Options): void 6832 6833Subscribes to data changes of the linear acceleration sensor. If this API is called multiple times for the same application, the last call takes effect. 6834 6835> **NOTE** 6836> 6837> This API is deprecated since API version 9. You are advised to use [sensor.on.LINEAR_ACCELEROMETER](#linear_accelerometer9)<sup>9+</sup> instead. 6838 6839**Required permissions**: ohos.permission.ACCELEROMETER 6840 6841**System capability**: SystemCapability.Sensors.Sensor 6842 6843**Parameters** 6844 6845| Name | Type | Mandatory| Description | 6846| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6847| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_LINEAR_ACCELERATION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_LINEAR_ACCELERATION**.| 6848| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | Yes | Callback used to return the linear acceleration sensor data. The reported data type in the callback is **LinearAccelerometerResponse**.| 6849| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 6850 6851### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 6852 6853on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void 6854 6855Subscribes to data changes of the uncalibrated acceleration sensor. If this API is called multiple times for the same application, the last call takes effect. 6856 6857> **NOTE** 6858> 6859> This API is deprecated since API version 9. You are advised to use [sensor.on.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9)<sup>9+</sup> instead. 6860 6861**Required permissions**: ohos.permission.ACCELEROMETER 6862 6863**System capability**: SystemCapability.Sensors.Sensor 6864 6865**Parameters** 6866 6867| Name | Type | Mandatory| Description | 6868| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6869| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED**.| 6870| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | Yes | Callback used to return the uncalibrated acceleration sensor data. The reported data type in the callback is **AccelerometerUncalibratedResponse**.| 6871| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 6872 6873**Example** 6874 6875```ts 6876import { sensor } from '@kit.SensorServiceKit'; 6877 6878sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 6879 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6880 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6881 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6882 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 6883 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 6884 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 6885}, 6886 { interval: 100000000 } 6887); 6888 6889``` 6890 6891### GRAVITY<sup>(deprecated)</sup> 6892 6893on(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>,options?: Options): void 6894 6895Subscribes to data changes of the gravity sensor. If this API is called multiple times for the same application, the last call takes effect. 6896 6897> **NOTE** 6898> 6899> This API is deprecated since API version 9. You are advised to use [sensor.on.GRAVITY](#gravity9)<sup>9+</sup> instead. 6900 6901**System capability**: SystemCapability.Sensors.Sensor 6902 6903**Parameters** 6904 6905| Name | Type | Mandatory| Description | 6906| -------- | ---------------------------------------------------------- | ---- | ----------------------------------------------------------- | 6907| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GRAVITY | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_GRAVITY**. | 6908| callback | Callback<[GravityResponse](#gravityresponse)> | Yes | Callback used to return the gravity sensor data. The reported data type in the callback is **GravityResponse**.| 6909| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 6910 6911**Example** 6912 6913```ts 6914import { sensor } from '@kit.SensorServiceKit'; 6915 6916sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, (data: sensor.GravityResponse) => { 6917 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6918 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6919 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6920}, 6921 { interval: 100000000 } 6922); 6923``` 6924 6925### GYROSCOPE<sup>(deprecated)</sup> 6926 6927on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void 6928 6929Subscribes to data changes of the gyroscope sensor. If this API is called multiple times for the same application, the last call takes effect. 6930 6931> **NOTE** 6932> 6933> This API is deprecated since API version 9. You are advised to use [sensor.on.GYROSCOPE](#gyroscope9)<sup>9+</sup> instead. 6934 6935**Required permissions**: ohos.permission.GYROSCOPE 6936 6937**System capability**: SystemCapability.Sensors.Sensor 6938 6939**Parameters** 6940 6941| Name | Type | Mandatory| Description | 6942| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6943| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_GYROSCOPE**. | 6944| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | Yes | Callback used to return the gyroscope sensor data. The reported data type in the callback is **GyroscopeResponse**.| 6945| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 6946 6947**Example** 6948 6949```ts 6950import { sensor } from '@kit.SensorServiceKit'; 6951 6952sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, (data: sensor.GyroscopeResponse) => { 6953 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6954 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6955 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6956}, 6957 { interval: 100000000 } 6958); 6959``` 6960 6961### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 6962 6963on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback:Callback<GyroscopeUncalibratedResponse>, options?: Options): void 6964 6965Subscribes to data changes of the uncalibrated gyroscope sensor. If this API is called multiple times for the same application, the last call takes effect. 6966 6967> **NOTE** 6968> 6969> This API is deprecated since API version 9. You are advised to use [sensor.on.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9)<sup>9+</sup> instead. 6970 6971**Required permissions**: ohos.permission.GYROSCOPE 6972 6973**System capability**: SystemCapability.Sensors.Sensor 6974 6975**Parameters** 6976 6977| Name | Type | Mandatory| Description | 6978| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6979| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED**.| 6980| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | Yes | Callback used to return the uncalibrated gyroscope sensor data. The reported data type in the callback is **GyroscopeUncalibratedResponse**.| 6981| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 6982 6983**Example** 6984 6985```ts 6986import { sensor } from '@kit.SensorServiceKit'; 6987 6988sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 6989 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6990 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6991 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6992 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 6993 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 6994 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 6995}, 6996 { interval: 100000000 } 6997); 6998``` 6999 7000### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 7001 7002on(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, options?: Options): void 7003 7004Subscribes to data changes of the significant motion sensor. If this API is called multiple times for the same application, the last call takes effect. 7005 7006> **NOTE** 7007> 7008> This API is deprecated since API version 9. You are advised to use [sensor.on.SIGNIFICANT_MOTION](#significant_motion9)<sup>9+</sup> instead. 7009 7010**System capability**: SystemCapability.Sensors.Sensor 7011 7012**Parameters** 7013 7014| Name | Type | Mandatory| Description | 7015| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7016| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_SIGNIFICANT_MOTION**.| 7017| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | Yes | Callback used to return the significant motion sensor data. The reported data type in the callback is **SignificantMotionResponse**.| 7018| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7019 7020**Example** 7021 7022```ts 7023import { sensor } from '@kit.SensorServiceKit'; 7024 7025sensor.on(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 7026 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 7027}, 7028 { interval: 100000000 } 7029); 7030``` 7031 7032### PEDOMETER_DETECTION<sup>(deprecated)</sup> 7033 7034on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, options?: Options): void 7035 7036Subscribes to data changes of the pedometer detection sensor. If this API is called multiple times for the same application, the last call takes effect. 7037 7038> **NOTE** 7039> 7040> This API is deprecated since API version 9. You are advised to use [sensor.on.PEDOMETER_DETECTION](#pedometer_detection9)<sup>9+</sup> instead. 7041 7042**Required permissions**: ohos.permission.ACTIVITY_MOTION 7043 7044**System capability**: SystemCapability.Sensors.Sensor 7045 7046**Parameters** 7047 7048| Name | Type | Mandatory| Description | 7049| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7050| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER_DETECTION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_PEDOMETER_DETECTION**.| 7051| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | Yes | Callback used to return the pedometer detection sensor data. The reported data type in the callback is **PedometerDetectionResponse**.| 7052| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7053 7054**Example** 7055 7056```ts 7057import { sensor } from '@kit.SensorServiceKit'; 7058 7059sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 7060 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 7061}, 7062 { interval: 100000000 } 7063); 7064``` 7065 7066### PEDOMETER<sup>(deprecated)</sup> 7067 7068on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void 7069 7070Subscribes to data changes of the pedometer sensor. If this API is called multiple times for the same application, the last call takes effect. 7071 7072> **NOTE** 7073> 7074> This API is deprecated since API version 9. You are advised to use [sensor.on.PEDOMETER](#pedometer9)<sup>9+</sup> instead. 7075 7076**Required permissions**: ohos.permission.ACTIVITY_MOTION 7077 7078**System capability**: SystemCapability.Sensors.Sensor 7079 7080**Parameters** 7081 7082| Name | Type | Mandatory| Description | 7083| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7084| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_PEDOMETER**. | 7085| callback | Callback<[PedometerResponse](#pedometerresponse)> | Yes | Callback used to return the pedometer sensor data. The reported data type in the callback is **PedometerResponse**.| 7086| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7087 7088**Example** 7089 7090```ts 7091import { sensor } from '@kit.SensorServiceKit'; 7092 7093sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, (data: sensor.PedometerResponse) => { 7094 console.info('Succeeded in invoking on. Steps: ' + data.steps); 7095}, 7096 { interval: 100000000 } 7097); 7098``` 7099 7100### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 7101 7102on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback:Callback<AmbientTemperatureResponse>, options?: Options): void 7103 7104Subscribes to data changes of the ambient temperature sensor. If this API is called multiple times for the same application, the last call takes effect. 7105 7106> **NOTE** 7107> 7108> This API is deprecated since API version 9. You are advised to use [sensor.on.AMBIENT_TEMPERATURE](#ambient_temperature9)<sup>9+</sup> instead. 7109 7110**System capability**: SystemCapability.Sensors.Sensor 7111 7112**Parameters** 7113 7114| Name | Type | Mandatory| Description | 7115| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7116| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_AMBIENT_TEMPERATURE**.| 7117| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | Yes | Callback used to return the ambient temperature sensor data. The reported data type in the callback is **AmbientTemperatureResponse**.| 7118| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7119 7120**Example** 7121 7122```ts 7123import { sensor } from '@kit.SensorServiceKit'; 7124 7125sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 7126 console.info('Succeeded in invoking on. Temperature: ' + data.temperature); 7127}, 7128 { interval: 100000000 } 7129); 7130``` 7131 7132### MAGNETIC_FIELD<sup>(deprecated)</sup> 7133 7134on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>,options?: Options): void 7135 7136Subscribes to data changes of the magnetic field sensor. If this API is called multiple times for the same application, the last call takes effect. 7137 7138> **NOTE** 7139> 7140> This API is deprecated since API version 9. You are advised to use [sensor.on.MAGNETIC_FIELD](#magnetic_field9)<sup>9+</sup> instead. 7141 7142**System capability**: SystemCapability.Sensors.Sensor 7143 7144**Parameters** 7145 7146| Name | Type | Mandatory| Description | 7147| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7148| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_MAGNETIC_FIELD**. | 7149| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | Yes | Callback used to return the magnetic field sensor data. The reported data type in the callback is **MagneticFieldResponse**.| 7150| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7151 7152**Example** 7153 7154```ts 7155import { sensor } from '@kit.SensorServiceKit'; 7156 7157sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 7158 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 7159 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 7160 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 7161}, 7162 { interval: 100000000 } 7163); 7164``` 7165 7166### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 7167 7168on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void 7169 7170Subscribes to data changes of the uncalibrated magnetic field sensor. If this API is called multiple times for the same application, the last call takes effect. 7171 7172> **NOTE** 7173> 7174> This API is deprecated since API version 9. You are advised to use [sensor.on.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9)<sup>9+</sup> instead. 7175 7176**System capability**: SystemCapability.Sensors.Sensor 7177 7178**Parameters** 7179 7180| Name | Type | Mandatory| Description | 7181| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7182| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED**.| 7183| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | Yes | Callback used to return the uncalibrated magnetic field sensor data. The reported data type in the callback is **MagneticFieldUncalibratedResponse**.| 7184| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7185 7186**Example** 7187 7188```ts 7189import { sensor } from '@kit.SensorServiceKit'; 7190 7191sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 7192 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 7193 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 7194 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 7195 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 7196 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 7197 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 7198}, 7199 { interval: 100000000 } 7200); 7201``` 7202 7203### PROXIMITY<sup>(deprecated)</sup> 7204 7205on(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: Callback<ProximityResponse>,options?: Options): void 7206 7207Subscribes to data changes of the proximity sensor. If this API is called multiple times for the same application, the last call takes effect. 7208 7209> **NOTE** 7210> 7211> This API is deprecated since API version 9. You are advised to use [sensor.on.PROXIMITY](#proximity9)<sup>9+</sup> instead. 7212 7213**System capability**: SystemCapability.Sensors.Sensor 7214 7215**Parameters** 7216 7217| Name | Type | Mandatory| Description | 7218| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7219| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PROXIMITY | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_PROXIMITY**. | 7220| callback | Callback<[ProximityResponse](#proximityresponse)> | Yes | Callback used to return the proximity sensor data. The reported data type in the callback is **ProximityResponse**.| 7221| options | [Options](#options) | No | List of optional parameters. The default value is 200,000,000 ns. This parameter is used to set the data reporting frequency when proximity sensor events are frequently triggered.| 7222 7223**Example** 7224 7225```ts 7226import { sensor } from '@kit.SensorServiceKit'; 7227 7228sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, (data: sensor.ProximityResponse) => { 7229 console.info('Succeeded in invoking on. Distance: ' + data.distance); 7230}, 7231 { interval: 100000000 } 7232); 7233``` 7234 7235### HUMIDITY<sup>(deprecated)</sup> 7236 7237on(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: Callback<HumidityResponse>,options?: Options): void 7238 7239Subscribes to data changes of the humidity sensor. If this API is called multiple times for the same application, the last call takes effect. 7240 7241> **NOTE** 7242> 7243> This API is deprecated since API version 9. You are advised to use [sensor.on.HUMIDITY](#humidity9)<sup>9+</sup> instead. 7244 7245**System capability**: SystemCapability.Sensors.Sensor 7246 7247**Parameters** 7248 7249| Name | Type | Mandatory| Description | 7250| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7251| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HUMIDITY | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HUMIDITY**. | 7252| callback | Callback<[HumidityResponse](#humidityresponse)> | Yes | Callback used to return the humidity sensor data. The reported data type in the callback is **HumidityResponse**.| 7253| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7254 7255**Example** 7256 7257```ts 7258import { sensor } from '@kit.SensorServiceKit'; 7259 7260sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, (data: sensor.HumidityResponse) => { 7261 console.info('Succeeded in invoking on. Humidity: ' + data.humidity); 7262}, 7263 { interval: 100000000 } 7264); 7265``` 7266 7267### BAROMETER<sup>(deprecated)</sup> 7268 7269on(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: Callback<BarometerResponse>,options?: Options): void 7270 7271Subscribes to data changes of the barometer sensor. If this API is called multiple times for the same application, the last call takes effect. 7272 7273> **NOTE** 7274> 7275> This API is deprecated since API version 9. You are advised to use [sensor.on.BAROMETER](#barometer9)<sup>9+</sup> instead. 7276 7277**System capability**: SystemCapability.Sensors.Sensor 7278 7279**Parameters** 7280 7281| Name | Type | Mandatory| Description | 7282| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7283| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_BAROMETER | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_BAROMETER**. | 7284| callback | Callback<[BarometerResponse](#barometerresponse)> | Yes | Callback used to return the barometer sensor data. The reported data type in the callback is **BarometerResponse**.| 7285| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7286 7287**Example** 7288 7289```ts 7290import { sensor } from '@kit.SensorServiceKit'; 7291 7292sensor.on(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, (data: sensor.BarometerResponse) => { 7293 console.info('Succeeded in invoking on. Atmospheric pressure: ' + data.pressure); 7294}, 7295 { interval: 100000000 } 7296); 7297``` 7298 7299### HALL<sup>(deprecated)</sup> 7300 7301on(type: SensorType.SENSOR_TYPE_ID_HALL, callback: Callback<HallResponse>, options?: Options): void 7302 7303Subscribes to data changes of the Hall effect sensor. If this API is called multiple times for the same application, the last call takes effect. 7304 7305> **NOTE** 7306> 7307> This API is deprecated since API version 9. You are advised to use [sensor.on.HALL](#hall9)<sup>9+</sup> instead. 7308 7309**System capability**: SystemCapability.Sensors.Sensor 7310 7311**Parameters** 7312 7313| Name | Type | Mandatory| Description | 7314| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7315| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HALL | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HALL**. | 7316| callback | Callback<[HallResponse](#hallresponse)> | Yes | Callback used to return the Hall effect sensor data. The reported data type in the callback is **HallResponse**.| 7317| options | [Options](#options) | No | List of optional parameters. The default value is 200,000,000 ns. This parameter is used to set the data reporting frequency when Hall effect events are frequently triggered.| 7318 7319**Example** 7320 7321```ts 7322import { sensor } from '@kit.SensorServiceKit'; 7323 7324sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HALL, (data: sensor.HallResponse) => { 7325 console.info('Succeeded in invoking on. Status: ' + data.status); 7326}, 7327 { interval: 100000000 } 7328); 7329``` 7330 7331### AMBIENT_LIGHT<sup>(deprecated)</sup> 7332 7333on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: Callback<LightResponse>, options?: Options): void 7334 7335Subscribes to data changes of the ambient light sensor. If this API is called multiple times for the same application, the last call takes effect. 7336 7337> **NOTE** 7338> 7339> This API is deprecated since API version 9. You are advised to use [sensor.on.AMBIENT_LIGHT](#ambient_light9)<sup>9+</sup> instead. 7340 7341**System capability**: SystemCapability.Sensors.Sensor 7342 7343**Parameters** 7344 7345| Name | Type | Mandatory| Description | 7346| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 7347| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_LIGHT | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_AMBIENT_LIGHT**. | 7348| callback | Callback<[LightResponse](#lightresponse)> | Yes | Callback used to return the ambient light sensor data. The reported data type in the callback is **LightResponse**.| 7349| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 7350 7351**Example** 7352 7353```ts 7354import { sensor } from '@kit.SensorServiceKit'; 7355 7356sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, (data: sensor.LightResponse) => { 7357 console.info('Succeeded in invoking on. Illumination: ' + data.intensity); 7358}, 7359 { interval: 100000000 } 7360); 7361``` 7362 7363### ORIENTATION<sup>(deprecated)</sup> 7364 7365on(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: Callback<OrientationResponse>, options?: Options): void 7366 7367Subscribes to data changes of the orientation sensor. If this API is called multiple times for the same application, the last call takes effect. 7368 7369> **NOTE** 7370> 7371> This API is deprecated since API version 9. You are advised to use [sensor.on.ORIENTATION](#orientation9)<sup>9+</sup> instead. 7372 7373**System capability**: SystemCapability.Sensors.Sensor 7374 7375**Parameters** 7376 7377| Name | Type | Mandatory| Description | 7378| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7379| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ORIENTATION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ORIENTATION**. | 7380| callback | Callback<[OrientationResponse](#orientationresponse)> | Yes | Callback used to return the orientation sensor data. The reported data type in the callback is **OrientationResponse**.| 7381| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7382 7383**Example** 7384 7385```ts 7386import { sensor } from '@kit.SensorServiceKit'; 7387 7388sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, (data: sensor.OrientationResponse) => { 7389 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 7390 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 7391 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 7392}, 7393 { interval: 100000000 } 7394); 7395``` 7396 7397### HEART_RATE<sup>(deprecated)</sup> 7398 7399on(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback<HeartRateResponse>, options?: Options): void 7400 7401Subscribes to data changes of the heart rate sensor. If this API is called multiple times for the same application, the last call takes effect. 7402 7403> **NOTE** 7404> 7405> This API is deprecated since API version 9. You are advised to use [sensor.on.HEART_RATE](#heart_rate9)<sup>9+</sup> instead. 7406 7407**Required permissions**: ohos.permission.HEALTH_DATA 7408 7409**System capability**: SystemCapability.Sensors.Sensor 7410 7411**Parameters** 7412 7413| Name | Type | Mandatory| Description | 7414| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7415| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HEART_RATE | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HEART_RATE**. | 7416| callback | Callback<[HeartRateResponse](#heartrateresponse)> | Yes | Callback used to return the heart rate sensor data. The reported data type in the callback is **HeartRateResponse**.| 7417| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7418 7419### ROTATION_VECTOR<sup>(deprecated)</sup> 7420 7421on(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR,callback: Callback<RotationVectorResponse>,options?: Options): void 7422 7423Subscribes to data changes of the rotation vector sensor. If this API is called multiple times for the same application, the last call takes effect. 7424 7425> **NOTE** 7426> 7427> This API is deprecated since API version 9. You are advised to use [sensor.on.ROTATION_VECTOR](#rotation_vector9)<sup>9+</sup> instead. 7428 7429**System capability**: SystemCapability.Sensors.Sensor 7430 7431**Parameters** 7432 7433| Name | Type | Mandatory| Description | 7434| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7435| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ROTATION_VECTOR | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ROTATION_VECTOR**.| 7436| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | Yes | Callback used to return the rotation vector sensor data. The reported data type in the callback is **RotationVectorResponse**.| 7437| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7438 7439**Example** 7440 7441```ts 7442import { sensor } from '@kit.SensorServiceKit'; 7443 7444sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 7445 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 7446 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 7447 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 7448 console.info('Succeeded in invoking on. Scalar quantity: ' + data.w); 7449}, 7450 { interval: 100000000 } 7451); 7452``` 7453 7454### WEAR_DETECTION<sup>(deprecated)</sup> 7455 7456on(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback<WearDetectionResponse>,options?: Options): void 7457 7458Subscribes to data changes of the wear detection sensor. If this API is called multiple times for the same application, the last call takes effect. 7459 7460> **NOTE** 7461> 7462> This API is deprecated since API version 9. You are advised to use [sensor.on.WEAR_DETECTION](#wear_detection9)<sup>9+</sup> instead. 7463 7464**System capability**: SystemCapability.Sensors.Sensor 7465 7466**Parameters** 7467 7468| Name | Type | Mandatory| Description | 7469| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7470| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_WEAR_DETECTION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_WEAR_DETECTION**. | 7471| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | Yes | Callback used to return the wear detection sensor data. The reported data type in the callback is **WearDetectionResponse**.| 7472| options | [Options](#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. | 7473 7474**Example** 7475 7476```ts 7477import { sensor } from '@kit.SensorServiceKit'; 7478 7479sensor.on(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 7480 console.info('Succeeded in invoking on. Wear status: ' + data.value); 7481}, 7482 { interval: 100000000 } 7483); 7484``` 7485 7486## sensor.once<sup>(deprecated)</sup> 7487 7488### ACCELEROMETER<sup>(deprecated)</sup> 7489 7490once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>): void 7491 7492Subscribes to only one data change of the acceleration sensor. 7493 7494> **NOTE** 7495> 7496> This API is deprecated since API version 9. You are advised to use [sensor.once.ACCELEROMETER](#accelerometer9-1)<sup>9+</sup> instead. 7497 7498**Required permissions**: ohos.permission.ACCELEROMETER 7499 7500**System capability**: SystemCapability.Sensors.Sensor 7501 7502**Parameters** 7503 7504| Name | Type | Mandatory| Description | 7505| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7506| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ACCELEROMETER**. | 7507| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | Yes | One-shot callback used to return the acceleration sensor data. The reported data type in the callback is **AccelerometerResponse**.| 7508 7509**Example** 7510 7511```ts 7512import { sensor } from '@kit.SensorServiceKit'; 7513 7514sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 7515 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7516 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7517 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7518}); 7519``` 7520 7521### LINEAR_ACCELERATION<sup>(deprecated)</sup> 7522 7523once(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>): void 7524 7525Subscribes to only one data change of the linear acceleration sensor. 7526 7527> **NOTE** 7528> 7529> This API is deprecated since API version 9. You are advised to use [sensor.once.LINEAR_ACCELEROMETER](#linear_accelerometer9-1)<sup>9+</sup> instead. 7530 7531**Required permissions**: ohos.permission.ACCELERATION 7532 7533**System capability**: SystemCapability.Sensors.Sensor 7534 7535**Parameters** 7536 7537| Name | Type | Mandatory| Description | 7538| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7539| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_LINEAR_ACCELERATION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_LINEAR_ACCELERATION**. | 7540| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | Yes | One-shot callback used to return the linear acceleration sensor data. The reported data type in the callback is **LinearAccelerometerResponse**.| 7541 7542### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 7543 7544once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>): void 7545 7546Subscribes to only one data change of the uncalibrated acceleration sensor. 7547 7548> **NOTE** 7549> 7550> This API is deprecated since API version 9. You are advised to use [sensor.once.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9-1)<sup>9+</sup> instead. 7551 7552**Required permissions**: ohos.permission.ACCELEROMETER 7553 7554**System capability**: SystemCapability.Sensors.Sensor 7555 7556**Parameters** 7557 7558| Name | Type | Mandatory| Description | 7559| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7560| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED**.| 7561| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | Yes | One-shot callback used to return the uncalibrated acceleration sensor data. The reported data type in the callback is **AccelerometerUncalibratedResponse**.| 7562 7563**Example** 7564 7565```ts 7566import { sensor } from '@kit.SensorServiceKit'; 7567 7568sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 7569 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7570 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7571 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7572 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 7573 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 7574 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 7575}); 7576``` 7577 7578### GRAVITY<sup>(deprecated)</sup> 7579 7580once(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>): void 7581 7582Subscribes to only one data change of the gravity sensor. 7583 7584> **NOTE** 7585> 7586> This API is deprecated since API version 9. You are advised to use [sensor.once.GRAVITY](#gravity9-1)<sup>9+</sup> instead. 7587 7588**System capability**: SystemCapability.Sensors.Sensor 7589 7590**Parameters** 7591 7592| Name | Type | Mandatory| Description | 7593| -------- | ---------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7594| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GRAVITY | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_GRAVITY**. | 7595| callback | Callback<[GravityResponse](#gravityresponse)> | Yes | One-shot callback used to return the gravity sensor data. The reported data type in the callback is **GravityResponse**.| 7596 7597**Example** 7598 7599```ts 7600import { sensor } from '@kit.SensorServiceKit'; 7601 7602sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, (data: sensor.GravityResponse) => { 7603 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7604 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7605 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7606 }); 7607``` 7608 7609### GYROSCOPE<sup>(deprecated)</sup> 7610 7611once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>): void 7612 7613Subscribes to only one data change of the gyroscope sensor. 7614 7615> **NOTE** 7616> 7617> This API is deprecated since API version 9. You are advised to use [sensor.once.GYROSCOPE](#gyroscope9-1)<sup>9+</sup> instead. 7618 7619**Required permissions**: ohos.permission.GYROSCOPE 7620 7621**System capability**: SystemCapability.Sensors.Sensor 7622 7623**Parameters** 7624 7625| Name | Type | Mandatory| Description | 7626| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7627| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_GYROSCOPE**. | 7628| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | Yes | One-shot callback used to return the gyroscope sensor data. The reported data type in the callback is **GyroscopeResponse**.| 7629 7630**Example** 7631 7632```ts 7633import { sensor } from '@kit.SensorServiceKit'; 7634 7635sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, (data: sensor.GyroscopeResponse) => { 7636 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7637 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7638 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7639}); 7640``` 7641 7642### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 7643 7644once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback: Callback<GyroscopeUncalibratedResponse>): void 7645 7646Subscribes to only one data change of the uncalibrated gyroscope sensor. 7647 7648> **NOTE** 7649> 7650> This API is deprecated since API version 9. You are advised to use [sensor.once.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9-1)<sup>9+</sup> instead. 7651 7652**Required permissions**: ohos.permission.GYROSCOPE 7653 7654**System capability**: SystemCapability.Sensors.Sensor 7655 7656**Parameters** 7657 7658| Name | Type | Mandatory| Description | 7659| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7660| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED**.| 7661| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | Yes | One-shot callback used to return the uncalibrated gyroscope sensor data. The reported data type in the callback is **GyroscopeUncalibratedResponse**.| 7662 7663**Example** 7664 7665 7666```ts 7667import { sensor } from '@kit.SensorServiceKit'; 7668 7669sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 7670 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7671 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7672 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7673 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 7674 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 7675 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 7676}); 7677``` 7678 7679### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 7680 7681once(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION,callback: Callback<SignificantMotionResponse>): void 7682 7683Subscribes to only one data change of the significant motion sensor. 7684 7685> **NOTE** 7686> 7687> This API is deprecated since API version 9. You are advised to use [sensor.once.SIGNIFICANT_MOTION](#significant_motion9-1)<sup>9+</sup> instead. 7688 7689**System capability**: SystemCapability.Sensors.Sensor 7690 7691**Parameters** 7692 7693| Name | Type | Mandatory| Description | 7694| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7695| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_SIGNIFICANT_MOTION**. | 7696| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | Yes | One-shot callback used to return the significant motion sensor data. The reported data type in the callback is **SignificantMotionResponse**.| 7697 7698**Example** 7699 7700```ts 7701import { sensor } from '@kit.SensorServiceKit'; 7702 7703sensor.once(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 7704 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 7705}); 7706``` 7707 7708### PEDOMETER_DETECTION<sup>(deprecated)</sup> 7709 7710once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION,callback: Callback<PedometerDetectionResponse>): void 7711 7712Subscribes to only one data change of the pedometer detection sensor. 7713 7714> **NOTE** 7715> 7716> This API is deprecated since API version 9. You are advised to use [sensor.once.PEDOMETER_DETECTION](#pedometer_detection9-1)<sup>9+</sup> instead. 7717 7718**Required permissions**: ohos.permission.ACTIVITY_MOTION 7719 7720**System capability**: SystemCapability.Sensors.Sensor 7721 7722**Parameters** 7723 7724| Name | Type | Mandatory| Description | 7725| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7726| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER_DETECTION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_PEDOMETER_DETECTION**. | 7727| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | Yes | One-shot callback used to return the pedometer detection sensor data. The reported data type in the callback is **PedometerDetectionResponse**.| 7728 7729**Example** 7730 7731```ts 7732import { sensor } from '@kit.SensorServiceKit'; 7733 7734sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 7735 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 7736}); 7737``` 7738 7739### PEDOMETER<sup>(deprecated)</sup> 7740 7741once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>): void 7742 7743Subscribes to only one data change of the pedometer sensor. 7744 7745> **NOTE** 7746> 7747> This API is deprecated since API version 9. You are advised to use [sensor.once.PEDOMETER](#pedometer9-1)<sup>9+</sup> instead. 7748 7749**Required permissions**: ohos.permission.ACTIVITY_MOTION 7750 7751**System capability**: SystemCapability.Sensors.Sensor 7752 7753**Parameters** 7754 7755| Name | Type | Mandatory| Description | 7756| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7757| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_PEDOMETER**. | 7758| callback | Callback<[PedometerResponse](#pedometerresponse)> | Yes | One-shot callback used to return the pedometer sensor data. The reported data type in the callback is **PedometerResponse**.| 7759 7760**Example** 7761 7762```ts 7763import { sensor } from '@kit.SensorServiceKit'; 7764 7765sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, (data: sensor.PedometerResponse) => { 7766 console.info('Succeeded in invoking once. Steps: ' + data.steps); 7767}); 7768``` 7769 7770### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 7771 7772once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback: Callback<AmbientTemperatureResponse>): void 7773 7774Subscribes to only one data change of the ambient temperature sensor. 7775 7776> **NOTE** 7777> 7778> This API is deprecated since API version 9. You are advised to use [sensor.once.AMBIENT_TEMPERATURE](#ambient_temperature9-1)<sup>9+</sup> instead. 7779 7780**System capability**: SystemCapability.Sensors.Sensor 7781 7782**Parameters** 7783 7784| Name | Type | Mandatory| Description | 7785| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7786| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_AMBIENT_TEMPERATURE**. | 7787| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | Yes | One-shot callback used to return the ambient temperature sensor data. The reported data type in the callback is **AmbientTemperatureResponse**.| 7788 7789**Example** 7790 7791```ts 7792import { sensor } from '@kit.SensorServiceKit'; 7793 7794sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 7795 console.info('Succeeded in invoking once. Temperature: ' + data.temperature); 7796}); 7797``` 7798 7799### MAGNETIC_FIELD<sup>(deprecated)</sup> 7800 7801once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>): void 7802 7803Subscribes to only one data change of the magnetic field sensor. 7804 7805> **NOTE** 7806> 7807> This API is deprecated since API version 9. You are advised to use [sensor.once.MAGNETIC_FIELD](#magnetic_field9-1)<sup>9+</sup> instead. 7808 7809**System capability**: SystemCapability.Sensors.Sensor 7810 7811**Parameters** 7812 7813| Name | Type | Mandatory| Description | 7814| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7815| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_MAGNETIC_FIELD**. | 7816| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | Yes | One-shot callback used to return the magnetic field sensor data. The reported data type in the callback is **MagneticFieldResponse**.| 7817 7818**Example** 7819 7820```ts 7821import { sensor } from '@kit.SensorServiceKit'; 7822 7823sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 7824 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7825 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7826 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7827}); 7828``` 7829 7830### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 7831 7832once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>): void 7833 7834Subscribes to only one data change of the uncalibrated magnetic field sensor. 7835 7836> **NOTE** 7837> 7838> This API is deprecated since API version 9. You are advised to use [sensor.once.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9-1)<sup>9+</sup> instead. 7839 7840**System capability**: SystemCapability.Sensors.Sensor 7841 7842**Parameters** 7843 7844| Name | Type | Mandatory| Description | 7845| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7846| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED**.| 7847| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | Yes | One-shot callback used to return the uncalibrated magnetic field sensor data. The reported data type in the callback is **MagneticFieldUncalibratedResponse**.| 7848 7849**Example** 7850 7851```ts 7852import { sensor } from '@kit.SensorServiceKit'; 7853 7854sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 7855 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7856 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7857 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7858 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 7859 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 7860 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 7861}); 7862``` 7863 7864### PROXIMITY<sup>(deprecated)</sup> 7865 7866once(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: Callback<ProximityResponse>): void 7867 7868Subscribes to only one data change of the proximity sensor. 7869 7870> **NOTE** 7871> 7872> This API is deprecated since API version 9. You are advised to use [sensor.once.PROXIMITY](#proximity9-1)<sup>9+</sup> instead. 7873 7874**System capability**: SystemCapability.Sensors.Sensor 7875 7876**Parameters** 7877 7878| Name | Type | Mandatory| Description | 7879| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7880| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PROXIMITY | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_PROXIMITY**. | 7881| callback | Callback<[ProximityResponse](#proximityresponse)> | Yes | One-shot callback used to return the proximity sensor data. The reported data type in the callback is **ProximityResponse**.| 7882 7883**Example** 7884 7885```ts 7886import { sensor } from '@kit.SensorServiceKit'; 7887 7888sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, (data: sensor.ProximityResponse) => { 7889 console.info('Succeeded in invoking once. Distance: ' + data.distance); 7890} 7891); 7892``` 7893 7894### HUMIDITY<sup>(deprecated)</sup> 7895 7896once(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: Callback<HumidityResponse>): void 7897 7898Subscribes to only one data change of the humidity sensor. 7899 7900> **NOTE** 7901> 7902> This API is deprecated since API version 9. You are advised to use [sensor.once.HUMIDITY](#humidity9-1)<sup>9+</sup> instead. 7903 7904**System capability**: SystemCapability.Sensors.Sensor 7905 7906**Parameters** 7907 7908| Name | Type | Mandatory| Description | 7909| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7910| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HUMIDITY | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HUMIDITY**. | 7911| callback | Callback<[HumidityResponse](#humidityresponse)> | Yes | One-shot callback used to return the humidity sensor data. The reported data type in the callback is **HumidityResponse**.| 7912 7913**Example** 7914 7915```ts 7916import { sensor } from '@kit.SensorServiceKit'; 7917 7918sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, (data: sensor.HumidityResponse) => { 7919 console.info('Succeeded in invoking once. Humidity: ' + data.humidity); 7920}); 7921``` 7922 7923### BAROMETER<sup>(deprecated)</sup> 7924 7925once(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: Callback<BarometerResponse>): void 7926 7927Subscribes to only one data change of the barometer sensor. 7928 7929> **NOTE** 7930> 7931> This API is deprecated since API version 9. You are advised to use [sensor.once.BAROMETER](#barometer9-1)<sup>9+</sup> instead. 7932 7933**System capability**: SystemCapability.Sensors.Sensor 7934 7935**Parameters** 7936 7937| Name | Type | Mandatory| Description | 7938| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7939| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_BAROMETER | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_BAROMETER**. | 7940| callback | Callback<[BarometerResponse](#barometerresponse)> | Yes | One-shot callback used to return the barometer sensor data. The reported data type in the callback is **BarometerResponse**.| 7941 7942**Example** 7943 7944```ts 7945import { sensor } from '@kit.SensorServiceKit'; 7946 7947sensor.once(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, (data: sensor.BarometerResponse) => { 7948 console.info('Succeeded in invoking once. Atmospheric pressure: ' + data.pressure); 7949}); 7950``` 7951 7952### HALL<sup>(deprecated)</sup> 7953 7954once(type: SensorType.SENSOR_TYPE_ID_HALL, callback: Callback<HallResponse>): void 7955 7956Subscribes to only one data change of the Hall effect sensor. 7957 7958> **NOTE** 7959> 7960> This API is deprecated since API version 9. You are advised to use [sensor.once.HALL](#hall9-1)<sup>9+</sup> instead. 7961 7962**System capability**: SystemCapability.Sensors.Sensor 7963 7964**Parameters** 7965 7966| Name | Type | Mandatory| Description | 7967| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7968| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HALL | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HALL**. | 7969| callback | Callback<[HallResponse](#hallresponse)> | Yes | One-shot callback used to return the Hall effect sensor data. The reported data type in the callback is **HallResponse**.| 7970 7971**Example** 7972 7973```ts 7974import { sensor } from '@kit.SensorServiceKit'; 7975 7976sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HALL, (data: sensor.HallResponse) => { 7977 console.info('Succeeded in invoking once. Status: ' + data.status); 7978}); 7979``` 7980 7981### AMBIENT_LIGHT<sup>(deprecated)</sup> 7982 7983once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: Callback<LightResponse>): void 7984 7985Subscribes to only one data change of the ambient light sensor. 7986 7987> **NOTE** 7988> 7989> This API is deprecated since API version 9. You are advised to use [sensor.once.AMBIENT_LIGHT](#ambient_light9-1)<sup>9+</sup> instead. 7990 7991**System capability**: SystemCapability.Sensors.Sensor 7992 7993**Parameters** 7994 7995| Name | Type | Mandatory| Description | 7996| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7997| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_LIGHT | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_AMBIENT_LIGHT**. | 7998| callback | Callback<[LightResponse](#lightresponse)> | Yes | One-shot callback used to return the ambient light sensor data. The reported data type in the callback is **LightResponse**.| 7999 8000**Example** 8001 8002```ts 8003import { sensor } from '@kit.SensorServiceKit'; 8004 8005sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, (data: sensor.LightResponse) => { 8006 console.info('Succeeded in invoking once. invoking once. Illumination: ' + data.intensity); 8007}); 8008``` 8009 8010### ORIENTATION<sup>(deprecated)</sup> 8011 8012once(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: Callback<OrientationResponse>): void 8013 8014Subscribes to only one data change of the orientation sensor. 8015 8016> **NOTE** 8017> 8018> This API is deprecated since API version 9. You are advised to use [sensor.once.ORIENTATION](#orientation9-1)<sup>9+</sup> instead. 8019 8020**System capability**: SystemCapability.Sensors.Sensor 8021 8022**Parameters** 8023 8024| Name | Type | Mandatory| Description | 8025| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8026| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ORIENTATION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ORIENTATION**. | 8027| callback | Callback<[OrientationResponse](#orientationresponse)> | Yes | One-shot callback used to return the orientation sensor data. The reported data type in the callback is **OrientationResponse**.| 8028 8029**Example** 8030 8031```ts 8032import { sensor } from '@kit.SensorServiceKit'; 8033 8034sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, (data: sensor.OrientationResponse) => { 8035 console.info('Succeeded in invoking the device rotating at an angle around the X axis: ' + data.beta); 8036 console.info('Succeeded in invoking the device rotating at an angle around the Y axis: ' + data.gamma); 8037 console.info('Succeeded in invoking the device rotating at an angle around the Z axis: ' + data.alpha); 8038}); 8039``` 8040 8041### ROTATION_VECTOR<sup>(deprecated)</sup> 8042 8043once(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback: Callback<RotationVectorResponse>): void 8044 8045Subscribes to only one data change of the rotation vector sensor. 8046 8047> **NOTE** 8048> 8049> This API is deprecated since API version 9. You are advised to use [sensor.once.ROTATION_VECTOR](#rotation_vector9-1)<sup>9+</sup> instead. 8050 8051**System capability**: SystemCapability.Sensors.Sensor 8052 8053**Parameters** 8054 8055| Name | Type | Mandatory| Description | 8056| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8057| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ROTATION_VECTOR | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ROTATION_VECTOR**. | 8058| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | Yes | One-shot callback used to return the rotation vector sensor data. The reported data type in the callback is **RotationVectorResponse**.| 8059 8060**Example** 8061 8062```ts 8063import { sensor } from '@kit.SensorServiceKit'; 8064 8065sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 8066 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 8067 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 8068 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 8069 console.info('Succeeded in invoking once. Scalar quantity: ' + data.w); 8070}); 8071``` 8072 8073### HEART_RATE<sup>(deprecated)</sup> 8074 8075once(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback<HeartRateResponse>): void 8076 8077Subscribes to only one data change of the heart rate sensor. 8078 8079> **NOTE** 8080> 8081> This API is deprecated since API version 9. You are advised to use [sensor.once.HEART_RATE](#heart_rate9-1)<sup>9+</sup> instead. 8082 8083**Required permissions**: ohos.permission.HEART_RATE 8084 8085**System capability**: SystemCapability.Sensors.Sensor 8086 8087**Parameters** 8088 8089| Name | Type | Mandatory| Description | 8090| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8091| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HEART_RATE | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HEART_RATE**. | 8092| callback | Callback<[HeartRateResponse](#heartrateresponse)> | Yes | One-shot callback used to return the heart rate sensor data. The reported data type in the callback is **HeartRateResponse**.| 8093 8094**Example** 8095 8096 8097```ts 8098import { sensor } from '@kit.SensorServiceKit'; 8099 8100sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, (data: sensor.HeartRateResponse) => { 8101 console.info("Succeeded in invoking once. Heart rate: " + data.heartRate); 8102}); 8103``` 8104 8105### WEAR_DETECTION<sup>(deprecated)</sup> 8106 8107once(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback<WearDetectionResponse>): void 8108 8109Subscribes to only one data change of the wear detection sensor. 8110 8111> **NOTE** 8112> 8113> This API is deprecated since API version 9. You are advised to use [sensor.once.WEAR_DETECTION](#wear_detection9-1)<sup>9+</sup> instead. 8114 8115**System capability**: SystemCapability.Sensors.Sensor 8116 8117**Parameters** 8118 8119| Name | Type | Mandatory| Description | 8120| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8121| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_WEAR_DETECTION | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_WEAR_DETECTION**. | 8122| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | Yes | One-shot callback used to return the wear detection sensor data. The reported data type in the callback is **WearDetectionResponse**.| 8123 8124**Example** 8125 8126 8127```ts 8128import { sensor } from '@kit.SensorServiceKit'; 8129 8130sensor.once(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 8131 console.info("Succeeded in invoking once. Wear status: " + data.value); 8132}); 8133``` 8134 8135## sensor.off<sup>(deprecated)</sup> 8136 8137### ACCELEROMETER<sup>(deprecated)</sup> 8138 8139off(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback?: Callback<AccelerometerResponse>): void 8140 8141Unsubscribes from sensor data changes. 8142 8143> **NOTE** 8144> 8145> This API is deprecated since API version 9. You are advised to use [sensor.off.ACCELEROMETER<sup>9+</sup>](#accelerometer9-2) instead. 8146 8147**Required permissions**: ohos.permission.ACCELEROMETER 8148 8149**System capability**: SystemCapability.Sensors.Sensor 8150 8151**Parameters** 8152 8153| Name | Type | Mandatory| Description | 8154| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8155| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_ACCELEROMETER**.| 8156| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8157 8158**Example** 8159 8160```ts 8161import { sensor } from '@kit.SensorServiceKit'; 8162 8163function callback(data: sensor.AccelerometerResponse) { 8164 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8165 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8166 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8167} 8168 8169sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 8170``` 8171 8172### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 8173 8174off(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, callback?: Callback<AccelerometerUncalibratedResponse>): void 8175 8176Unsubscribes from sensor data changes. 8177 8178> **NOTE** 8179> 8180> This API is deprecated since API version 9. You are advised to use [sensor.off.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9-2)<sup>9+</sup> instead. 8181 8182**Required permissions**: ohos.permission.ACCELEROMETER 8183 8184**System capability**: SystemCapability.Sensors.Sensor 8185 8186**Parameters** 8187 8188| Name | Type | Mandatory| Description | 8189| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8190| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED**.| 8191| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8192 8193**Example** 8194 8195```ts 8196import { sensor } from '@kit.SensorServiceKit'; 8197 8198function callback(data: sensor.AccelerometerUncalibratedResponse) { 8199 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8200 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8201 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8202 console.info('Succeeded in invoking off. X-coordinate bias: ' + data.biasX); 8203 console.info('Succeeded in invoking off. Y-coordinate bias: ' + data.biasY); 8204 console.info('Succeeded in invoking off. Z-coordinate bias: ' + data.biasZ); 8205} 8206 8207sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, callback); 8208``` 8209 8210### AMBIENT_LIGHT<sup>(deprecated)</sup> 8211 8212off(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback?: Callback<LightResponse>): void 8213 8214Unsubscribes from sensor data changes. 8215 8216> **NOTE** 8217> 8218> This API is deprecated since API version 9. You are advised to use [sensor.off.AMBIENT_LIGHT](#ambient_light9-2)<sup>9+</sup> instead. 8219 8220**System capability**: SystemCapability.Sensors.Sensor 8221 8222**Parameters** 8223 8224| Name | Type | Mandatory| Description | 8225| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8226| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_LIGHT | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_AMBIENT_LIGHT**.| 8227| callback | Callback<[LightResponse](#lightresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8228 8229**Example** 8230 8231```ts 8232import { sensor } from '@kit.SensorServiceKit'; 8233 8234function callback(data: sensor.LightResponse) { 8235 console.info('Succeeded in invoking off. Illumination: ' + data.intensity); 8236} 8237 8238sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); 8239``` 8240 8241### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 8242 8243off(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, callback?: Callback<AmbientTemperatureResponse>): void 8244 8245Unsubscribes from sensor data changes. 8246 8247> **NOTE** 8248> 8249> This API is deprecated since API version 9. You are advised to use [sensor.off.AMBIENT_TEMPERATURE](#ambient_temperature9-2)<sup>9+</sup> instead. 8250 8251**System capability**: SystemCapability.Sensors.Sensor 8252 8253**Parameters** 8254 8255| Name | Type | Mandatory| Description | 8256| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8257| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_AMBIENT_TEMPERATURE**.| 8258| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8259 8260**Example** 8261 8262```ts 8263import { sensor } from '@kit.SensorServiceKit'; 8264 8265function callback(data: sensor.AmbientTemperatureResponse) { 8266 console.info('Succeeded in invoking off. Temperature: ' + data.temperature); 8267} 8268 8269sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, callback); 8270``` 8271 8272### BAROMETER<sup>(deprecated)</sup> 8273 8274off(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback?: Callback<BarometerResponse>): void 8275 8276Unsubscribes from sensor data changes. 8277 8278> **NOTE** 8279> 8280> This API is deprecated since API version 9. You are advised to use [sensor.off.BAROMETER](#barometer9-2)<sup>9+</sup> instead. 8281 8282**System capability**: SystemCapability.Sensors.Sensor 8283 8284**Parameters** 8285 8286| Name | Type | Mandatory| Description | 8287| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8288| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_BAROMETER | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_BAROMETER**. | 8289| callback | Callback<[BarometerResponse](#barometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8290 8291**Example** 8292 8293```ts 8294import { sensor } from '@kit.SensorServiceKit'; 8295 8296function callback(data: sensor.BarometerResponse) { 8297 console.info('Succeeded in invoking off. Atmospheric pressure: ' + data.pressure); 8298} 8299 8300sensor.off(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, callback); 8301``` 8302 8303### GRAVITY<sup>(deprecated)</sup> 8304 8305off(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback?: Callback<GravityResponse>): void 8306 8307Unsubscribes from sensor data changes. 8308 8309> **NOTE** 8310> 8311> This API is deprecated since API version 9. You are advised to use [sensor.off.GRAVITY](#gravity9-2)<sup>9+</sup> instead. 8312 8313**System capability**: SystemCapability.Sensors.Sensor 8314 8315**Parameters** 8316 8317| Name | Type | Mandatory| Description | 8318| -------- | ---------------------------------------------------------- | ---- | ------------------------------------------------------------ | 8319| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GRAVITY | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_GRAVITY**. | 8320| callback | Callback<[GravityResponse](#gravityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8321 8322**Example** 8323 8324```ts 8325import { sensor } from '@kit.SensorServiceKit'; 8326 8327function callback(data: sensor.GravityResponse) { 8328 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8329 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8330 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8331} 8332 8333sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, callback); 8334``` 8335 8336### GYROSCOPE<sup>(deprecated)</sup> 8337 8338off(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback?: Callback<GyroscopeResponse>): void 8339 8340Unsubscribes from sensor data changes. 8341 8342> **NOTE** 8343> 8344> This API is deprecated since API version 9. You are advised to use [sensor.off.GYROSCOPE](#gyroscope9-2)<sup>9+</sup> instead. 8345 8346**Required permissions**: ohos.permission.GYROSCOPE 8347 8348**System capability**: SystemCapability.Sensors.Sensor 8349 8350**Parameters** 8351 8352| Name | Type | Mandatory| Description | 8353| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8354| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_GYROSCOPE**. | 8355| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8356 8357**Example** 8358 8359```ts 8360import { sensor } from '@kit.SensorServiceKit'; 8361 8362function callback(data: sensor.GyroscopeResponse) { 8363 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8364 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8365 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8366} 8367 8368sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback); 8369``` 8370 8371### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 8372 8373off(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, callback?: Callback<GyroscopeUncalibratedResponse>): void 8374 8375Unsubscribes from sensor data changes. 8376 8377> **NOTE** 8378> 8379> This API is deprecated since API version 9. You are advised to use [sensor.off.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9-2)<sup>9+</sup> instead. 8380 8381**Required permissions**: ohos.permission.GYROSCOPE 8382 8383**System capability**: SystemCapability.Sensors.Sensor 8384 8385**Parameters** 8386 8387| Name | Type | Mandatory| Description | 8388| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8389| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED**.| 8390| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8391 8392**Example** 8393 8394```ts 8395import { sensor } from '@kit.SensorServiceKit'; 8396 8397function callback(data: sensor.GyroscopeUncalibratedResponse) { 8398 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8399 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8400 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8401} 8402 8403sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, callback); 8404``` 8405 8406### HALL<sup>(deprecated)</sup> 8407 8408off(type: SensorType.SENSOR_TYPE_ID_HALL, callback?: Callback<HallResponse>): void 8409 8410Unsubscribes from sensor data changes. 8411 8412> **NOTE** 8413> 8414> This API is deprecated since API version 9. You are advised to use [sensor.off.HALL](#hall9-2)<sup>9+</sup> instead. 8415 8416**System capability**: SystemCapability.Sensors.Sensor 8417 8418**Parameters** 8419 8420| Name | Type | Mandatory| Description | 8421| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 8422| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HALL | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_HALL**. | 8423| callback | Callback<[HallResponse](#hallresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8424 8425**Example** 8426 8427```ts 8428import { sensor } from '@kit.SensorServiceKit'; 8429 8430function callback(data: sensor.HallResponse) { 8431 console.info('Succeeded in invoking off. Status: ' + data.status); 8432} 8433 8434sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HALL, callback); 8435``` 8436 8437### HEART_RATE<sup>(deprecated)</sup> 8438 8439off(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback?: Callback<HeartRateResponse>): void 8440 8441Unsubscribes from sensor data changes. 8442 8443> **NOTE** 8444> 8445> This API is deprecated since API version 9. You are advised to use [sensor.off.HEART_RATE](#heart_rate9-2)<sup>9+</sup> instead. 8446 8447**Required permissions**: ohos.permission.HEALTH_DATA 8448 8449**System capability**: SystemCapability.Sensors.Sensor 8450 8451**Parameters** 8452 8453| Name | Type | Mandatory| Description | 8454| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8455| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HEART_RATE | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_HEART_RATE**. | 8456| callback | Callback<[HeartRateResponse](#heartrateresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8457 8458**Example** 8459 8460```ts 8461import { sensor } from '@kit.SensorServiceKit'; 8462 8463function callback(data: sensor.HeartRateResponse) { 8464 console.info('Succeeded in invoking off. Heart rate: ' + data.heartRate); 8465} 8466 8467sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, callback); 8468``` 8469 8470### HUMIDITY<sup>(deprecated)</sup> 8471 8472off(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback?: Callback<HumidityResponse>): void 8473 8474Unsubscribes from sensor data changes. 8475 8476> **NOTE** 8477> 8478> This API is deprecated since API version 9. You are advised to use [sensor.off.HUMIDITY](#humidity9-2)<sup>9+</sup> instead. 8479 8480**System capability**: SystemCapability.Sensors.Sensor 8481 8482**Parameters** 8483 8484| Name | Type | Mandatory| Description | 8485| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 8486| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HUMIDITY | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_HUMIDITY**. | 8487| callback | Callback<[HumidityResponse](#humidityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8488 8489**Example** 8490 8491```ts 8492import { sensor } from '@kit.SensorServiceKit'; 8493 8494function callback(data: sensor.HumidityResponse) { 8495 console.info('Succeeded in invoking off. Humidity: ' + data.humidity); 8496} 8497 8498sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, callback); 8499``` 8500 8501### LINEAR_ACCELERATION<sup>(deprecated)</sup> 8502 8503off(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback?: Callback<LinearAccelerometerResponse>): void 8504 8505Unsubscribes from sensor data changes. 8506 8507> **NOTE** 8508> 8509> This API is deprecated since API version 9. You are advised to use [sensor.off.LINEAR_ACCELEROMETER](#linear_accelerometer9-2)<sup>9+</sup> instead. 8510 8511**Required permissions**: ohos.permission.ACCELEROMETER 8512 8513**System capability**: SystemCapability.Sensors.Sensor 8514 8515**Parameters** 8516 8517| Name | Type | Mandatory| Description | 8518| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8519| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_LINEAR_ACCELERATION | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_LINEAR_ACCELERATION**.| 8520| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8521 8522**Example** 8523 8524```ts 8525import { sensor } from '@kit.SensorServiceKit'; 8526 8527function callback(data: sensor.LinearAccelerometerResponse) { 8528 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8529 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8530 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8531} 8532 8533sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback); 8534``` 8535 8536### MAGNETIC_FIELD<sup>(deprecated)</sup> 8537 8538 off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback?: Callback<MagneticFieldResponse>): void 8539 8540Unsubscribes from sensor data changes. 8541 8542> **NOTE** 8543> 8544> This API is deprecated since API version 9. You are advised to use [sensor.off.MAGNETIC_FIELD](#magnetic_field9-2)<sup>9+</sup> instead. 8545 8546**System capability**: SystemCapability.Sensors.Sensor 8547 8548**Parameters** 8549 8550| Name | Type | Mandatory| Description | 8551| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8552| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_MAGNETIC_FIELD**. | 8553| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8554 8555**Example** 8556 8557```ts 8558import { sensor } from '@kit.SensorServiceKit'; 8559 8560function callback(data: sensor.MagneticFieldResponse) { 8561 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8562 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8563 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8564} 8565 8566sensor.off(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback); 8567``` 8568 8569### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 8570 8571 off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, callback?: Callback<MagneticFieldUncalibratedResponse>): void 8572 8573Unsubscribes from sensor data changes. 8574 8575> **NOTE** 8576> 8577> This API is deprecated since API version 9. You are advised to use [sensor.off.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9-2)<sup>9+</sup> instead. 8578 8579**System capability**: SystemCapability.Sensors.Sensor 8580 8581**Parameters** 8582 8583| Name | Type | Mandatory| Description | 8584| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8585| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED**.| 8586| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8587 8588**Example** 8589 8590```ts 8591import { sensor } from '@kit.SensorServiceKit'; 8592 8593function callback(data: sensor.MagneticFieldUncalibratedResponse) { 8594 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8595 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8596 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8597 console.info('Succeeded in invoking off. X-coordinate bias: ' + data.biasX); 8598 console.info('Succeeded in invoking off. Y-coordinate bias: ' + data.biasY); 8599 console.info('Succeeded in invoking off. Z-coordinate bias: ' + data.biasZ); 8600} 8601 8602sensor.off(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, callback); 8603``` 8604 8605### ORIENTATION<sup>(deprecated)</sup> 8606 8607 off(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback?: Callback<OrientationResponse>): void 8608 8609Unsubscribes from sensor data changes. 8610 8611> **NOTE** 8612> 8613> This API is deprecated since API version 9. You are advised to use [sensor.off.ORIENTATION](#orientation9-2)<sup>9+</sup> instead. 8614 8615**System capability**: SystemCapability.Sensors.Sensor 8616 8617**Parameters** 8618 8619| Name | Type | Mandatory| Description | 8620| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8621| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ORIENTATION | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_ORIENTATION**. | 8622| callback | Callback<[OrientationResponse](#orientationresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8623 8624**Example** 8625 8626```ts 8627import { sensor } from '@kit.SensorServiceKit'; 8628 8629function callback(data: sensor.OrientationResponse) { 8630 console.info('Succeeded in invoking off. The device rotates at an angle around the X axis: ' + data.beta); 8631 console.info('Succeeded in invoking off. The device rotates at an angle around the Y axis: ' + data.gamma); 8632 console.info('Succeeded in invoking off. The device rotates at an angle around the Z axis: ' + data.alpha); 8633} 8634 8635sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, callback); 8636``` 8637 8638### PEDOMETER<sup>(deprecated)</sup> 8639 8640off(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback?: Callback<PedometerResponse>): void 8641 8642Unsubscribes from sensor data changes. 8643 8644> **NOTE** 8645> 8646> This API is deprecated since API version 9. You are advised to use [sensor.off.PEDOMETER](#pedometer9-2)<sup>9+</sup> instead. 8647 8648**Required permissions**: ohos.permission.ACTIVITY_MOTION 8649 8650**System capability**: SystemCapability.Sensors.Sensor 8651 8652**Parameters** 8653 8654| Name | Type | Mandatory| Description | 8655| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8656| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_PEDOMETER**. | 8657| callback | Callback<[PedometerResponse](#pedometerresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8658 8659**Example** 8660 8661```ts 8662import { sensor } from '@kit.SensorServiceKit'; 8663 8664function callback(data: sensor.PedometerResponse) { 8665 console.info('Succeeded in invoking off. Steps: ' + data.steps); 8666} 8667 8668sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, callback); 8669``` 8670 8671### PEDOMETER_DETECTION<sup>(deprecated)</sup> 8672 8673off(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback?: Callback<PedometerDetectionResponse>): void 8674 8675Unsubscribes from sensor data changes. 8676 8677> **NOTE** 8678> 8679> This API is deprecated since API version 9. You are advised to use [sensor.off.PEDOMETER_DETECTION](#pedometer_detection9-2)<sup>9+</sup> instead. 8680 8681**Required permissions**: ohos.permission.ACTIVITY_MOTION 8682 8683**System capability**: SystemCapability.Sensors.Sensor 8684 8685**Parameters** 8686 8687| Name | Type | Mandatory| Description | 8688| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8689| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER_DETECTION | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_PEDOMETER_DETECTION**.| 8690| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8691 8692**Example** 8693 8694```ts 8695import { sensor } from '@kit.SensorServiceKit'; 8696 8697function callback(data: sensor.PedometerDetectionResponse) { 8698 console.info('Succeeded in invoking off. Scalar data: ' + data.scalar); 8699} 8700 8701sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback); 8702``` 8703 8704### PROXIMITY<sup>(deprecated)</sup> 8705 8706off(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback?: Callback<ProximityResponse>): void 8707 8708Unsubscribes from sensor data changes. 8709 8710> **NOTE** 8711> 8712> This API is deprecated since API version 9. You are advised to use [sensor.off.PROXIMITY](#proximity9-2)<sup>9+</sup> instead. 8713 8714**System capability**: SystemCapability.Sensors.Sensor 8715 8716**Parameters** 8717 8718| Name | Type | Mandatory| Description | 8719| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8720| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PROXIMITY | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_PROXIMITY**. | 8721| callback | Callback<[ProximityResponse](#proximityresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8722 8723**Example** 8724 8725```ts 8726import { sensor } from '@kit.SensorServiceKit'; 8727 8728function callback(data: sensor.ProximityResponse) { 8729 console.info('Succeeded in invoking off. Distance: ' + data.distance); 8730} 8731 8732sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, callback); 8733``` 8734 8735### ROTATION_VECTOR<sup>(deprecated)</sup> 8736 8737off(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback?: Callback<RotationVectorResponse>): void 8738 8739Unsubscribes from sensor data changes. 8740 8741> **NOTE** 8742> 8743> This API is deprecated since API version 9. You are advised to use [sensor.off.ROTATION_VECTOR](#rotation_vector9-2)<sup>9+</sup> instead. 8744 8745**System capability**: SystemCapability.Sensors.Sensor 8746 8747**Parameters** 8748 8749| Name | Type | Mandatory| Description | 8750| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8751| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ROTATION_VECTOR | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_ROTATION_VECTOR**.| 8752| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8753 8754**Example** 8755 8756```ts 8757import { sensor } from '@kit.SensorServiceKit'; 8758 8759function callback(data: sensor.RotationVectorResponse) { 8760 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8761 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8762 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8763 console.info('Succeeded in invoking off. Scalar quantity: ' + data.w); 8764} 8765 8766sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 8767``` 8768 8769### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 8770 8771off(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback?: Callback<SignificantMotionResponse>): void 8772 8773Unsubscribes from the significant motion sensor data. 8774 8775> **NOTE** 8776> 8777> This API is deprecated since API version 9. You are advised to use [sensor.off.SIGNIFICANT_MOTION](#significant_motion9-2)<sup>9+</sup> instead. 8778 8779**System capability**: SystemCapability.Sensors.Sensor 8780 8781**Parameters** 8782 8783| Name | Type | Mandatory| Description | 8784| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8785| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_SIGNIFICANT_MOTION**.| 8786| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8787 8788**Example** 8789 8790```ts 8791import { sensor } from '@kit.SensorServiceKit'; 8792 8793function callback(data: sensor.SignificantMotionResponse) { 8794 console.info('Succeeded in invoking off. Scalar data: ' + data.scalar); 8795} 8796 8797sensor.off(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback); 8798``` 8799 8800### WEAR_DETECTION<sup>(deprecated)</sup> 8801 8802off(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback?: Callback<WearDetectionResponse>): void 8803 8804Unsubscribes from sensor data changes. 8805 8806> **NOTE** 8807> 8808> This API is deprecated since API version 9. You are advised to use [sensor.off.WEAR_DETECTION](#wear_detection9-2)<sup>9+</sup> instead. 8809 8810**System capability**: SystemCapability.Sensors.Sensor 8811 8812**Parameters** 8813 8814| Name | Type | Mandatory| Description | 8815| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8816| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_WEAR_DETECTION | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_WEAR_DETECTION**.| 8817| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 8818 8819**Example** 8820 8821```ts 8822import { sensor } from '@kit.SensorServiceKit'; 8823 8824function accCallback(data: sensor.WearDetectionResponse) { 8825 console.info('Succeeded in invoking off. Wear status: ' + data.value); 8826} 8827 8828sensor.off(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, accCallback); 8829``` 8830 8831## sensor.transformCoordinateSystem<sup>(deprecated)</sup> 8832 8833transformCoordinateSystem(inRotationVector: Array<number>, coordinates: CoordinatesOptions, callback: AsyncCallback<Array<number>>): void 8834 8835Rotates a rotation vector so that it can represent the coordinate system in different ways. This API uses an asynchronous callback to return the result. 8836 8837> **NOTE** 8838> 8839> This API is deprecated since API version 9. You are advised to use [sensor.transformRotationMatrix](#sensortransformrotationmatrix9)<sup>9+</sup> instead. 8840 8841**System capability**: SystemCapability.Sensors.Sensor 8842 8843**Parameters** 8844 8845| Name | Type | Mandatory| Description | 8846| ---------------- | ----------------------------------------- | ---- | -------------------------- | 8847| inRotationVector | Array<number> | Yes | Rotation vector to rotate. | 8848| coordinates | [CoordinatesOptions](#coordinatesoptions) | Yes | Direction of the coordinate system. | 8849| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the rotation vector after being rotated.| 8850 8851**Example** 8852 8853```ts 8854import { sensor } from '@kit.SensorServiceKit'; 8855import { BusinessError } from '@kit.BasicServicesKit'; 8856 8857sensor.transformCoordinateSystem([1, 0, 0, 0, 1, 0, 0, 0, 1], { x: 2, y: 3 }, 8858 (err: BusinessError, data: Array<number>) => { 8859 if (err) { 8860 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 8861 return; 8862 } 8863 console.info("Succeeded in starting Operation. Data obtained: " + data); 8864 for (let i = 0; i < data.length; i++) { 8865 console.info("Succeeded in getting transformCoordinateSystem data[ " + i + "] = " + data[i]); 8866 } 8867}) 8868``` 8869## sensor.transformCoordinateSystem<sup>(deprecated)</sup> 8870 8871transformCoordinateSystem(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>> 8872 8873Rotates a rotation vector so that it can represent the coordinate system in different ways. This API uses a promise to return the result. 8874 8875> **NOTE** 8876> 8877> This API is deprecated since API version 9. You are advised to use [sensor.transformRotationMatrix](#sensortransformrotationmatrix9-1)<sup>9+</sup> instead. 8878 8879**System capability**: SystemCapability.Sensors.Sensor 8880 8881**Parameters** 8882 8883| Name | Type | Mandatory | Description | 8884| ---------------- | ---------------------------------------- | ---- | -------- | 8885| inRotationVector | Array<number> | Yes | Rotation vector to rotate. | 8886| coordinates | [CoordinatesOptions](#coordinatesoptions) | Yes | Direction of the coordinate system.| 8887 8888**Return value** 8889 8890| Type | Description | 8891| ---------------------------------- | ---------------------------------- | 8892| Promise<Array<number>> | Promise used to return the rotation vector after being rotated.| 8893 8894**Example** 8895 8896```ts 8897import { sensor } from '@kit.SensorServiceKit'; 8898import { BusinessError } from '@kit.BasicServicesKit'; 8899 8900const promise = sensor.transformCoordinateSystem([1, 0, 0, 0, 1, 0, 0, 0, 1], { x: 2, y: 3 }); 8901promise.then((data: Array<number>) => { 8902 console.info("Succeeded in starting Operation"); 8903 for (let i = 0; i < data.length; i++) { 8904 console.info("Succeeded in getting transformCoordinateSystem data[ " + i + "] = " + data[i]); 8905 } 8906}).catch((err: BusinessError) => { 8907 console.error(`Failed to operate.`); 8908}) 8909``` 8910 8911## sensor.getGeomagneticField<sup>(deprecated)</sup> 8912 8913getGeomagneticField(locationOptions: LocationOptions, timeMillis: number, callback: AsyncCallback<GeomagneticResponse>): void 8914 8915Obtains the geomagnetic field of a geographic location. This API uses an asynchronous callback to return the result. 8916 8917> **NOTE** 8918> 8919> This API is deprecated since API version 9. You are advised to use [sensor.getGeomagneticInfo](#sensorgetgeomagneticinfo9)<sup>9+</sup> instead. 8920 8921**System capability**: SystemCapability.Sensors.Sensor 8922 8923**Parameters** 8924 8925| Name | Type | Mandatory| Description | 8926| --------------- | ------------------------------------------------------------ | ---- | ---------------------------------- | 8927| locationOptions | [LocationOptions](#locationoptions) | Yes | Geographic location. | 8928| timeMillis | number | Yes | Time for obtaining the magnetic declination, in milliseconds.| 8929| callback | AsyncCallback<[GeomagneticResponse](#geomagneticresponse)> | Yes | Callback used to return the geomagnetic field. | 8930 8931**Example** 8932 8933```ts 8934import { sensor } from '@kit.SensorServiceKit'; 8935import { BusinessError } from '@kit.BasicServicesKit'; 8936 8937sensor.getGeomagneticField({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000, 8938 (err: BusinessError, data: sensor.GeomagneticResponse) => { 8939 if (err) { 8940 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 8941 return; 8942 } 8943 console.info('Succeeded in getting sensor_getGeomagneticField_callback x: ' + data.x + ',y: ' + data.y + ',z: ' + 8944 data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + 8945 ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); 8946}); 8947``` 8948## sensor.getGeomagneticField<sup>(deprecated)</sup> 8949 8950getGeomagneticField(locationOptions: LocationOptions, timeMillis: number): Promise<GeomagneticResponse> 8951 8952Obtains the geomagnetic field of a geographic location. This API uses a promise to return the result. 8953 8954> **NOTE** 8955> 8956> This API is deprecated since API version 9. You are advised to use [sensor.getGeomagneticInfo](#sensorgetgeomagneticinfo9-1)<sup>9+</sup> instead. 8957 8958**System capability**: SystemCapability.Sensors.Sensor 8959 8960**Parameters** 8961 8962| Name | Type | Mandatory | Description | 8963| --------------- | ----------------------------------- | ---- | ----------------- | 8964| locationOptions | [LocationOptions](#locationoptions) | Yes | Geographic location. | 8965| timeMillis | number | Yes | Time for obtaining the magnetic declination, in milliseconds.| 8966 8967**Return value** 8968 8969| Type | Description | 8970| ---------------------------------------------------------- | -------------------------- | 8971| Promise<[GeomagneticResponse](#geomagneticresponse)> | Promise used to return the geomagnetic field.| 8972 8973**Example** 8974 8975```ts 8976import { sensor } from '@kit.SensorServiceKit'; 8977import { BusinessError } from '@kit.BasicServicesKit'; 8978 8979const promise = sensor.getGeomagneticField({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000); 8980promise.then((data: sensor.GeomagneticResponse) => { 8981 console.info('Succeeded in getting sensor_getGeomagneticField_promise x: ' + data.x + ',y: ' + data.y + ',z: ' + 8982 data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + 8983 ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); 8984}).catch((reason: BusinessError) => { 8985 console.error(`Failed to operate.`); 8986}) 8987``` 8988 8989## sensor.getAltitude<sup>(deprecated)</sup> 8990 8991getAltitude(seaPressure: number, currentPressure: number, callback: AsyncCallback<number>): void 8992 8993Obtains the altitude at which the device is located based on the sea-level atmospheric pressure and the current atmospheric pressure. This API uses an asynchronous callback to return the result. 8994 8995> **NOTE** 8996> 8997> This API is deprecated since API version 9. You are advised to use [sensor.getDeviceAltitude](#sensorgetdevicealtitude9)<sup>9+</sup> instead. 8998 8999**System capability**: SystemCapability.Sensors.Sensor 9000 9001**Parameters** 9002 9003| Name | Type | Mandatory| Description | 9004| --------------- | --------------------------- | ---- | -------------------------------------- | 9005| seaPressure | number | Yes | Sea-level atmospheric pressure, in hPa. | 9006| currentPressure | number | Yes | Atmospheric pressure at the altitude where the device is located, in hPa. | 9007| callback | AsyncCallback<number> | Yes | Callback used to return the altitude, in meters.| 9008 9009**Example** 9010 9011```ts 9012import { sensor } from '@kit.SensorServiceKit'; 9013import { BusinessError } from '@kit.BasicServicesKit'; 9014 9015sensor.getAltitude(0, 200, (err: BusinessError, data: number) => { 9016 if (err) { 9017 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 9018 return; 9019 } 9020 console.info("Succeeded in getting getAltitude interface get data: " + data); 9021}); 9022``` 9023 9024## sensor.getAltitude<sup>(deprecated)</sup> 9025 9026getAltitude(seaPressure: number, currentPressure: number): Promise<number> 9027 9028Obtains the altitude at which the device is located based on the sea-level atmospheric pressure and the current atmospheric pressure. This API uses a promise to return the result. 9029 9030> **NOTE** 9031> 9032> This API is deprecated since API version 9. You are advised to use [sensor.getDeviceAltitude](#sensorgetdevicealtitude9-1)<sup>9+</sup> instead. 9033 9034**System capability**: SystemCapability.Sensors.Sensor 9035 9036**Parameters** 9037 9038| Name | Type | Mandatory | Description | 9039| --------------- | ------ | ---- | -------------------- | 9040| seaPressure | number | Yes | Sea-level atmospheric pressure, in hPa. | 9041| currentPressure | number | Yes | Atmospheric pressure at the altitude where the device is located, in hPa.| 9042 9043**Return value** 9044 9045| Type | Description | 9046| --------------------- | ------------------------------------------------ | 9047| Promise<number> | Promise used to return the altitude, in meters.| 9048 9049**Example** 9050 9051```ts 9052import { sensor } from '@kit.SensorServiceKit'; 9053import { BusinessError } from '@kit.BasicServicesKit'; 9054 9055const promise = sensor.getAltitude(0, 200); 9056promise.then((data: number) => { 9057 console.info('Succeeded in getting sensor_getAltitude_Promise success', data); 9058}).catch((err: BusinessError) => { 9059 console.error(`Failed to operate.`); 9060}) 9061``` 9062 9063 9064## sensor.getGeomagneticDip<sup>(deprecated)</sup> 9065 9066getGeomagneticDip(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void 9067 9068Obtains the magnetic dip based on the inclination matrix. This API uses an asynchronous callback to return the result. 9069 9070> **NOTE** 9071> 9072> This API is deprecated since API version 9. You are advised to use [sensor.getInclination](#sensorgetinclination9)<sup>9+</sup> instead. 9073 9074**System capability**: SystemCapability.Sensors.Sensor 9075 9076**Parameters** 9077 9078| Name | Type | Mandatory| Description | 9079| ----------------- | --------------------------- | ---- | -------------------------------- | 9080| inclinationMatrix | Array<number> | Yes | Inclination matrix. | 9081| callback | AsyncCallback<number> | Yes | Callback used to return the magnetic dip, in radians.| 9082 9083**Example** 9084 9085```ts 9086import { sensor } from '@kit.SensorServiceKit'; 9087import { BusinessError } from '@kit.BasicServicesKit'; 9088 9089sensor.getGeomagneticDip([1, 0, 0, 0, 1, 0, 0, 0, 1], (err: BusinessError, data: number) => { 9090 if (err) { 9091 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9092 return; 9093 } 9094 console.info("Succeeded in getting getGeomagneticDip interface get data: " + data); 9095}) 9096``` 9097 9098## sensor.getGeomagneticDip<sup>(deprecated)</sup> 9099 9100getGeomagneticDip(inclinationMatrix: Array<number>): Promise<number> 9101 9102Obtains the magnetic dip based on the inclination matrix. This API uses a promise to return the result. 9103 9104> **NOTE** 9105> 9106> This API is deprecated since API version 9. You are advised to use [sensor.getInclination](#sensorgetinclination9-1)<sup>9+</sup> instead. 9107 9108**System capability**: SystemCapability.Sensors.Sensor 9109 9110**Parameters** 9111 9112| Name | Type | Mandatory | Description | 9113| ----------------- | ------------------- | ---- | ------- | 9114| inclinationMatrix | Array<number> | Yes | Inclination matrix.| 9115 9116**Return value** 9117 9118| Type | Description | 9119| --------------------- | ---------------------------------------- | 9120| Promise<number> | Promise used to return the magnetic dip, in radians.| 9121 9122**Example** 9123 9124```ts 9125import { sensor } from '@kit.SensorServiceKit'; 9126import { BusinessError } from '@kit.BasicServicesKit'; 9127 9128const promise = sensor.getGeomagneticDip([1, 0, 0, 0, 1, 0, 0, 0, 1]); 9129promise.then((data: number) => { 9130 console.info('Succeeded in get GeomagneticDip_promise', data); 9131}).catch((err: BusinessError) => { 9132 console.error(`Failed to operate.`); 9133}) 9134``` 9135 9136## sensor. getAngleModify<sup>(deprecated)</sup> 9137 9138getAngleModify(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 9139 9140Obtains the angle change between two rotation matrices. This API uses an asynchronous callback to return the result. 9141 9142> **NOTE** 9143> 9144> This API is deprecated since API version 9. You are advised to use [sensor.getAngleVariation](#sensorgetanglevariation9)<sup>9+</sup> instead. 9145 9146**System capability**: SystemCapability.Sensors.Sensor 9147 9148**Parameters** 9149 9150| Name | Type | Mandatory| Description | 9151| --------------------- | ---------------------------------------- | ---- | ------------------------------------- | 9152| currentRotationMatrix | Array<number> | Yes | Current rotation matrix. | 9153| preRotationMatrix | Array<number> | Yes | Peer rotation matrix. | 9154| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the angle change around the z, x, and y axes.| 9155 9156**Example** 9157 9158```ts 9159import { sensor } from '@kit.SensorServiceKit'; 9160import { BusinessError } from '@kit.BasicServicesKit'; 9161 9162sensor.getAngleModify([1, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0.87, -0.50, 0, 0.50, 0.87], 9163 (err: BusinessError, data: Array<number>) => { 9164 if (err) { 9165 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9166 return; 9167 } 9168 for (let i = 0; i < data.length; i++) { 9169 console.info("data[" + i + "]: " + data[i]); 9170 } 9171}) 9172``` 9173 9174## sensor. getAngleModify<sup>(deprecated)</sup> 9175 9176getAngleModify(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>> 9177 9178Obtains the angle change between two rotation matrices. This API uses a promise to return the result. 9179 9180> **NOTE** 9181> 9182> This API is deprecated since API version 9. You are advised to use [sensor.getAngleVariation](#sensorgetanglevariation9-1)<sup>9+</sup> instead. 9183 9184**System capability**: SystemCapability.Sensors.Sensor 9185 9186**Parameters** 9187 9188| Name | Type | Mandatory | Description | 9189| --------------------- | ------------------- | ---- | --------- | 9190| currentRotationMatrix | Array<number> | Yes | Current rotation matrix.| 9191| preRotationMatrix | Array<number> | Yes | Rotation vector to rotate. | 9192 9193**Return value** 9194 9195| Type | Description | 9196| ---------------------------------- | --------------------------------------------- | 9197| Promise<Array<number>> | Promise used to return the angle change around the z, x, and y axes.| 9198 9199**Example** 9200 9201```ts 9202import { sensor } from '@kit.SensorServiceKit'; 9203import { BusinessError } from '@kit.BasicServicesKit'; 9204 9205const promise = sensor.getAngleModify([1, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0.87, -0.50, 0, 0.50, 0.87]); 9206promise.then((data: Array<number>) => { 9207 console.info('Succeeded in getting AngleModify_promise.'); 9208 for (let i = 0; i < data.length; i++) { 9209 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 9210 } 9211}).catch((reason: BusinessError) => { 9212 let e: BusinessError = reason as BusinessError; 9213 console.info("Succeeded in getting promise::catch", e); 9214}) 9215``` 9216 9217## sensor.createRotationMatrix<sup>(deprecated)</sup> 9218 9219createRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 9220 9221Converts a rotation vector into a rotation matrix. This API uses an asynchronous callback to return the result. 9222 9223> **NOTE** 9224> 9225> This API is deprecated since API version 9. You are advised to use [sensor.getRotationMatrix](#sensorgetrotationmatrix9)<sup>9+</sup> instead. 9226 9227**System capability**: SystemCapability.Sensors.Sensor 9228 9229**Parameters** 9230 9231| Name | Type | Mandatory| Description | 9232| -------------- | ---------------------------------------- | ---- | ------------------ | 9233| rotationVector | Array<number> | Yes | Rotation vector to convert. | 9234| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the rotation matrix.| 9235 9236**Example** 9237 9238```ts 9239import { sensor } from '@kit.SensorServiceKit'; 9240import { BusinessError } from '@kit.BasicServicesKit'; 9241 9242sensor.createRotationMatrix([0.20046076, 0.21907, 0.73978853, 0.60376877], 9243 (err: BusinessError, data: Array<number>) => { 9244 if (err) { 9245 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9246 return; 9247 } 9248 for (let i = 0; i < data.length; i++) { 9249 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 9250 } 9251}) 9252``` 9253 9254## sensor.createRotationMatrix<sup>(deprecated)</sup> 9255 9256createRotationMatrix(rotationVector: Array<number>): Promise<Array<number>> 9257 9258Converts a rotation vector into a rotation matrix. This API uses a promise to return the result. 9259 9260> **NOTE** 9261> 9262> This API is deprecated since API version 9. You are advised to use [sensor.getRotationMatrix](#sensorgetrotationmatrix9-1)<sup>9+</sup> instead. 9263 9264**System capability**: SystemCapability.Sensors.Sensor 9265 9266**Parameters** 9267 9268| Name | Type | Mandatory | Description | 9269| -------------- | ------------------- | ---- | ------- | 9270| rotationVector | Array<number> | Yes | Rotation vector to convert.| 9271 9272**Return value** 9273 9274| Type | Description | 9275| ---------------------------------- | -------------------------- | 9276| Promise<Array<number>> | Promise used to return the rotation matrix.| 9277 9278**Example** 9279 9280 ```ts 9281import { sensor } from '@kit.SensorServiceKit'; 9282import { BusinessError } from '@kit.BasicServicesKit'; 9283 9284const promise = sensor.createRotationMatrix([0.20046076, 0.21907, 0.73978853, 0.60376877]); 9285promise.then((data: Array<number>) => { 9286 console.info('Succeeded in getting createRotationMatrix_promise'); 9287 for (let i = 0; i < data.length; i++) { 9288 console.info("data[" + i + "]: " + data[i]); 9289 } 9290}).catch((reason: BusinessError) => { 9291 console.info("Succeeded in getting promise::catch", reason); 9292}) 9293 ``` 9294 9295## sensor.createQuaternion<sup>(deprecated)</sup> 9296 9297createQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 9298 9299Converts a rotation vector into a quaternion. This API uses an asynchronous callback to return the result. 9300 9301> **NOTE** 9302> 9303> This API is deprecated since API version 9. You are advised to use [sensor.getQuaternion](#sensorgetquaternion9)<sup>9+</sup> instead. 9304 9305**System capability**: SystemCapability.Sensors.Sensor 9306 9307**Parameters** 9308 9309| Name | Type | Mandatory| Description | 9310| -------------- | ---------------------------------------- | ---- | ---------------- | 9311| rotationVector | Array<number> | Yes | Rotation vector to convert. | 9312| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the quaternion.| 9313 9314**Example** 9315 9316```ts 9317import { sensor } from '@kit.SensorServiceKit'; 9318import { BusinessError } from '@kit.BasicServicesKit'; 9319 9320sensor.createQuaternion([0.20046076, 0.21907, 0.73978853, 0.60376877], 9321 (err: BusinessError, data: Array<number>) => { 9322 if (err) { 9323 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9324 return; 9325 } 9326 for (let i = 0; i < data.length; i++) { 9327 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 9328 } 9329}) 9330``` 9331 9332## sensor.createQuaternion<sup>(deprecated)</sup> 9333 9334createQuaternion(rotationVector: Array<number>): Promise<Array<number>> 9335 9336Converts a rotation vector into a quaternion. This API uses a promise to return the result. 9337 9338> **NOTE** 9339> 9340> This API is deprecated since API version 9. You are advised to use [sensor.getQuaternion](#sensorgetquaternion9-1)<sup>9+</sup> instead. 9341 9342**System capability**: SystemCapability.Sensors.Sensor 9343 9344**Parameters** 9345 9346| Name | Type | Mandatory | Description | 9347| -------------- | ------------------- | ---- | ------- | 9348| rotationVector | Array<number> | Yes | Rotation vector to convert.| 9349 9350**Return value** 9351 9352| Type | Description | 9353| ---------------------------------- | ------------------------ | 9354| Promise<Array<number>> | Promise used to return the quaternion.| 9355 9356**Example** 9357 9358```ts 9359import { sensor } from '@kit.SensorServiceKit'; 9360import { BusinessError } from '@kit.BasicServicesKit'; 9361 9362const promise = sensor.createQuaternion([0.20046076, 0.21907, 0.73978853, 0.60376877]); 9363promise.then((data: Array<number>) => { 9364 console.info('Succeeded in getting createQuaternion_promise'); 9365 for (let i = 0; i < data.length; i++) { 9366 console.info("data[" + i + "]: " + data[i]); 9367 } 9368}).catch((err: BusinessError) => { 9369 console.info(`Failed to get promise.`); 9370}) 9371``` 9372 9373## sensor.getDirection<sup>(deprecated)</sup> 9374 9375getDirection(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 9376 9377Obtains the device direction based on the rotation matrix. This API uses an asynchronous callback to return the result. 9378 9379> **NOTE** 9380> 9381> This API is deprecated since API version 9. You are advised to use [sensor.getOrientation](#sensorgetorientation9)<sup>9+</sup> instead. 9382 9383**System capability**: SystemCapability.Sensors.Sensor 9384 9385**Parameters** 9386 9387| Name | Type | Mandatory| Description | 9388| -------------- | ---------------------------------------- | ---- | ------------------------------------- | 9389| rotationMatrix | Array<number> | Yes | Peer rotation matrix. | 9390| callback | AsyncCallback<Array<number>> | Yes | Callback used to return the rotation angle around the z, x, and y axes.| 9391 9392**Example** 9393 9394```ts 9395import { sensor } from '@kit.SensorServiceKit'; 9396import { BusinessError } from '@kit.BasicServicesKit'; 9397 9398sensor.getDirection([1, 0, 0, 0, 1, 0, 0, 0, 1], (err: BusinessError, data: Array<number>) => { 9399 if (err) { 9400 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9401 return; 9402 } 9403 console.info("Succeeded in getting getDirection interface get data: " + data); 9404 for (let i = 1; i < data.length; i++) { 9405 console.info("Succeeded in getting sensor_getDirection_callback" + data[i]); 9406 } 9407}) 9408``` 9409 9410## sensor.getDirection<sup>(deprecated)</sup> 9411 9412getDirection(rotationMatrix: Array<number>): Promise<Array<number>> 9413 9414Obtains the device direction based on the rotation matrix. This API uses a promise to return the result. 9415 9416> **NOTE** 9417> 9418> This API is deprecated since API version 9. You are advised to use [sensor.getOrientation](#sensorgetorientation9-1)<sup>9+</sup> instead. 9419 9420**System capability**: SystemCapability.Sensors.Sensor 9421 9422**Parameters** 9423 9424| Name | Type | Mandatory | Description | 9425| -------------- | ------------------- | ---- | ------- | 9426| rotationMatrix | Array<number> | Yes | Rotation vector to rotate.| 9427 9428**Return value** 9429 9430| Type | Description | 9431| ---------------------------------- | --------------------------------------------- | 9432| Promise<Array<number>> | Promise used to return the rotation angle around the z, x, and y axes.| 9433 9434**Example** 9435 9436```ts 9437import { sensor } from '@kit.SensorServiceKit'; 9438import { BusinessError } from '@kit.BasicServicesKit'; 9439 9440const promise = sensor.getDirection([1, 0, 0, 0, 1, 0, 0, 0, 1]); 9441promise.then((data: Array<number>) => { 9442 console.info('Succeeded in getting sensor_getAltitude_Promise', data); 9443 for (let i = 1; i < data.length; i++) { 9444 console.info("Succeeded in getting sensor_getDirection_promise" + data[i]); 9445 } 9446}).catch((err: BusinessError) => { 9447 console.info(`Failed to get promise.`); 9448}) 9449``` 9450 9451## sensor.createRotationMatrix<sup>(deprecated)</sup> 9452 9453createRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void 9454 9455Creates a rotation matrix based on the gravity vector and geomagnetic vector. This API uses an asynchronous callback to return the result. 9456 9457> **NOTE** 9458> 9459> This API is deprecated since API version 9. You are advised to use [sensor.getRotationMatrix](#sensorgetrotationmatrix9-2)<sup>9+</sup> instead. 9460 9461**System capability**: SystemCapability.Sensors.Sensor 9462 9463**Parameters** 9464 9465| Name | Type | Mandatory| Description | 9466| ----------- | ------------------------------------------------------------ | ---- | ------------------ | 9467| gravity | Array<number> | Yes | Gravity vector. | 9468| geomagnetic | Array<number> | Yes | Geomagnetic vector. | 9469| callback | AsyncCallback<[RotationMatrixResponse](#rotationmatrixresponse)> | Yes | Callback used to return the rotation matrix.| 9470 9471**Example** 9472 9473```ts 9474import { sensor } from '@kit.SensorServiceKit'; 9475import { BusinessError } from '@kit.BasicServicesKit'; 9476 9477sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444], 9478 (err: BusinessError, data: sensor.RotationMatrixResponse) => { 9479 if (err) { 9480 console.error(`Failed to get create rotationMatrix. Code: ${err.code}, message: ${err.message}`); 9481 return; 9482 } 9483 console.info(JSON.stringify(data)); 9484}) 9485``` 9486 9487## sensor.createRotationMatrix<sup>(deprecated)</sup> 9488 9489createRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>): Promise<RotationMatrixResponse> 9490 9491Creates a rotation matrix based on the gravity vector and geomagnetic vector. This API uses a promise to return the result. 9492 9493> **NOTE** 9494> 9495> This API is deprecated since API version 9. You are advised to use [sensor.getRotationMatrix](#sensorgetrotationmatrix9-3)<sup>9+</sup> instead. 9496 9497**System capability**: SystemCapability.Sensors.Sensor 9498 9499**Parameters** 9500 9501| Name | Type | Mandatory | Description | 9502| ----------- | ------------------- | ---- | ------- | 9503| gravity | Array<number> | Yes | Gravity vector.| 9504| geomagnetic | Array<number> | Yes | Geomagnetic vector.| 9505 9506**Return value** 9507 9508| Type | Description | 9509| ------------------------------------------------------------ | -------------------------- | 9510| Promise<[RotationMatrixResponse](#rotationmatrixresponse)> | Promise used to return the rotation matrix.| 9511 9512**Example** 9513 9514```ts 9515import { sensor } from '@kit.SensorServiceKit'; 9516import { BusinessError } from '@kit.BasicServicesKit'; 9517 9518const promise = sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444]); 9519promise.then((data: sensor.RotationMatrixResponse) => { 9520 console.info(JSON.stringify(data)); 9521}).catch((err: BusinessError) => { 9522 console.info(`Failed to get promise.`); 9523}) 9524``` 9525