1# @ohos.sensor (传感器) 2 3sensor模块提供了获取传感器数据的能力,包括获取传感器属性列表,订阅传感器数据,以及一些通用的传感器算法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```ts 13import sensor from '@ohos.sensor'; 14``` 15## sensor.on 16 17### COLOR<sup>10+</sup> 18 19on(type: SensorId.COLOR, callback: Callback\<ColorResponse>, options?: Options): void 20 21订阅颜色传感器数据。 22 23**系统能力**:SystemCapability.Sensors.Sensor 24 25**系统API**:此接口为系统接口 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | ------------------------------------------------- | ---- | ----------------------------------------------------------- | 31| type | [SensorId](#sensorid9).COLOR | 是 | 传感器类型,该值固定为SensorId.COLOR。 | 32| callback | Callback<[ColorResponse](#colorresponse10)> | 是 | 回调函数,异步上报的传感器数据固定为ColorResponse。 | 33| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 34 35**错误码**: 36 37以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 38 39| 错误码ID | 错误信息 | 40| -------- | ------------------ | 41| 14500101 | Service exception. | 42 43**示例:** 44 45```ts 46import sensor from "@ohos.sensor" 47import BusinessError from "@ohos.base" 48 49try{ 50 sensor.on(sensor.SensorId.COLOR, (data: sensor.ColorResponse) => { 51 console.log('Succeeded in getting the intensity of light: ' + data.lightIntensity); 52 console.log('Succeeded in getting the color temperature: ' + data.colorTemperature); 53 }, { interval: 100000000 }); 54 setTimeout(() => { 55 sensor.off(sensor.SensorId.COLOR); 56 }, 500); 57} catch (error) { 58 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 59 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 60} 61``` 62 63### SAR<sup>10+</sup> 64 65on(type: SensorId.SAR, callback: Callback\<SarResponse>, options?: Options): void 66 67订阅吸收比率传感器数据。 68 69**系统能力**:SystemCapability.Sensors.Sensor 70 71**系统API**:此接口为系统接口 72 73**参数:** 74 75| 参数名 | 类型 | 必填 | 说明 | 76| -------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 77| type | [SensorId](#sensorid9).SAR | 是 | 传感器类型,该值固定为SensorId.SAR。 | 78| callback | Callback<[SarResponse](#sarresponse10)> | 是 | 回调函数,异步上报的传感器数据固定为SarResponse。 | 79| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 80 81**错误码**: 82 83以下错误码的详细介绍请参见[ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 84 85| 错误码ID | 错误信息 | 86| -------- | ------------------ | 87| 14500101 | Service exception. | 88 89**示例:** 90 91```ts 92import sensor from "@ohos.sensor" 93import BusinessError from "@ohos.base" 94 95try { 96 sensor.on(sensor.SensorId.SAR, (data: sensor.SarResponse) => { 97 console.info('Succeeded in getting specific absorption rate : ' + data.absorptionRatio); 98 }, { interval: 100000000 }); 99 setTimeout(() => { 100 sensor.off(sensor.SensorId.SAR); 101 }, 500); 102} catch (error) { 103 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 104 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 105} 106``` 107 108### ACCELEROMETER<sup>9+</sup> 109 110on(type: SensorId.ACCELEROMETER, callback: Callback<AccelerometerResponse>, options?: Options): void 111 112订阅加速度传感器数据。 113 114**需要权限**:ohos.permission.ACCELEROMETER 115 116**系统能力**:SystemCapability.Sensors.Sensor 117 118**参数:** 119 120| 参数名 | 类型 | 必填 | 说明 | 121| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 122| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 123| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerResponse。 | 124| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 125 126**错误码**: 127 128以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 129 130| 错误码ID | 错误信息 | 131| -------- | ------------------ | 132| 14500101 | Service exception. | 133 134**示例:** 135 136```ts 137import sensor from "@ohos.sensor" 138import BusinessError from "@ohos.base" 139 140try { 141 sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 142 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 143 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 144 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 145 }, { interval: 100000000 }); 146 setTimeout(() => { 147 sensor.off(sensor.SensorId.ACCELEROMETER); 148 }, 500); 149} catch (error) { 150 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 151 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 152} 153``` 154 155### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 156 157on(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void 158 159订阅未校准加速度传感器数据。 160 161**需要权限**:ohos.permission.ACCELEROMETER 162 163**系统能力**:SystemCapability.Sensors.Sensor 164 165**参数:** 166 167| 参数名 | 类型 | 必填 | 说明 | 168| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 169| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 170| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerUncalibratedResponse。 | 171| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 172 173**错误码**: 174 175以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 176 177| 错误码ID | 错误信息 | 178| -------- | ------------------ | 179| 14500101 | Service exception. | 180 181**示例:** 182 183```ts 184import sensor from "@ohos.sensor" 185import BusinessError from "@ohos.base" 186 187try { 188 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 189 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 190 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 191 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 192 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 193 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 194 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 195 }, { interval: 100000000 }); 196 setTimeout(() => { 197 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED); 198 }, 500); 199} catch (error) { 200 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 201 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 202} 203``` 204 205### AMBIENT_LIGHT<sup>9+</sup> 206 207on(type: SensorId.AMBIENT_LIGHT, callback: Callback<LightResponse>, options?: Options): void 208 209订阅环境光传感器数据。 210 211**系统能力**:SystemCapability.Sensors.Sensor 212 213**参数:** 214 215| 参数名 | 类型 | 必填 | 说明 | 216| -------- | ----------------------------------------------- | ---- | ----------------------------------------------------------- | 217| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 218| callback | Callback<[LightResponse](#lightresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LightResponse。 | 219| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 220 221**错误码**: 222 223以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 224 225| 错误码ID | 错误信息 | 226| -------- | ------------------ | 227| 14500101 | Service exception. | 228 229**示例:** 230 231```ts 232import sensor from "@ohos.sensor" 233import BusinessError from "@ohos.base" 234 235try { 236 sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => { 237 console.info('Succeeded in getting the ambient light intensity: ' + data.intensity); 238 }, { interval: 100000000 }); 239 setTimeout(() => { 240 sensor.off(sensor.SensorId.AMBIENT_LIGHT); 241 }, 500); 242} catch (error) { 243 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 244 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 245} 246``` 247 248### AMBIENT_TEMPERATURE<sup>9+</sup> 249 250on(type: SensorId.AMBIENT_TEMPERATURE, callback: Callback<AmbientTemperatureResponse>, options?: Options): void 251 252订阅温度传感器数据。 253 254**系统能力**:SystemCapability.Sensors.Sensor 255 256**参数:** 257 258| 参数名 | 类型 | 必填 | 说明 | 259| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 260| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 261| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AmbientTemperatureResponse。 | 262| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 263 264**错误码**: 265 266以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 267 268| 错误码ID | 错误信息 | 269| -------- | ------------------ | 270| 14500101 | Service exception. | 271 272**示例:** 273 274```ts 275import sensor from "@ohos.sensor" 276import BusinessError from "@ohos.base" 277 278try { 279 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 280 console.info('Succeeded in invoking on. Temperature: ' + data.temperature); 281 }, { interval: 100000000 }); 282 setTimeout(() => { 283 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE); 284 }, 500); 285} catch (error) { 286 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 287 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 288} 289``` 290 291### BAROMETER<sup>9+</sup> 292 293on(type: SensorId.BAROMETER, callback: Callback<BarometerResponse>, options?: Options): void 294 295订阅气压计传感器数据。 296 297**系统能力**:SystemCapability.Sensors.Sensor 298 299**参数:** 300 301| 参数名 | 类型 | 必填 | 说明 | 302| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 303| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 304| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为BarometerResponse。 | 305| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 306 307**错误码**: 308 309以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 310 311| 错误码ID | 错误信息 | 312| -------- | ------------------ | 313| 14500101 | Service exception. | 314 315**示例:** 316 317```ts 318import sensor from "@ohos.sensor" 319import BusinessError from "@ohos.base" 320 321try { 322 sensor.on(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => { 323 console.info('Succeeded in invoking on. Atmospheric pressure: ' + data.pressure); 324 }, { interval: 100000000 }); 325 setTimeout(() => { 326 sensor.off(sensor.SensorId.BAROMETER); 327 }, 500); 328} catch (error) { 329 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 330 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 331} 332``` 333 334### GRAVITY<sup>9+</sup> 335 336on(type: SensorId.GRAVITY, callback: Callback<GravityResponse>, options?: Options): void 337 338订阅重力传感器数据。 339 340**系统能力**:SystemCapability.Sensors.Sensor 341 342**参数:** 343 344| 参数名 | 类型 | 必填 | 说明 | 345| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------------- | 346| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 347| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GravityResponse。 | 348| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 349 350**错误码**: 351 352以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 353 354| 错误码ID | 错误信息 | 355| -------- | ------------------ | 356| 14500101 | Service exception. | 357 358**示例:** 359 360```ts 361import sensor from "@ohos.sensor" 362import BusinessError from "@ohos.base" 363 364try { 365 sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 366 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 367 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 368 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 369 }, { interval: 100000000 }); 370 setTimeout(() => { 371 sensor.off(sensor.SensorId.GRAVITY); 372 }, 500); 373} catch (error) { 374 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 375 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 376} 377``` 378 379### GYROSCOPE<sup>9+</sup> 380 381on(type: SensorId.GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void 382 383订阅校准的陀螺仪传感器数据。 384 385**需要权限**:ohos.permission.GYROSCOPE 386 387**系统能力**:SystemCapability.Sensors.Sensor 388 389**参数:** 390 391| 参数名 | 类型 | 必填 | 说明 | 392| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 393| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 394| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeResponse。 | 395| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 396 397**错误码**: 398 399以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 400 401| 错误码ID | 错误信息 | 402| -------- | ------------------ | 403| 14500101 | Service exception. | 404 405**示例:** 406 407```ts 408import sensor from "@ohos.sensor" 409import BusinessError from "@ohos.base" 410 411try { 412 sensor.on(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => { 413 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 414 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 415 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 416 }, { interval: 100000000 }); 417 setTimeout(() => { 418 sensor.off(sensor.SensorId.GYROSCOPE); 419 }, 500); 420} catch (error) { 421 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 422 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 423} 424``` 425 426### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 427 428on(type: SensorId.GYROSCOPE_UNCALIBRATED, callback: Callback<GyroscopeUncalibratedResponse>, 429 options?: Options): void 430 431订阅未校准陀螺仪传感器数据。 432 433**需要权限**:ohos.permission.GYROSCOPE 434 435**系统能力**:SystemCapability.Sensors.Sensor 436 437**参数:** 438 439| 参数名 | 类型 | 必填 | 说明 | 440| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 441| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 442| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeUncalibratedResponse。 | 443| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 444 445**错误码**: 446 447以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 448 449| 错误码ID | 错误信息 | 450| -------- | ------------------ | 451| 14500101 | Service exception. | 452 453**示例:** 454 455```ts 456import sensor from "@ohos.sensor" 457import BusinessError from "@ohos.base" 458 459try { 460 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 461 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 462 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 463 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 464 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 465 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 466 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 467 }, { interval: 100000000 }); 468 setTimeout(() => { 469 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED); 470 }, 500); 471} catch (error) { 472 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 473 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 474} 475 476``` 477 478### HALL<sup>9+</sup> 479 480on(type: SensorId.HALL, callback: Callback<HallResponse>, options?: Options): void 481 482订阅霍尔传感器数据。 483 484**系统能力**:SystemCapability.Sensors.Sensor 485 486**参数:** 487 488| 参数名 | 类型 | 必填 | 说明 | 489| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 490| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 491| callback | Callback<[HallResponse](#hallresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HallResponse。 | 492| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当霍尔事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 493 494**错误码**: 495 496以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 497 498| 错误码ID | 错误信息 | 499| -------- | ------------------ | 500| 14500101 | Service exception. | 501 502**示例:** 503 504```ts 505import sensor from "@ohos.sensor" 506import BusinessError from "@ohos.base" 507 508try { 509 sensor.on(sensor.SensorId.HALL, (data: sensor.HallResponse) => { 510 console.info('Succeeded in invoking on. Hall status: ' + data.status); 511 }, { interval: 100000000 }); 512 setTimeout(() => { 513 sensor.off(sensor.SensorId.HALL); 514 }, 500); 515} catch (error) { 516 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 517 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 518} 519 520``` 521 522### HEART_RATE<sup>9+</sup> 523 524on(type: SensorId.HEART_RATE, callback: Callback<HeartRateResponse>, options?: Options): void 525 526订阅心率传感器数据。 527 528**需要权限**:ohos.permission.READ_HEALTH_DATA 529 530**系统能力**:SystemCapability.Sensors.Sensor 531 532**参数:** 533 534| 参数名 | 类型 | 必填 | 说明 | 535| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 536| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 537| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HeartRateResponse。 | 538| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 539 540**错误码**: 541 542以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 543 544| 错误码ID | 错误信息 | 545| -------- | ------------------ | 546| 14500101 | Service exception. | 547 548**示例:** 549 550```ts 551import sensor from "@ohos.sensor" 552import BusinessError from "@ohos.base" 553 554try { 555 sensor.on(sensor.SensorId.HEART_RATE, (data: sensor.HeartRateResponse) => { 556 console.info('Succeeded in invoking on. Heart rate: ' + data.heartRate); 557 }, { interval: 100000000 }); 558 setTimeout(() => { 559 sensor.off(sensor.SensorId.HEART_RATE); 560 }, 500); 561} catch (error) { 562 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 563 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 564} 565``` 566 567### HUMIDITY<sup>9+</sup> 568 569on(type: SensorId.HUMIDITY, callback: Callback<HumidityResponse>, options?: Options): void 570 571订阅湿度传感器数据。 572 573**系统能力**:SystemCapability.Sensors.Sensor 574 575**参数:** 576 577| 参数名 | 类型 | 必填 | 说明 | 578| -------- | ----------------------------------------------------- | ---- | ----------------------------------------------------------- | 579| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 580| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HumidityResponse。 | 581| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 582 583**错误码**: 584 585以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 586 587| 错误码ID | 错误信息 | 588| -------- | ------------------ | 589| 14500101 | Service exception. | 590 591**示例:** 592 593```ts 594import sensor from "@ohos.sensor" 595import BusinessError from "@ohos.base" 596 597try { 598 sensor.on(sensor.SensorId.HUMIDITY, (data: sensor.HumidityResponse) => { 599 console.info('Succeeded in invoking on. Humidity: ' + data.humidity); 600 }, { interval: 100000000 }); 601 setTimeout(() => { 602 sensor.off(sensor.SensorId.HUMIDITY); 603 }, 500); 604} catch (error) { 605 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 606 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 607} 608``` 609 610### LINEAR_ACCELEROMETER<sup>9+</sup> 611 612on(type: SensorId.LINEAR_ACCELEROMETER, callback: Callback<LinearAccelerometerResponse>, 613 options?: Options): void 614 615订阅线性加速度传感器数据。 616 617**需要权限**:ohos.permission.ACCELEROMETER 618 619**系统能力**:SystemCapability.Sensors.Sensor 620 621**参数:** 622 623| 参数名 | 类型 | 必填 | 说明 | 624| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 625| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELEROMETER。 | 626| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LinearAccelerometerResponse。 | 627| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 628 629**错误码**: 630 631以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 632 633| 错误码ID | 错误信息 | 634| -------- | ------------------ | 635| 14500101 | Service exception. | 636 637**示例:** 638 639```ts 640import sensor from "@ohos.sensor" 641import BusinessError from "@ohos.base" 642 643try { 644 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, (data: sensor.LinearAccelerometerResponse) => { 645 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 646 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 647 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 648 }, { interval: 100000000 }); 649 setTimeout(() => { 650 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER); 651 }, 500); 652} catch (error) { 653 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 654 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 655} 656``` 657 658### MAGNETIC_FIELD<sup>9+</sup> 659 660on(type: SensorId.MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>, options?: Options): void 661 662订阅地磁传感器数据。 663 664**系统能力**:SystemCapability.Sensors.Sensor 665 666**参数:** 667 668| 参数名 | 类型 | 必填 | 说明 | 669| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 670| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 671| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldResponse。 | 672| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 673 674**错误码**: 675 676以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 677 678| 错误码ID | 错误信息 | 679| -------- | ------------------ | 680| 14500101 | Service exception. | 681 682**示例:** 683 684```ts 685import sensor from "@ohos.sensor" 686import BusinessError from "@ohos.base" 687 688try { 689 sensor.on(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 690 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 691 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 692 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 693 }, { interval: 100000000 }); 694 setTimeout(() => { 695 sensor.off(sensor.SensorId.MAGNETIC_FIELD); 696 }, 500); 697} catch (error) { 698 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 699 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 700} 701``` 702 703### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 704 705on(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void 706 707订阅未校准地磁传感器数据。 708 709**系统能力**:SystemCapability.Sensors.Sensor 710 711**参数:** 712 713| 参数名 | 类型 | 必填 | 说明 | 714| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 715| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 716| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldUncalibratedResponse。 | 717| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 718 719**错误码**: 720 721以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 722 723| 错误码ID | 错误信息 | 724| -------- | ------------------ | 725| 14500101 | Service exception. | 726 727**示例:** 728 729```ts 730import sensor from "@ohos.sensor" 731import BusinessError from "@ohos.base" 732 733try { 734 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 735 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 736 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 737 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 738 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 739 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 740 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 741 }, { interval: 100000000 }); 742 setTimeout(() => { 743 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED); 744 }, 500); 745} catch (error) { 746 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 747 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 748} 749``` 750 751### ORIENTATION<sup>9+</sup> 752 753on(type: SensorId.ORIENTATION, callback: Callback<OrientationResponse>, options?: Options): void 754 755订阅方向传感器数据。 756 757**系统能力**:SystemCapability.Sensors.Sensor 758 759**错误码**: 760 761以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 762 763| 错误码ID | 错误信息 | 764| -------- | ------------------ | 765| 14500101 | Service exception. | 766 767**参数:** 768 769| 参数名 | 类型 | 必填 | 说明 | 770| -------- | ----------------------------------------------------------- | ---- | ----------------------------------------------------------- | 771| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 772| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 回调函数,异步上报的传感器数据固定为OrientationResponse。 | 773| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 774 775**示例:** 776 777```ts 778import sensor from "@ohos.sensor" 779import BusinessError from "@ohos.base" 780 781try { 782 sensor.on(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => { 783 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 784 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 785 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 786 }, { interval: 100000000 }); 787 setTimeout(() => { 788 sensor.off(sensor.SensorId.ORIENTATION); 789 }, 500); 790} catch (error) { 791 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 792 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 793} 794``` 795 796### PEDOMETER<sup>9+</sup> 797 798on(type: SensorId.PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void 799 800订阅计步器传感器数据。 801 802**需要权限**:ohos.permission.ACTIVITY_MOTION 803 804**系统能力**:SystemCapability.Sensors.Sensor 805 806**错误码**: 807 808以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 809 810| 错误码ID | 错误信息 | 811| -------- | ------------------ | 812| 14500101 | Service exception. | 813 814**参数:** 815 816| 参数名 | 类型 | 必填 | 说明 | 817| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 818| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 819| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerResponse。 | 820| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 821 822**示例:** 823 824```ts 825import sensor from "@ohos.sensor" 826import BusinessError from "@ohos.base" 827 828try { 829 sensor.on(sensor.SensorId.PEDOMETER, (data: sensor.PedometerResponse) => { 830 console.info('Succeeded in invoking on. Step count: ' + data.steps); 831 }, { interval: 100000000 }); 832 setTimeout(() => { 833 sensor.off(sensor.SensorId.PEDOMETER); 834 }, 500); 835} catch (error) { 836 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 837 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 838} 839``` 840 841### PEDOMETER_DETECTION<sup>9+</sup> 842 843on(type: SensorId.PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, 844 options?: Options): void 845 846订阅计步检测器传感器数据。 847 848**需要权限**:ohos.permission.ACTIVITY_MOTION 849 850**系统能力**:SystemCapability.Sensors.Sensor 851 852**参数:** 853 854| 参数名 | 类型 | 必填 | 说明 | 855| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 856| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 857| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerDetectionResponse。 | 858| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 859 860**错误码**: 861 862以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 863 864| 错误码ID | 错误信息 | 865| -------- | ------------------ | 866| 14500101 | Service exception. | 867 868**示例:** 869 870```ts 871import sensor from "@ohos.sensor" 872import BusinessError from "@ohos.base" 873 874try { 875 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 876 console.info('Succeeded in invoking on. Pedometer scalar: ' + data.scalar); 877 }, { interval: 100000000 }); 878 setTimeout(() => { 879 sensor.off(sensor.SensorId.PEDOMETER_DETECTION); 880 }, 500); 881} catch (error) { 882 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 883 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 884} 885``` 886 887### PROXIMITY<sup>9+</sup> 888 889on(type: SensorId.PROXIMITY, callback: Callback<ProximityResponse>, options?: Options): void 890 891订阅接近光传感器数据。 892 893**系统能力**:SystemCapability.Sensors.Sensor 894 895**参数:** 896 897| 参数名 | 类型 | 必填 | 说明 | 898| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 899| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 900| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为ProximityResponse。 | 901| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当接近光事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 902 903**错误码**: 904 905以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 906 907| 错误码ID | 错误信息 | 908| -------- | ------------------ | 909| 14500101 | Service exception. | 910 911**示例:** 912 913```ts 914import sensor from "@ohos.sensor" 915import BusinessError from "@ohos.base" 916 917try { 918 sensor.on(sensor.SensorId.PROXIMITY, (data: sensor.ProximityResponse) => { 919 console.info('Succeeded in invoking on. Distance: ' + data.distance); 920 }, { interval: 100000000 }); 921 setTimeout(() => { 922 sensor.off(sensor.SensorId.PROXIMITY); 923 }, 500); 924} catch (error) { 925 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 926 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 927} 928``` 929 930### ROTATION_VECTOR<sup>9+</sup> 931 932on(type: SensorId.ROTATION_VECTOR, callback: Callback<RotationVectorResponse>, 933 options?: Options): void 934 935订阅旋转矢量传感器数据。 936 937**系统能力**:SystemCapability.Sensors.Sensor 938 939**参数:** 940 941| 参数名 | 类型 | 必填 | 说明 | 942| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 943| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 944| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 回调函数,异步上报的传感器数据固定为RotationVectorResponse。 | 945| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 946 947**错误码**: 948 949以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 950 951| 错误码ID | 错误信息 | 952| -------- | ------------------ | 953| 14500101 | Service exception. | 954 955**示例:** 956 957```ts 958import sensor from "@ohos.sensor" 959import BusinessError from "@ohos.base" 960 961try { 962 sensor.on(sensor.SensorId.ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 963 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 964 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 965 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 966 console.info('Succeeded in invoking on. Scalar quantity: ' + data.w); 967 }, { interval: 100000000 }); 968 setTimeout(() => { 969 sensor.off(sensor.SensorId.ROTATION_VECTOR); 970 }, 500); 971} catch (error) { 972 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 973 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 974} 975``` 976 977### SIGNIFICANT_MOTION<sup>9+</sup> 978 979on(type: SensorId.SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, 980 options?: Options): void 981 982订阅大幅动作检测传感器数据。 983 984**系统能力**:SystemCapability.Sensors.Sensor 985 986**参数:** 987 988| 参数名 | 类型 | 必填 | 说明 | 989| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 990| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 991| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为SignificantMotionResponse。 | 992| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 993 994**错误码**: 995 996以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 997 998| 错误码ID | 错误信息 | 999| -------- | ------------------ | 1000| 14500101 | Service exception. | 1001 1002**示例:** 1003 1004```ts 1005import sensor from "@ohos.sensor" 1006import BusinessError from "@ohos.base" 1007 1008try { 1009 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 1010 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 1011 }, { interval: 100000000 }); 1012 setTimeout(() => { 1013 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION); 1014 }, 500); 1015} catch (error) { 1016 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1017 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 1018} 1019``` 1020 1021### WEAR_DETECTION<sup>9+</sup> 1022 1023on(type: SensorId.WEAR_DETECTION, callback: Callback<WearDetectionResponse>, 1024 options?: Options): void 1025 1026订阅佩戴检测传感器数据。 1027 1028**系统能力**:SystemCapability.Sensors.Sensor 1029 1030**参数:** 1031 1032| 参数名 | 类型 | 必填 | 说明 | 1033| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1034| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 1035| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为WearDetectionResponse。 | 1036| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 1037 1038**错误码**: 1039 1040以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1041 1042| 错误码ID | 错误信息 | 1043| -------- | ------------------ | 1044| 14500101 | Service exception. | 1045 1046**示例:** 1047 1048```ts 1049import sensor from "@ohos.sensor" 1050import BusinessError from "@ohos.base" 1051 1052try { 1053 sensor.on(sensor.SensorId.WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 1054 console.info('Succeeded in invoking on. Wear status: ' + data.value); 1055 }, { interval: 100000000 }); 1056 setTimeout(() => { 1057 sensor.off(sensor.SensorId.WEAR_DETECTION); 1058 }, 500); 1059} catch (error) { 1060 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1061 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 1062} 1063``` 1064 1065## sensor.once<sup>9+</sup> 1066 1067### ACCELEROMETER<sup>9+</sup> 1068 1069once(type: SensorId.ACCELEROMETER, callback: Callback<AccelerometerResponse>): void 1070 1071获取一次加速度传感器数据。 1072 1073**需要权限**:ohos.permission.ACCELEROMETER 1074 1075**系统能力**:SystemCapability.Sensors.Sensor 1076 1077**参数:** 1078 1079| 参数名 | 类型 | 必填 | 说明 | 1080| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1081| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 1082| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerResponse。 | 1083 1084**错误码**: 1085 1086以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1087 1088| 错误码ID | 错误信息 | 1089| -------- | ------------------ | 1090| 14500101 | Service exception. | 1091 1092**示例:** 1093 1094```ts 1095import sensor from "@ohos.sensor" 1096import BusinessError from "@ohos.base" 1097 1098try { 1099 sensor.once(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 1100 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1101 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1102 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1103 }); 1104} catch (error) { 1105 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1106 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1107} 1108``` 1109 1110### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 1111 1112once(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback: Callback<AccelerometerUncalibratedResponse>): void 1113 1114获取一次未校准加速度传感器数据。 1115 1116**需要权限**:ohos.permission.ACCELEROMETER 1117 1118**系统能力**:SystemCapability.Sensors.Sensor 1119 1120**参数:** 1121 1122| 参数名 | 类型 | 必填 | 说明 | 1123| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1124| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 1125| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerUncalibratedResponse。 | 1126 1127**错误码**: 1128 1129以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1130 1131| 错误码ID | 错误信息 | 1132| -------- | ------------------ | 1133| 14500101 | Service exception. | 1134 1135**示例:** 1136 1137```ts 1138import sensor from "@ohos.sensor" 1139import BusinessError from "@ohos.base" 1140 1141try { 1142 sensor.once(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 1143 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1144 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1145 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1146 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1147 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1148 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1149 }); 1150} catch (error) { 1151 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1152 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1153} 1154``` 1155 1156### AMBIENT_LIGHT<sup>9+</sup> 1157 1158once(type: SensorId.AMBIENT_LIGHT, callback: Callback<LightResponse>): void 1159 1160获取一次环境光传感器数据。 1161 1162**系统能力**:SystemCapability.Sensors.Sensor 1163 1164**参数:** 1165 1166| 参数名 | 类型 | 必填 | 说明 | 1167| -------- | ----------------------------------------------- | ---- | --------------------------------------------------- | 1168| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 1169| callback | Callback<[LightResponse](#lightresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LightResponse。 | 1170 1171**错误码**: 1172 1173以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1174 1175| 错误码ID | 错误信息 | 1176| -------- | ------------------ | 1177| 14500101 | Service exception. | 1178 1179**示例:** 1180 1181```ts 1182import sensor from "@ohos.sensor" 1183import BusinessError from "@ohos.base" 1184 1185try { 1186 sensor.once(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => { 1187 console.info('Succeeded in invoking once. the ambient light intensity: ' + data.intensity); 1188 }); 1189} catch (error) { 1190 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1191 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1192} 1193``` 1194 1195### AMBIENT_TEMPERATURE<sup>9+</sup> 1196 1197once(type: SensorId.AMBIENT_TEMPERATURE, callback: Callback<AmbientTemperatureResponse>): void 1198 1199获取一次温度传感器数据。 1200 1201**系统能力**:SystemCapability.Sensors.Sensor 1202 1203**参数:** 1204 1205| 参数名 | 类型 | 必填 | 说明 | 1206| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1207| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 1208| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AmbientTemperatureResponse。 | 1209 1210**错误码**: 1211 1212以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1213 1214| 错误码ID | 错误信息 | 1215| -------- | ------------------ | 1216| 14500101 | Service exception. | 1217 1218**示例:** 1219 1220```ts 1221import sensor from "@ohos.sensor" 1222import BusinessError from "@ohos.base" 1223 1224try { 1225 sensor.once(sensor.SensorId.AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 1226 console.info('Succeeded in invoking once. Temperature: ' + data.temperature); 1227 }); 1228} catch (error) { 1229 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1230 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1231} 1232``` 1233 1234### BAROMETER<sup>9+</sup> 1235 1236once(type: SensorId.BAROMETER, callback: Callback<BarometerResponse>): void 1237 1238获取一次气压计传感器数据。 1239 1240**系统能力**:SystemCapability.Sensors.Sensor 1241 1242**参数:** 1243 1244| 参数名 | 类型 | 必填 | 说明 | 1245| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1246| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 1247| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为BarometerResponse。 | 1248 1249**错误码**: 1250 1251以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1252 1253| 错误码ID | 错误信息 | 1254| -------- | ------------------ | 1255| 14500101 | Service exception. | 1256 1257**示例:** 1258 1259```ts 1260import sensor from "@ohos.sensor" 1261import BusinessError from "@ohos.base" 1262 1263try { 1264 sensor.once(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => { 1265 console.info('Succeeded in invoking once. Atmospheric pressure: ' + data.pressure); 1266 }); 1267} catch (error) { 1268 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1269 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1270} 1271``` 1272 1273### GRAVITY<sup>9+</sup> 1274 1275once(type: SensorId.GRAVITY, callback: Callback<GravityResponse>): void 1276 1277获取一次重力传感器数据。 1278 1279**系统能力**:SystemCapability.Sensors.Sensor 1280 1281**参数:** 1282 1283| 参数名 | 类型 | 必填 | 说明 | 1284| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------- | 1285| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 1286| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GravityResponse。 | 1287 1288**错误码**: 1289 1290以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1291 1292| 错误码ID | 错误信息 | 1293| -------- | ------------------ | 1294| 14500101 | Service exception. | 1295 1296**示例:** 1297 1298```ts 1299import sensor from "@ohos.sensor" 1300import BusinessError from "@ohos.base" 1301 1302try { 1303 sensor.once(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 1304 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1305 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1306 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1307 }); 1308} catch (error) { 1309 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1310 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1311} 1312``` 1313 1314### GYROSCOPE<sup>9+</sup> 1315 1316once(type: SensorId.GYROSCOPE, callback: Callback<GyroscopeResponse>): void 1317 1318获取一次陀螺仪传感器数据。 1319 1320**需要权限**:ohos.permission.GYROSCOPE 1321 1322**系统能力**:SystemCapability.Sensors.Sensor 1323 1324**参数:** 1325 1326| 参数名 | 类型 | 必填 | 说明 | 1327| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1328| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 1329| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeResponse。 | 1330 1331**错误码**: 1332 1333以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1334 1335| 错误码ID | 错误信息 | 1336| -------- | ------------------ | 1337| 14500101 | Service exception. | 1338 1339**示例:** 1340 1341```ts 1342import sensor from '@ohos.sensor'; 1343import BusinessError from "@ohos.base" 1344 1345try { 1346 sensor.once(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => { 1347 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1348 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1349 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1350 }); 1351} catch (error) { 1352 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1353 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1354} 1355``` 1356 1357### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 1358 1359once(type: SensorId.GYROSCOPE_UNCALIBRATED, callback: Callback<GyroscopeUncalibratedResponse>): void 1360 1361获取一次未校准陀螺仪传感器数据。 1362 1363**需要权限**:ohos.permission.GYROSCOPE 1364 1365**系统能力**:SystemCapability.Sensors.Sensor 1366 1367**参数:** 1368 1369| 参数名 | 类型 | 必填 | 说明 | 1370| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1371| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 1372| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeUncalibratedResponse。 | 1373 1374**错误码**: 1375 1376以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1377 1378| 错误码ID | 错误信息 | 1379| -------- | ------------------ | 1380| 14500101 | Service exception. | 1381 1382**示例:** 1383 1384```ts 1385import sensor from "@ohos.sensor" 1386import BusinessError from "@ohos.base" 1387 1388try { 1389 sensor.once(sensor.SensorId.GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 1390 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1391 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1392 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1393 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1394 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1395 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1396 }); 1397} catch (error) { 1398 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1399 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1400} 1401``` 1402 1403### HALL<sup>9+</sup> 1404 1405once(type: SensorId.HALL, callback: Callback<HallResponse>): void 1406 1407获取一次霍尔传感器数据。 1408 1409**系统能力**:SystemCapability.Sensors.Sensor 1410 1411**参数:** 1412 1413| 参数名 | 类型 | 必填 | 说明 | 1414| -------- | --------------------------------------------- | ---- | -------------------------------------------------- | 1415| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 1416| callback | Callback<[HallResponse](#hallresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HallResponse。 | 1417 1418**错误码**: 1419 1420以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1421 1422| 错误码ID | 错误信息 | 1423| -------- | ------------------ | 1424| 14500101 | Service exception. | 1425 1426**示例:** 1427 1428```ts 1429import sensor from "@ohos.sensor" 1430import BusinessError from "@ohos.base" 1431 1432try { 1433 sensor.once(sensor.SensorId.HALL, (data: sensor.HallResponse) => { 1434 console.info('Succeeded in invoking once. Status: ' + data.status); 1435 }); 1436} catch (error) { 1437 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1438 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1439} 1440``` 1441 1442### HEART_RATE<sup>9+</sup> 1443 1444once(type: SensorId.HEART_RATE, callback: Callback<HeartRateResponse>): void 1445 1446获取一次心率传感器数据。 1447 1448**需要权限**:ohos.permission.READ_HEALTH_DATA 1449 1450**系统能力**:SystemCapability.Sensors.Sensor 1451 1452**参数:** 1453 1454| 参数名 | 类型 | 必填 | 说明 | 1455| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1456| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 1457| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HeartRateResponse。 | 1458 1459**错误码**: 1460 1461以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1462 1463| 错误码ID | 错误信息 | 1464| -------- | ------------------ | 1465| 14500101 | Service exception. | 1466 1467**示例:** 1468 1469```ts 1470import sensor from "@ohos.sensor" 1471import BusinessError from "@ohos.base" 1472 1473try { 1474 sensor.once(sensor.SensorId.HEART_RATE, (data: sensor.HeartRateResponse) => { 1475 console.info('Succeeded in invoking once. Heart rate: ' + data.heartRate); 1476 }); 1477} catch (error) { 1478 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1479 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1480} 1481``` 1482 1483### HUMIDITY<sup>9+</sup> 1484 1485once(type: SensorId.HUMIDITY, callback: Callback<HumidityResponse>): void 1486 1487获取一次湿度传感器数据。 1488 1489**系统能力**:SystemCapability.Sensors.Sensor 1490 1491**参数:** 1492 1493| 参数名 | 类型 | 必填 | 说明 | 1494| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------ | 1495| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 1496| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HumidityResponse。 | 1497 1498**错误码**: 1499 1500以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1501 1502| 错误码ID | 错误信息 | 1503| -------- | ------------------ | 1504| 14500101 | Service exception. | 1505 1506**示例:** 1507 1508```ts 1509import sensor from "@ohos.sensor" 1510import BusinessError from "@ohos.base" 1511 1512try { 1513 sensor.once(sensor.SensorId.HUMIDITY, (data: sensor.HumidityResponse) => { 1514 console.info('Succeeded in invoking once. Humidity: ' + data.humidity); 1515 }); 1516} catch (error) { 1517 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1518 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1519} 1520``` 1521 1522### LINEAR_ACCELEROMETER<sup>9+</sup> 1523 1524once(type: SensorId.LINEAR_ACCELEROMETER, callback: Callback<LinearAccelerometerResponse>): void 1525 1526获取一次线性加速度传感器数据。 1527 1528**需要权限**:ohos.permission.ACCELEROMETER 1529 1530**系统能力**:SystemCapability.Sensors.Sensor 1531 1532**参数:** 1533 1534| 参数名 | 类型 | 必填 | 说明 | 1535| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1536| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELEROMETER。 | 1537| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LinearAccelerometerResponse。 | 1538 1539**错误码**: 1540 1541以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1542 1543| 错误码ID | 错误信息 | 1544| -------- | ------------------ | 1545| 14500101 | Service exception. | 1546 1547**示例:** 1548 1549```ts 1550import sensor from "@ohos.sensor" 1551import BusinessError from "@ohos.base" 1552 1553try { 1554 sensor.once(sensor.SensorId.LINEAR_ACCELEROMETER, (data: sensor.LinearAccelerometerResponse) => { 1555 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1556 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1557 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1558 }); 1559} catch (error) { 1560 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1561 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1562} 1563``` 1564 1565### MAGNETIC_FIELD<sup>9+</sup> 1566 1567once(type: SensorId.MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>): void 1568 1569获取一次磁场传感器数据。 1570 1571**系统能力**:SystemCapability.Sensors.Sensor 1572 1573**参数:** 1574 1575| 参数名 | 类型 | 必填 | 说明 | 1576| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1577| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 1578| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldResponse。 | 1579 1580**错误码**: 1581 1582以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1583 1584| 错误码ID | 错误信息 | 1585| -------- | ------------------ | 1586| 14500101 | Service exception. | 1587 1588**示例:** 1589 1590```ts 1591import sensor from "@ohos.sensor" 1592import BusinessError from "@ohos.base" 1593 1594try { 1595 sensor.once(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 1596 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1597 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1598 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1599 }); 1600} catch (error) { 1601 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1602 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1603} 1604``` 1605 1606### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 1607 1608once(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback: Callback<MagneticFieldUncalibratedResponse>): void 1609 1610获取一次未经校准的磁场传感器数据。 1611 1612**系统能力**:SystemCapability.Sensors.Sensor 1613 1614**参数:** 1615 1616| 参数名 | 类型 | 必填 | 说明 | 1617| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1618| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 1619| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldUncalibratedResponse。 | 1620 1621**错误码**: 1622 1623以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1624 1625| 错误码ID | 错误信息 | 1626| -------- | ------------------ | 1627| 14500101 | Service exception. | 1628 1629**示例:** 1630 1631```ts 1632import sensor from "@ohos.sensor" 1633import BusinessError from "@ohos.base" 1634 1635try { 1636 sensor.once(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 1637 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1638 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1639 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1640 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1641 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1642 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1643 }); 1644} catch (error) { 1645 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1646 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1647} 1648``` 1649 1650### ORIENTATION<sup>9+</sup> 1651 1652once(type: SensorId.ORIENTATION, callback: Callback<OrientationResponse>): void 1653 1654获取一次方向传感器数据。 1655 1656**系统能力**:SystemCapability.Sensors.Sensor 1657 1658**参数:** 1659 1660| 参数名 | 类型 | 必填 | 说明 | 1661| -------- | ----------------------------------------------------------- | ---- | --------------------------------------------------------- | 1662| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 1663| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 回调函数,异步上报的传感器数据固定为OrientationResponse。 | 1664 1665**错误码**: 1666 1667以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1668 1669| 错误码ID | 错误信息 | 1670| -------- | ------------------ | 1671| 14500101 | Service exception. | 1672 1673**示例:** 1674 1675```ts 1676import sensor from "@ohos.sensor" 1677import BusinessError from "@ohos.base" 1678 1679try { 1680 sensor.once(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => { 1681 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 1682 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 1683 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 1684 }); 1685} catch (error) { 1686 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1687 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1688} 1689``` 1690 1691### PEDOMETER<sup>9+</sup> 1692 1693once(type: SensorId.PEDOMETER, callback: Callback<PedometerResponse>): void 1694 1695获取一次计步器传感器数据。 1696 1697**需要权限**:ohos.permission.ACTIVITY_MOTION 1698 1699**系统能力**:SystemCapability.Sensors.Sensor 1700 1701**参数:** 1702 1703| 参数名 | 类型 | 必填 | 说明 | 1704| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1705| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 1706| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerResponse。 | 1707 1708**错误码**: 1709 1710以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1711 1712| 错误码ID | 错误信息 | 1713| -------- | ------------------ | 1714| 14500101 | Service exception. | 1715 1716**示例:** 1717 1718```ts 1719import sensor from "@ohos.sensor" 1720import BusinessError from "@ohos.base" 1721 1722try { 1723 sensor.once(sensor.SensorId.PEDOMETER, (data: sensor.PedometerResponse) => { 1724 console.info('Succeeded in invoking once. Step count: ' + data.steps); 1725 }); 1726} catch (error) { 1727 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1728 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1729} 1730``` 1731 1732### PEDOMETER_DETECTION<sup>9+</sup> 1733 1734once(type: SensorId.PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>): void 1735 1736获取一次计步检测器传感器数据。 1737 1738**需要权限**:ohos.permission.ACTIVITY_MOTION 1739 1740**系统能力**:SystemCapability.Sensors.Sensor 1741 1742**参数:** 1743 1744| 参数名 | 类型 | 必填 | 说明 | 1745| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1746| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 1747| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerDetectionResponse。 | 1748 1749**错误码**: 1750 1751以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1752 1753| 错误码ID | 错误信息 | 1754| -------- | ------------------ | 1755| 14500101 | Service exception. | 1756 1757**示例:** 1758 1759```ts 1760import sensor from "@ohos.sensor" 1761import BusinessError from "@ohos.base" 1762 1763try { 1764 sensor.once(sensor.SensorId.PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 1765 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 1766 }); 1767} catch (error) { 1768 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1769 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1770} 1771``` 1772 1773### PROXIMITY<sup>9+</sup> 1774 1775once(type: SensorId.PROXIMITY, callback: Callback<ProximityResponse>): void 1776 1777获取一次接近光传感器数据。 1778 1779**系统能力**:SystemCapability.Sensors.Sensor 1780 1781**参数:** 1782 1783| 参数名 | 类型 | 必填 | 说明 | 1784| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1785| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 1786| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为ProximityResponse。 | 1787 1788**错误码**: 1789 1790以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1791 1792| 错误码ID | 错误信息 | 1793| -------- | ------------------ | 1794| 14500101 | Service exception. | 1795 1796**示例:** 1797 1798```ts 1799import sensor from "@ohos.sensor" 1800import BusinessError from "@ohos.base" 1801 1802try { 1803 sensor.once(sensor.SensorId.PROXIMITY, (data: sensor.ProximityResponse) => { 1804 console.info('Succeeded in invoking once. Distance: ' + data.distance); 1805 }); 1806} catch (error) { 1807 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1808 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1809} 1810``` 1811 1812### ROTATION_VECTOR<sup>9+</sup> 1813 1814once(type: SensorId.ROTATION_VECTOR, callback: Callback<RotationVectorResponse>): void 1815 1816获取一次旋转矢量传感器数据。 1817 1818**系统能力**:SystemCapability.Sensors.Sensor 1819 1820**参数:** 1821 1822| 参数名 | 类型 | 必填 | 说明 | 1823| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1824| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 1825| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 回调函数,异步上报的传感器数据固定为RotationVectorResponse。 | 1826 1827**错误码**: 1828 1829以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1830 1831| 错误码ID | 错误信息 | 1832| -------- | ------------------ | 1833| 14500101 | Service exception. | 1834 1835**示例:** 1836 1837```ts 1838import sensor from "@ohos.sensor" 1839import BusinessError from "@ohos.base" 1840 1841try { 1842 sensor.once(sensor.SensorId.ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 1843 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1844 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1845 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1846 console.info('Succeeded in invoking once. Scalar quantity: ' + data.w); 1847 }); 1848} catch (error) { 1849 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1850 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1851} 1852``` 1853 1854### SIGNIFICANT_MOTION<sup>9+</sup> 1855 1856once(type: SensorId.SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>): void 1857 1858获取一次大幅动作检测传感器数据。 1859 1860**系统能力**:SystemCapability.Sensors.Sensor 1861 1862**参数:** 1863 1864| 参数名 | 类型 | 必填 | 说明 | 1865| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1866| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 1867| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为SignificantMotionResponse。 | 1868 1869**错误码**: 1870 1871以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1872 1873| 错误码ID | 错误信息 | 1874| -------- | ------------------ | 1875| 14500101 | Service exception. | 1876 1877**示例:** 1878 1879```ts 1880import sensor from "@ohos.sensor" 1881import BusinessError from "@ohos.base" 1882 1883try { 1884 sensor.once(sensor.SensorId.SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 1885 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 1886 }); 1887} catch (error) { 1888 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1889 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1890} 1891``` 1892 1893### WEAR_DETECTION<sup>9+</sup> 1894 1895once(type: SensorId.WEAR_DETECTION, callback: Callback<WearDetectionResponse>): void 1896 1897获取一次佩戴检测传感器数据。 1898 1899**系统能力**:SystemCapability.Sensors.Sensor 1900 1901**参数:** 1902 1903| 参数名 | 类型 | 必填 | 说明 | 1904| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1905| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 1906| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为WearDetectionResponse。 | 1907 1908**错误码**: 1909 1910以下错误码的详细介绍请参见 [ohos.sensor(传感器)错误码](../errorcodes/errorcode-sensor.md)。 1911 1912| 错误码ID | 错误信息 | 1913| -------- | ------------------ | 1914| 14500101 | Service exception. | 1915 1916**示例:** 1917 1918```ts 1919import sensor from "@ohos.sensor" 1920import BusinessError from "@ohos.base" 1921 1922try { 1923 sensor.once(sensor.SensorId.WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 1924 console.info('Succeeded in invoking once. Wear status: ' + data.value); 1925 }); 1926} catch (error) { 1927 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1928 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1929} 1930``` 1931 1932## sensor.off 1933 1934### COLOR<sup>10+</sup> 1935 1936off(type: SensorId.COLOR, callback?: Callback\<ColorResponse>): void 1937 1938取消订阅颜色传感器数据。 1939 1940**系统能力**:SystemCapability.Sensors.Sensor 1941 1942**系统API**:此接口为系统接口 1943 1944**参数:** 1945 1946| 参数名 | 类型 | 必填 | 说明 | 1947| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1948| type | [SensorId](#sensorid9).COLOR | 是 | 传感器类型,该值固定为SensorId.COLOR。 | 1949| callback | Callback<[ColorResponse](#colorresponse10)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 1950 1951**示例:** 1952 1953```ts 1954import sensor from "@ohos.sensor" 1955import BusinessError from "@ohos.base" 1956 1957function callback1(data: object) { 1958 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 1959} 1960 1961function callback2(data: object) { 1962 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 1963} 1964 1965try { 1966 sensor.on(sensor.SensorId.COLOR, callback1); 1967 sensor.on(sensor.SensorId.COLOR, callback2); 1968 // 仅取消callback1的注册 1969 sensor.off(sensor.SensorId.COLOR, callback1); 1970 // 取消注册SensorId.COLOR的所有回调 1971 sensor.off(sensor.SensorId.COLOR); 1972} catch (error) { 1973 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 1974 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 1975} 1976``` 1977 1978### SAR<sup>10+</sup> 1979 1980off(type: SensorId.SAR, callback?: Callback\<SarResponse>): void 1981 1982取消订阅吸收比率传感器数据。 1983 1984**系统能力**:SystemCapability.Sensors.Sensor 1985 1986**系统API**:此接口为系统接口 1987 1988**参数:** 1989 1990| 参数名 | 类型 | 必填 | 说明 | 1991| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 1992| type | [SensorId](#sensorid9).SAR | 是 | 传感器类型,该值固定为SensorId.SAR。 | 1993| callback | Callback<[SarResponse](#sarresponse10)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 1994 1995**示例:** 1996 1997```ts 1998import sensor from "@ohos.sensor" 1999import BusinessError from "@ohos.base" 2000 2001function callback1(data: object) { 2002 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2003} 2004 2005function callback2(data: object) { 2006 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2007} 2008 2009try { 2010 sensor.on(sensor.SensorId.SAR, callback1); 2011 sensor.on(sensor.SensorId.SAR, callback2); 2012 // 仅取消callback1的注册 2013 sensor.off(sensor.SensorId.SAR, callback1); 2014 // 取消注册SensorId.SAR的所有回调 2015 sensor.off(sensor.SensorId.SAR); 2016} catch (error) { 2017 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2018 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2019} 2020``` 2021 2022### ACCELEROMETER<sup>9+</sup> 2023 2024off(type: SensorId.ACCELEROMETER, callback?: Callback<AccelerometerResponse>): void 2025 2026取消订阅加速度传感器数据。 2027 2028**需要权限**:ohos.permission.ACCELEROMETER 2029 2030**系统能力**:SystemCapability.Sensors.Sensor 2031 2032**参数:** 2033 2034| 参数名 | 类型 | 必填 | 说明 | 2035| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2036| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 2037| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2038 2039**示例:** 2040 2041```ts 2042import sensor from "@ohos.sensor" 2043import BusinessError from "@ohos.base" 2044 2045function callback1(data: object) { 2046 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2047} 2048 2049function callback2(data: object) { 2050 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2051} 2052 2053try { 2054 sensor.on(sensor.SensorId.ACCELEROMETER, callback1); 2055 sensor.on(sensor.SensorId.ACCELEROMETER, callback2); 2056 // 仅取消callback1的注册 2057 sensor.off(sensor.SensorId.ACCELEROMETER, callback1); 2058 // 取消SensorId.ACCELEROMETER类型的所有回调 2059 sensor.off(sensor.SensorId.ACCELEROMETER); 2060} catch (error) { 2061 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2062 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2063} 2064``` 2065 2066### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 2067 2068off(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback?: Callback<AccelerometerUncalibratedResponse>): void 2069 2070取消订阅未校准加速度传感器数据。 2071 2072**需要权限**:ohos.permission.ACCELEROMETER 2073 2074**系统能力**:SystemCapability.Sensors.Sensor 2075 2076**参数:** 2077 2078| 参数名 | 类型 | 必填 | 说明 | 2079| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2080| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 2081| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2082 2083**示例:** 2084 2085```ts 2086import sensor from "@ohos.sensor" 2087import BusinessError from "@ohos.base" 2088 2089function callback1(data: object) { 2090 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2091} 2092 2093function callback2(data: object) { 2094 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2095} 2096 2097try { 2098 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback1); 2099 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback2); 2100 // 仅取消callback1的注册 2101 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback1); 2102 // 取消注册SensorId.ACCELEROMETER_UNCALIBRATED类型的所有回调 2103 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED); 2104} catch (error) { 2105 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2106 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2107} 2108``` 2109 2110### AMBIENT_LIGHT<sup>9+</sup> 2111 2112off(type: SensorId.AMBIENT_LIGHT, callback?: Callback<LightResponse>): void 2113 2114取消订阅环境光传感器数据。 2115 2116**系统能力**:SystemCapability.Sensors.Sensor 2117 2118**参数:** 2119 2120| 参数名 | 类型 | 必填 | 说明 | 2121| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 2122| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 2123| callback | Callback<[LightResponse](#lightresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2124 2125**示例:** 2126 2127```ts 2128import sensor from "@ohos.sensor" 2129import BusinessError from "@ohos.base" 2130 2131function callback1(data: object) { 2132 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2133} 2134 2135function callback2(data: object) { 2136 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2137} 2138 2139try { 2140 sensor.on(sensor.SensorId.AMBIENT_LIGHT, callback1); 2141 sensor.on(sensor.SensorId.AMBIENT_LIGHT, callback2); 2142 // 仅取消callback1的注册 2143 sensor.off(sensor.SensorId.AMBIENT_LIGHT, callback1); 2144 // 取消注册SensorId.AMBIENT_LIGHT 2145 sensor.off(sensor.SensorId.AMBIENT_LIGHT); 2146} catch (error) { 2147 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2148 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2149} 2150``` 2151 2152### AMBIENT_TEMPERATURE<sup>9+</sup> 2153 2154off(type: SensorId.AMBIENT_TEMPERATURE, callback?: Callback<AmbientTemperatureResponse>): void 2155 2156取消订阅温度传感器数据。 2157 2158**系统能力**:SystemCapability.Sensors.Sensor 2159 2160**参数:** 2161 2162| 参数名 | 类型 | 必填 | 说明 | 2163| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2164| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 2165| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2166 2167**示例:** 2168 2169```ts 2170import sensor from "@ohos.sensor" 2171import BusinessError from "@ohos.base" 2172 2173function callback1(data: object) { 2174 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2175} 2176 2177function callback2(data: object) { 2178 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2179} 2180 2181try { 2182 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, callback1); 2183 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, callback2); 2184 // 仅取消callback1的注册 2185 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE, callback1); 2186 // 取消注册SensorId.AMBIENT_TEMPERATURE的所有回调 2187 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE); 2188} catch (error) { 2189 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2190 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2191} 2192``` 2193 2194### BAROMETER<sup>9+</sup> 2195 2196off(type: SensorId.BAROMETER, callback?: Callback<BarometerResponse>): void 2197 2198取消订阅气压计传感器数据。 2199 2200**系统能力**:SystemCapability.Sensors.Sensor 2201 2202**参数:** 2203 2204| 参数名 | 类型 | 必填 | 说明 | 2205| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2206| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 2207| callback | Callback<[BarometerResponse](#barometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2208 2209**示例:** 2210 2211```ts 2212import sensor from "@ohos.sensor" 2213import BusinessError from "@ohos.base" 2214 2215function callback1(data: object) { 2216 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2217} 2218 2219function callback2(data: object) { 2220 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2221} 2222 2223try { 2224 sensor.on(sensor.SensorId.BAROMETER, callback1); 2225 sensor.on(sensor.SensorId.BAROMETER, callback2); 2226 // 仅取消callback1的注册 2227 sensor.off(sensor.SensorId.BAROMETER, callback1); 2228 // 取消注册SensorId.BAROMETER的所有回调 2229 sensor.off(sensor.SensorId.BAROMETER); 2230} catch (error) { 2231 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2232 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2233} 2234``` 2235 2236### GRAVITY<sup>9+</sup> 2237 2238off(type: SensorId.GRAVITY, callback?: Callback<GravityResponse>): void 2239 2240取消订阅重力传感器数据。 2241 2242**系统能力**:SystemCapability.Sensors.Sensor 2243 2244**参数:** 2245 2246| 参数名 | 类型 | 必填 | 说明 | 2247| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 2248| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 2249| callback | Callback<[GravityResponse](#gravityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2250 2251**示例:** 2252 2253```ts 2254import sensor from "@ohos.sensor" 2255import BusinessError from "@ohos.base" 2256 2257function callback1(data: object) { 2258 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2259} 2260 2261function callback2(data: object) { 2262 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2263} 2264 2265try { 2266 sensor.on(sensor.SensorId.GRAVITY, callback1); 2267 sensor.on(sensor.SensorId.GRAVITY, callback2); 2268 // 仅取消callback1的注册 2269 sensor.off(sensor.SensorId.GRAVITY, callback1); 2270 // 取消注册SensorId.GRAVITY的所有回调 2271 sensor.off(sensor.SensorId.GRAVITY); 2272} catch (error) { 2273 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2274 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2275} 2276 2277``` 2278 2279### GYROSCOPE<sup>9+</sup> 2280 2281off(type: SensorId.GYROSCOPE, callback?: Callback<GyroscopeResponse>): void 2282 2283取消订阅陀螺仪传感器数据。 2284 2285**需要权限**:ohos.permission.GYROSCOPE 2286 2287**系统能力**:SystemCapability.Sensors.Sensor 2288 2289**参数:** 2290 2291| 参数名 | 类型 | 必填 | 说明 | 2292| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2293| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 2294| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2295 2296**示例:** 2297 2298```ts 2299import sensor from "@ohos.sensor" 2300import BusinessError from "@ohos.base" 2301 2302function callback1(data: object) { 2303 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2304} 2305 2306function callback2(data: object) { 2307 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2308} 2309 2310try { 2311 sensor.on(sensor.SensorId.GYROSCOPE, callback1); 2312 sensor.on(sensor.SensorId.GYROSCOPE, callback2); 2313 // 仅取消callback1的注册 2314 sensor.off(sensor.SensorId.GYROSCOPE, callback1); 2315 // 取消注册SensorId.GYROSCOPE的所有回调 2316 sensor.off(sensor.SensorId.GYROSCOPE); 2317} catch (error) { 2318 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2319 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2320} 2321``` 2322 2323### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 2324 2325off(type: SensorId.GYROSCOPE_UNCALIBRATED, callback?: Callback<GyroscopeUncalibratedResponse>): void 2326 2327 取消订阅未校准陀螺仪传感器数据。 2328 2329**需要权限**:ohos.permission.GYROSCOPE 2330 2331**系统能力**:SystemCapability.Sensors.Sensor 2332 2333**参数:** 2334 2335| 参数名 | 类型 | 必填 | 说明 | 2336| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2337| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 2338| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2339 2340**示例:** 2341 2342```ts 2343import sensor from "@ohos.sensor" 2344import BusinessError from "@ohos.base" 2345 2346function callback1(data: object) { 2347 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2348} 2349 2350function callback2(data: object) { 2351 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2352} 2353 2354try { 2355 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback1); 2356 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback2); 2357 // 仅取消callback1的注册 2358 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback1); 2359 // 取消注册SensorId.GYROSCOPE_UNCALIBRATED的所有回调 2360 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED); 2361} catch (error) { 2362 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2363 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2364} 2365``` 2366 2367### HALL<sup>9+</sup> 2368 2369off(type: SensorId.HALL, callback?: Callback<HallResponse>): void 2370 2371取消订阅霍尔传感器数据。 2372 2373**系统能力**:SystemCapability.Sensors.Sensor 2374 2375**参数:** 2376 2377| 参数名 | 类型 | 必填 | 说明 | 2378| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 2379| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 2380| callback | Callback<[HallResponse](#hallresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2381 2382**示例:** 2383 2384```ts 2385import sensor from "@ohos.sensor" 2386import BusinessError from "@ohos.base" 2387 2388function callback1(data: object) { 2389 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2390} 2391 2392function callback2(data: object) { 2393 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2394} 2395 2396try { 2397 sensor.on(sensor.SensorId.HALL, callback1); 2398 sensor.on(sensor.SensorId.HALL, callback2); 2399 // 仅取消callback1的注册 2400 sensor.off(sensor.SensorId.HALL, callback1); 2401 // 取消注册SensorId.HALL的所有回调 2402 sensor.off(sensor.SensorId.HALL); 2403} catch (error) { 2404 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2405 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2406} 2407``` 2408 2409### HEART_RATE<sup>9+</sup> 2410 2411off(type: SensorId.HEART_RATE, callback?: Callback<HeartRateResponse>): void 2412 2413取消订阅心率传感器数据。 2414 2415**需要权限**:ohos.permission.READ_HEALTH_DATA 2416 2417**系统能力**:SystemCapability.Sensors.Sensor 2418 2419**参数:** 2420 2421| 参数名 | 类型 | 必填 | 说明 | 2422| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2423| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 2424| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2425 2426**示例:** 2427 2428```ts 2429import sensor from "@ohos.sensor" 2430import BusinessError from "@ohos.base" 2431 2432function callback1(data: object) { 2433 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2434} 2435 2436function callback2(data: object) { 2437 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2438} 2439 2440try { 2441 sensor.on(sensor.SensorId.HEART_RATE, callback1); 2442 sensor.on(sensor.SensorId.HEART_RATE, callback2); 2443 // 仅取消callback1的注册 2444 sensor.off(sensor.SensorId.HEART_RATE, callback1); 2445 // 取消注册SensorId.HEART_RATE的所有回调 2446 sensor.off(sensor.SensorId.HEART_RATE); 2447} catch (error) { 2448 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2449 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2450} 2451``` 2452 2453### HUMIDITY<sup>9+</sup> 2454 2455off(type: SensorId.HUMIDITY, callback?: Callback<HumidityResponse>): void 2456 2457取消订阅湿度传感器数据。 2458 2459**系统能力**:SystemCapability.Sensors.Sensor 2460 2461**参数:** 2462 2463| 参数名 | 类型 | 必填 | 说明 | 2464| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 2465| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 2466| callback | Callback<[HumidityResponse](#humidityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2467 2468**示例:** 2469 2470```ts 2471import sensor from "@ohos.sensor" 2472import BusinessError from "@ohos.base" 2473 2474function callback1(data: object) { 2475 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2476} 2477 2478function callback2(data: object) { 2479 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2480} 2481 2482try { 2483 sensor.on(sensor.SensorId.HUMIDITY, callback1); 2484 sensor.on(sensor.SensorId.HUMIDITY, callback2); 2485 // 仅取消callback1的注册 2486 sensor.off(sensor.SensorId.HUMIDITY, callback1); 2487 // 取消注册SensorId.HUMIDITY的所有回调 2488 sensor.off(sensor.SensorId.HUMIDITY); 2489} catch (error) { 2490 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2491 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2492} 2493``` 2494 2495### LINEAR_ACCELEROMETER<sup>9+</sup> 2496 2497off(type: SensorId.LINEAR_ACCELEROMETER, callback?: Callback<LinearAccelerometerResponse>): void 2498 2499取消订阅线性加速度传感器数据。 2500 2501**需要权限**:ohos.permission.ACCELEROMETER 2502 2503**系统能力**:SystemCapability.Sensors.Sensor 2504 2505**参数:** 2506 2507| 参数名 | 类型 | 必填 | 说明 | 2508| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2509| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELERATION。 | 2510| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2511 2512**示例:** 2513 2514```ts 2515import sensor from "@ohos.sensor" 2516import BusinessError from "@ohos.base" 2517 2518function callback1(data: object) { 2519 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2520} 2521 2522function callback2(data: object) { 2523 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2524} 2525 2526try { 2527 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, callback1); 2528 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, callback2); 2529 // 仅取消callback1的注册 2530 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER, callback1); 2531 // 取消注册SensorId.LINEAR_ACCELEROMETER的所有回调 2532 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER); 2533} catch (error) { 2534 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2535 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2536} 2537``` 2538 2539### MAGNETIC_FIELD<sup>9+</sup> 2540 2541off(type: SensorId.MAGNETIC_FIELD, callback?: Callback<MagneticFieldResponse>): void 2542 2543取消订阅磁场传感器数据。 2544 2545**系统能力**:SystemCapability.Sensors.Sensor 2546 2547**参数:** 2548 2549| 参数名 | 类型 | 必填 | 说明 | 2550| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2551| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 2552| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2553 2554**示例:** 2555 2556```ts 2557import sensor from "@ohos.sensor" 2558import BusinessError from "@ohos.base" 2559 2560function callback1(data: object) { 2561 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2562} 2563 2564function callback2(data: object) { 2565 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2566} 2567 2568try { 2569 sensor.on(sensor.SensorId.MAGNETIC_FIELD, callback1); 2570 sensor.on(sensor.SensorId.MAGNETIC_FIELD, callback2); 2571 // 仅取消callback1的注册 2572 sensor.off(sensor.SensorId.MAGNETIC_FIELD, callback1); 2573 // 取消注册SensorId.MAGNETIC_FIELD的所有回调 2574 sensor.off(sensor.SensorId.MAGNETIC_FIELD); 2575} catch (error) { 2576 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2577 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2578} 2579``` 2580 2581### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 2582 2583off(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback?: Callback<MagneticFieldUncalibratedResponse>): void 2584 2585取消订阅未校准的磁场传感器数据。 2586 2587**系统能力**:SystemCapability.Sensors.Sensor 2588 2589**参数:** 2590 2591| 参数名 | 类型 | 必填 | 说明 | 2592| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2593| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 2594| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2595 2596**示例:** 2597 2598```ts 2599import sensor from "@ohos.sensor" 2600import BusinessError from "@ohos.base" 2601 2602function callback1(data: object) { 2603 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2604} 2605 2606function callback2(data: object) { 2607 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2608} 2609 2610try { 2611 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback1); 2612 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback2); 2613 // 仅取消callback1的注册 2614 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback1); 2615 // 取消注册SensorId.MAGNETIC_FIELD_UNCALIBRATED的所有回调 2616 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED); 2617} catch (error) { 2618 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2619 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2620} 2621``` 2622 2623### ORIENTATION<sup>9+</sup> 2624 2625off(type: SensorId.ORIENTATION, callback?: Callback<OrientationResponse>): void 2626 2627取消订阅方向传感器数据。 2628 2629**系统能力**:SystemCapability.Sensors.Sensor 2630 2631**参数:** 2632 2633| 参数名 | 类型 | 必填 | 说明 | 2634| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2635| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 2636| callback | Callback<[OrientationResponse](#orientationresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2637 2638**示例:** 2639 2640```ts 2641import sensor from "@ohos.sensor" 2642import BusinessError from "@ohos.base" 2643 2644function callback1(data: object) { 2645 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2646} 2647 2648function callback2(data: object) { 2649 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2650} 2651 2652try { 2653 sensor.on(sensor.SensorId.ORIENTATION, callback1); 2654 sensor.on(sensor.SensorId.ORIENTATION, callback2); 2655 // 仅取消callback1的注册 2656 sensor.off(sensor.SensorId.ORIENTATION, callback1); 2657 // 取消注册SensorId.ORIENTATION的所有回调 2658 sensor.off(sensor.SensorId.ORIENTATION); 2659} catch (error) { 2660 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2661 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2662} 2663``` 2664 2665### PEDOMETER<sup>9+</sup> 2666 2667off(type: SensorId.PEDOMETER, callback?: Callback<PedometerResponse>): void 2668 2669取消订阅计步器传感器数据。 2670 2671**需要权限**:ohos.permission.ACTIVITY_MOTION 2672 2673**系统能力**:SystemCapability.Sensors.Sensor 2674 2675**参数:** 2676 2677| 参数名 | 类型 | 必填 | 说明 | 2678| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2679| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 2680| callback | Callback<[PedometerResponse](#pedometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2681 2682**示例:** 2683 2684```ts 2685import sensor from "@ohos.sensor" 2686import BusinessError from "@ohos.base" 2687 2688function callback1(data: object) { 2689 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2690} 2691 2692function callback2(data: object) { 2693 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2694} 2695 2696try { 2697 sensor.on(sensor.SensorId.PEDOMETER, callback1); 2698 sensor.on(sensor.SensorId.PEDOMETER, callback2); 2699 // 仅取消callback1的注册 2700 sensor.off(sensor.SensorId.PEDOMETER, callback1); 2701 // 取消注册SensorId.ORIENTATION的所有回调 2702 sensor.off(sensor.SensorId.PEDOMETER); 2703} catch (error) { 2704 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2705 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2706} 2707``` 2708 2709### PEDOMETER_DETECTION<sup>9+</sup> 2710 2711off(type: SensorId.PEDOMETER_DETECTION, callback?: Callback<PedometerDetectionResponse>): void 2712 2713取消订阅计步检测器传感器数据。 2714 2715**需要权限**:ohos.permission.ACTIVITY_MOTION 2716 2717**系统能力**:SystemCapability.Sensors.Sensor 2718 2719**参数:** 2720 2721| 参数名 | 类型 | 必填 | 说明 | 2722| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2723| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 2724| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2725 2726**示例:** 2727 2728```ts 2729import sensor from "@ohos.sensor" 2730import BusinessError from "@ohos.base" 2731 2732function callback1(data: object) { 2733 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2734} 2735 2736function callback2(data: object) { 2737 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2738} 2739 2740try { 2741 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, callback1); 2742 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, callback2); 2743 // 仅取消callback1的注册 2744 sensor.off(sensor.SensorId.PEDOMETER_DETECTION, callback1); 2745 // 取消注册SensorId.PEDOMETER_DETECTION的所有回调 2746 sensor.off(sensor.SensorId.PEDOMETER_DETECTION); 2747} catch (error) { 2748 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2749 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2750} 2751``` 2752 2753### PROXIMITY<sup>9+</sup> 2754 2755off(type: SensorId.PROXIMITY, callback?: Callback<ProximityResponse>): void 2756 2757取消订阅接近光传感器数据。 2758 2759**系统能力**:SystemCapability.Sensors.Sensor 2760 2761**参数:** 2762 2763| 参数名 | 类型 | 必填 | 说明 | 2764| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2765| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 2766| callback | Callback<[ProximityResponse](#proximityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2767 2768**示例:** 2769 2770```ts 2771import sensor from "@ohos.sensor" 2772import BusinessError from "@ohos.base" 2773 2774function callback1(data: object) { 2775 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2776} 2777 2778function callback2(data: object) { 2779 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2780} 2781 2782try { 2783 sensor.on(sensor.SensorId.PROXIMITY, callback1); 2784 sensor.on(sensor.SensorId.PROXIMITY, callback2); 2785 // 仅取消callback1的注册 2786 sensor.off(sensor.SensorId.PROXIMITY, callback1); 2787 // 取消注册SensorId.PROXIMITY的所有回调 2788 sensor.off(sensor.SensorId.PROXIMITY); 2789} catch (error) { 2790 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2791 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2792} 2793``` 2794 2795### ROTATION_VECTOR<sup>9+</sup> 2796 2797off(type: SensorId.ROTATION_VECTOR, callback?: Callback<RotationVectorResponse>): void 2798 2799取消订阅旋转矢量传感器数据。 2800 2801**系统能力**:SystemCapability.Sensors.Sensor 2802 2803**参数:** 2804 2805| 参数名 | 类型 | 必填 | 说明 | 2806| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2807| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 2808| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2809 2810**示例:** 2811 2812```ts 2813import sensor from "@ohos.sensor" 2814import BusinessError from "@ohos.base" 2815 2816function callback1(data: object) { 2817 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2818} 2819 2820function callback2(data: object) { 2821 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2822} 2823 2824try { 2825 sensor.on(sensor.SensorId.ROTATION_VECTOR, callback1); 2826 sensor.on(sensor.SensorId.ROTATION_VECTOR, callback2); 2827 // 仅取消callback1的注册 2828 sensor.off(sensor.SensorId.ROTATION_VECTOR, callback1); 2829 // 取消注册SensorId.ROTATION_VECTOR的所有回调 2830 sensor.off(sensor.SensorId.ROTATION_VECTOR); 2831} catch (error) { 2832 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2833 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2834} 2835``` 2836 2837### SIGNIFICANT_MOTION<sup>9+</sup> 2838 2839off(type: SensorId.SIGNIFICANT_MOTION, callback?: Callback<SignificantMotionResponse>): void 2840 2841取消大幅动作检测传感器数据。 2842 2843**系统能力**:SystemCapability.Sensors.Sensor 2844 2845**参数:** 2846 2847| 参数名 | 类型 | 必填 | 说明 | 2848| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2849| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 2850| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2851 2852**示例:** 2853 2854```ts 2855import sensor from "@ohos.sensor" 2856import BusinessError from "@ohos.base" 2857 2858function callback1(data: object) { 2859 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2860} 2861 2862function callback2(data: object) { 2863 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2864} 2865 2866try { 2867 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, callback1); 2868 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, callback2); 2869 // 仅取消callback1的注册 2870 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION, callback1); 2871 // 取消注册SensorId.SIGNIFICANT_MOTION的所有回调 2872 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION); 2873} catch (error) { 2874 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2875 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2876} 2877``` 2878 2879### WEAR_DETECTION<sup>9+</sup> 2880 2881off(type: SensorId.WEAR_DETECTION, callback?: Callback<WearDetectionResponse>): void 2882 2883取消订阅佩戴检测传感器数据。 2884 2885**系统能力**:SystemCapability.Sensors.Sensor 2886 2887**参数:** 2888 2889| 参数名 | 类型 | 必填 | 说明 | 2890| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2891| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 2892| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2893 2894**示例:** 2895 2896```ts 2897import sensor from "@ohos.sensor" 2898import BusinessError from "@ohos.base" 2899 2900function callback1(data: object) { 2901 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2902} 2903 2904function callback2(data: object) { 2905 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2906} 2907 2908try { 2909 sensor.on(sensor.SensorId.WEAR_DETECTION, callback1); 2910 sensor.on(sensor.SensorId.WEAR_DETECTION, callback2); 2911 // 仅取消callback1的注册 2912 sensor.off(sensor.SensorId.WEAR_DETECTION, callback1); 2913 // 取消注册SensorId.WEAR_DETECTION的所有回调 2914 sensor.off(sensor.SensorId.WEAR_DETECTION); 2915} catch (error) { 2916 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2917 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2918} 2919``` 2920 2921## sensor.getGeomagneticInfo<sup>9+</sup> 2922 2923getGeomagneticInfo(locationOptions: LocationOptions, timeMillis: number, callback: AsyncCallback<GeomagneticResponse>): void 2924 2925获取某时刻地球上特定位置的地磁场信息,使用Callback异步方式返回结果。 2926 2927**系统能力**:SystemCapability.Sensors.Sensor 2928 2929**参数:** 2930 2931| 参数名 | 类型 | 必填 | 说明 | 2932| --------------- | ------------------------------------------------------------ | ---- | ---------------------------------- | 2933| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置,包括经度、纬度和海拔高度。 | 2934| timeMillis | number | 是 | 获取磁偏角的时间,unix时间戳,单位毫秒。 | 2935| callback | AsyncCallback<[GeomagneticResponse](#geomagneticresponse)> | 是 | 回调函数,异步返回地磁场信息。 | 2936 2937**错误码**: 2938 2939以下错误码的详细介绍请参见 [sensor.getGeomagneticInfo错误码](../errorcodes/errorcode-sensor.md)。 2940 2941| 错误码ID | 错误信息 | 2942| -------- | ------------------ | 2943| 14500101 | Service exception. | 2944 2945**示例:** 2946 2947```ts 2948import sensor from "@ohos.sensor" 2949import BusinessError from "@ohos.base" 2950 2951try { 2952 sensor.getGeomagneticInfo({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000, 2953 (err: BusinessError.BusinessError, data: sensor.GeomagneticResponse) => { 2954 if (err) { 2955 console.error(`Failed to get geomagneticInfo. Code: ${err.code}, message: ${err.message}`); 2956 return; 2957 } 2958 console.info("Succeeded in getting geomagneticInfo x" + data.x); 2959 console.info("Succeeded in getting geomagneticInfo y" + data.y); 2960 console.info("Succeeded in getting geomagneticInfo z" + data.z); 2961 console.info("Succeeded in getting geomagneticInfo geomagneticDip" + data.geomagneticDip); 2962 console.info("Succeeded in getting geomagneticInfo deflectionAngle" + data.deflectionAngle); 2963 console.info("Succeeded in getting geomagneticInfo levelIntensity" + data.levelIntensity); 2964 console.info("Succeeded in getting geomagneticInfo totalIntensity" + data.totalIntensity); 2965 }); 2966} catch (error) { 2967 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 2968 console.error(`Failed to get geomagneticInfo. Code: ${e.code}, message: ${e.message}`); 2969} 2970``` 2971 2972## sensor.getGeomagneticInfo<sup>9+</sup> 2973 2974getGeomagneticInfo(locationOptions: LocationOptions, timeMillis: number): Promise<GeomagneticResponse> 2975 2976获取某时刻地球上特定位置的地磁场信息,使用Promise异步方式返回结果。 2977 2978**系统能力**:SystemCapability.Sensors.Sensor 2979 2980**参数:** 2981 2982| 参数名 | 类型 | 必填 | 说明 | 2983| --------------- | ----------------------------------- | ---- | ---------------------------------- | 2984| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置,包括经度、纬度和海拔高度。 | 2985| timeMillis | number | 是 | 获取磁偏角的时间,unix时间戳,单位毫秒。 | 2986 2987**返回值:** 2988 2989| 类型 | 说明 | 2990| ---------------------------------------------------------- | -------------- | 2991| Promise<[GeomagneticResponse](#geomagneticresponse)> | Promise对象,使用异步方式返回地磁场信息。 | 2992 2993**错误码**: 2994 2995以下错误码的详细介绍请参见 [sensor.getGeomagneticInfo错误码](../errorcodes/errorcode-sensor.md)。 2996 2997| 错误码ID | 错误信息 | 2998| -------- | ------------------ | 2999| 14500101 | Service exception. | 3000 3001**示例:** 3002 3003```ts 3004import sensor from "@ohos.sensor" 3005import BusinessError from "@ohos.base" 3006 3007try { 3008 const promise = sensor.getGeomagneticInfo({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000); 3009 promise.then((data: sensor.GeomagneticResponse) => { 3010 console.info("Succeeded in getting geomagneticInfo x" + data.x); 3011 console.info("Succeeded in getting geomagneticInfo y" + data.y); 3012 console.info("Succeeded in getting geomagneticInfo z" + data.z); 3013 console.info("Succeeded in getting geomagneticInfo geomagneticDip" + data.geomagneticDip); 3014 console.info("Succeeded in getting geomagneticInfo deflectionAngle" + data.deflectionAngle); 3015 console.info("Succeeded in getting geomagneticInfo levelIntensity" + data.levelIntensity); 3016 console.info("Succeeded in getting geomagneticInfo totalIntensity" + data.totalIntensity); 3017 }, (err: BusinessError.BusinessError) => { 3018 console.error(`Failed to get geomagneticInfo. Code: ${err.code}, message: ${err.message}`); 3019 }); 3020} catch (error) { 3021 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3022 console.error(`Failed to get geomagneticInfo. Code: ${e.code}, message: ${e.message}`); 3023} 3024``` 3025 3026## sensor.getDeviceAltitude<sup>9+</sup> 3027 3028getDeviceAltitude(seaPressure: number, currentPressure: number, callback: AsyncCallback<number>): void 3029 3030根据气压值获取海拔高度,使用Callback异步方式返回结果。 3031 3032**系统能力**:SystemCapability.Sensors.Sensor 3033 3034**参数:** 3035 3036| 参数名 | 类型 | 必填 | 说明 | 3037| --------------- | --------------------------- | ---- | ------------------------------------- | 3038| seaPressure | number | 是 | 海平面气压值,单位为hPa。 | 3039| currentPressure | number | 是 | 指定的气压值,单位为hPa。 | 3040| callback | AsyncCallback<number> | 是 | 回调函数,异步返回指定的气压值对应的海拔高度,单位为米。 | 3041 3042**错误码**: 3043 3044以下错误码的详细介绍请参见 [sensor.getDeviceAltitude错误码](../errorcodes/errorcode-sensor.md)。 3045 3046| 错误码ID | 错误信息 | 3047| -------- | ------------------ | 3048| 14500101 | Service exception. | 3049 3050**示例:** 3051 3052```ts 3053import sensor from "@ohos.sensor" 3054import BusinessError from "@ohos.base" 3055 3056try { 3057 let seaPressure = 1013.2; 3058 let currentPressure = 1500.0; 3059 sensor.getDeviceAltitude(seaPressure, currentPressure, (err: BusinessError.BusinessError, data: number) => { 3060 if (err) { 3061 console.error(`Failed to get altitude. Code: ${err.code}, message: ${err.message}`); 3062 return; 3063 } 3064 console.info('Succeeded in getting altitude: ' + data); 3065 }); 3066} catch (error) { 3067 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3068 console.error(`Failed to get altitude. Code: ${e.code}, message: ${e.message}`); 3069} 3070``` 3071 3072## sensor.getDeviceAltitude<sup>9+</sup> 3073 3074getDeviceAltitude(seaPressure: number, currentPressure: number): Promise<number> 3075 3076根据气压值获取海拔高度,使用Promise异步方式返回结果。 3077 3078**系统能力**:SystemCapability.Sensors.Sensor 3079 3080**参数:** 3081 3082| 参数名 | 类型 | 必填 | 说明 | 3083| --------------- | ------ | ---- | ------------------------------------- | 3084| seaPressure | number | 是 | 海平面气压值,单位为hPa。 | 3085| currentPressure | number | 是 | 指定的气压值,单位为hPa。 | 3086 3087**返回值:** 3088 3089| 类型 | 说明 | 3090| --------------------- | ------------------------------------ | 3091| Promise<number> | Promise对象,使用异步方式返回指定的气压值对应的海拔高度,单位为米。 | 3092 3093**错误码**: 3094 3095以下错误码的详细介绍请参见 [sensor.getDeviceAltitude错误码](../errorcodes/errorcode-sensor.md)。 3096 3097| 错误码ID | 错误信息 | 3098| -------- | ------------------ | 3099| 14500101 | Service exception. | 3100 3101**示例:** 3102 3103```ts 3104import sensor from "@ohos.sensor" 3105import BusinessError from "@ohos.base" 3106 3107try { 3108 let seaPressure = 1013.2; 3109 let currentPressure = 1500.0; 3110 const promise = sensor.getDeviceAltitude(seaPressure, currentPressure); 3111 promise.then((data: number) => { 3112 console.info('Succeeded in getting sensor_getDeviceAltitude_Promise', data); 3113 }, (err: BusinessError.BusinessError) => { 3114 console.error(`Failed to get altitude. Code: ${err.code}, message: ${err.message}`); 3115 }); 3116} catch (error) { 3117 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3118 console.error(`Failed to get altitude. Code: ${e.code}, message: ${e.message}`); 3119} 3120``` 3121 3122## sensor.getInclination<sup>9+</sup> 3123 3124getInclination(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void 3125 3126根据倾斜矩阵计算地磁倾角,使用Callback异步方式返回结果。 3127 3128**系统能力**:SystemCapability.Sensors.Sensor 3129 3130**参数:** 3131 3132| 参数名 | 类型 | 必填 | 说明 | 3133| ----------------- | --------------------------- | ---- | ---------------------------- | 3134| inclinationMatrix | Array<number> | 是 | 倾斜矩阵。 | 3135| callback | AsyncCallback<number> | 是 | 回调函数,异步返回地磁倾角,单位为弧度。 | 3136 3137**错误码**: 3138 3139以下错误码的详细介绍请参见 [sensor.getInclination错误码](../errorcodes/errorcode-sensor.md)。 3140 3141| 错误码ID | 错误信息 | 3142| -------- | ------------------ | 3143| 14500101 | Service exception. | 3144 3145**示例:** 3146 3147```ts 3148import sensor from "@ohos.sensor" 3149import BusinessError from "@ohos.base" 3150 3151try { 3152 // inclinationMatrix可以为3*3,或者4*4 3153 let inclinationMatrix = [ 3154 1, 0, 0, 3155 0, 1, 0, 3156 0, 0, 1 3157 ] 3158 sensor.getInclination(inclinationMatrix, (err: BusinessError.BusinessError, data: number) => { 3159 if (err) { 3160 console.error(`Failed to get inclination. Code: ${err.code}, message: ${err.message}`); 3161 return; 3162 } 3163 console.info('Succeeded in getting inclination: ' + data); 3164 }) 3165} catch (error) { 3166 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3167 console.error(`Failed to get inclination. Code: ${e.code}, message: ${e.message}`); 3168} 3169``` 3170 3171## sensor.getInclination<sup>9+</sup> 3172 3173 getInclination(inclinationMatrix: Array<number>): Promise<number> 3174 3175根据倾斜矩阵计算地磁倾角,使用Promise异步方式返回结果。 3176 3177**系统能力**:SystemCapability.Sensors.Sensor 3178 3179**参数:** 3180 3181| 参数名 | 类型 | 必填 | 说明 | 3182| ----------------- | ------------------- | ---- | -------------- | 3183| inclinationMatrix | Array<number> | 是 | 倾斜矩阵。 | 3184 3185**返回值:** 3186 3187| 类型 | 说明 | 3188| --------------------- | ---------------------------- | 3189| Promise<number> | Promise对象,使用异步方式返回地磁倾斜角,单位为弧度。 | 3190 3191**错误码**: 3192 3193以下错误码的详细介绍请参见 [sensor.getInclination错误码](../errorcodes/errorcode-sensor.md)。 3194 3195| 错误码ID | 错误信息 | 3196| -------- | ------------------ | 3197| 14500101 | Service exception. | 3198 3199**示例:** 3200 3201```ts 3202import sensor from "@ohos.sensor" 3203import BusinessError from "@ohos.base" 3204 3205try { 3206 // inclinationMatrix可以为3*3,或者4*4 3207 let inclinationMatrix = [ 3208 1, 0, 0, 3209 0, 1, 0, 3210 0, 0, 1 3211 ] 3212 const promise = sensor.getInclination(inclinationMatrix); 3213 promise.then((data: number) => { 3214 console.info('Succeeded in getting inclination: ' + data); 3215 }, (err: BusinessError.BusinessError) => { 3216 console.error(`Failed to get inclination. Code: ${err.code}, message: ${err.message}`); 3217 }); 3218} catch (error) { 3219 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3220 console.error(`Failed to get inclination. Code: ${e.code}, message: ${e.message}`); 3221} 3222``` 3223 3224## sensor.getAngleVariation<sup>9+</sup> 3225 3226 getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>, 3227 callback: AsyncCallback<Array<number>>): void 3228 3229计算两个旋转矩阵之间的角度变化,使用Callback异步方式返回结果。 3230 3231**系统能力**:SystemCapability.Sensors.Sensor 3232 3233**参数:** 3234 3235| 参数名 | 类型 | 必填 | 说明 | 3236| --------------------- | ---------------------------------------- | ---- | --------------------------------- | 3237| currentRotationMatrix | Array<number> | 是 | 当前旋转矩阵。 | 3238| preRotationMatrix | Array<number> | 是 | 相对旋转矩阵。 | 3239| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回绕z、x、y轴方向的旋转角度。 | 3240 3241**错误码**: 3242 3243以下错误码的详细介绍请参见 [sensor.getAngleVariation错误码](../errorcodes/errorcode-sensor.md)。 3244 3245| 错误码ID | 错误信息 | 3246| -------- | ------------------ | 3247| 14500101 | Service exception. | 3248 3249**示例:** 3250 3251```ts 3252import sensor from "@ohos.sensor" 3253import BusinessError from "@ohos.base" 3254 3255try { 3256 // 旋转矩阵可以为3*3,或者4*4 3257 let currentRotationMatrix = [ 3258 1, 0, 0, 3259 0, 1, 0, 3260 0, 0, 1 3261 ]; 3262 let preRotationMatrix = [ 3263 1, 0, 0, 3264 0, 0.87, -0.50, 3265 0, 0.50, 0.87 3266 ]; 3267 sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix, (err: BusinessError.BusinessError, data: Array<number>) => { 3268 if (err) { 3269 console.error(`Failed to get angle variation. Code: ${err.code}, message: ${err.message}`); 3270 return; 3271 } 3272 if (data.length < 3) { 3273 console.error("Failed to get angle variation, length" + data.length); 3274 } 3275 console.info("Z: " + data[0]); 3276 console.info("X: " + data[1]); 3277 console.info("Y : " + data[2]); 3278 }) 3279} catch (error) { 3280 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3281 console.error(`Failed to get angle variation. Code: ${e.code}, message: ${e.message}`); 3282} 3283``` 3284 3285## sensor.getAngleVariation<sup>9+</sup> 3286 3287getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>> 3288 3289得到两个旋转矩阵之间的角度变化,使用Promise异步方式返回结果。 3290 3291**系统能力**:SystemCapability.Sensors.Sensor 3292 3293**参数:** 3294 3295| 参数名 | 类型 | 必填 | 说明 | 3296| --------------------- | ------------------- | ---- | ------------------ | 3297| currentRotationMatrix | Array<number> | 是 | 当前旋转矩阵。 | 3298| preRotationMatrix | Array<number> | 是 | 相对旋转矩阵。 | 3299 3300**返回值:** 3301 3302| 类型 | 说明 | 3303| ---------------------------------- | --------------------------------- | 3304| Promise<Array<number>> | Promise对象,使用异步方式返回绕z、x、y轴方向的旋转角度。 | 3305 3306**错误码**: 3307 3308以下错误码的详细介绍请参见 [sensor.getAngleVariation错误码](../errorcodes/errorcode-sensor.md)。 3309 3310| 错误码ID | 错误信息 | 3311| -------- | ------------------ | 3312| 14500101 | Service exception. | 3313 3314**示例:** 3315 3316```ts 3317import sensor from "@ohos.sensor" 3318import BusinessError from "@ohos.base" 3319 3320try { 3321 // 旋转矩阵可以为3*3,或者4*4 3322 let currentRotationMatrix = [ 3323 1, 0, 0, 3324 0, 1, 0, 3325 0, 0, 1 3326 ]; 3327 let preRotationMatrix = [ 3328 1, 0, 0, 3329 0, 0.87, -0.50, 3330 0, 0.50, 0.87 3331 ]; 3332 const promise = sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix); 3333 promise.then((data: Array<number>) => { 3334 if (data.length < 3) { 3335 console.error("Failed to get angle variation, length" + data.length); 3336 } 3337 console.info("Z: " + data[0]); 3338 console.info("X: " + data[1]); 3339 console.info("Y : " + data[2]); 3340 }, (err: BusinessError.BusinessError) => { 3341 console.error(`Failed to get angle variation. Code: ${err.code}, message: ${err.message}`); 3342 }); 3343} catch (error) { 3344 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3345 console.error(`Failed to get angle variation. Code: ${e.code}, message: ${e.message}`); 3346} 3347``` 3348 3349## sensor.getRotationMatrix<sup>9+</sup> 3350 3351getRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 3352 3353根据旋转矢量获取旋转矩阵,使用Callback异步方式返回结果。 3354 3355**系统能力**:SystemCapability.Sensors.Sensor 3356 3357**参数:** 3358 3359| 参数名 | 类型 | 必填 | 说明 | 3360| -------------- | ---------------------------------------- | ---- | -------------- | 3361| rotationVector | Array<number> | 是 | 旋转矢量。 | 3362| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回3*3旋转矩阵。 | 3363 3364**错误码**: 3365 3366以下错误码的详细介绍请参见 [sensor.getRotationMatrix错误码](../errorcodes/errorcode-sensor.md)。 3367 3368| 错误码ID | 错误信息 | 3369| -------- | ------------------ | 3370| 14500101 | Service exception. | 3371 3372**示例:** 3373 3374```ts 3375import sensor from "@ohos.sensor" 3376import BusinessError from "@ohos.base" 3377 3378try { 3379 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 3380 sensor.getRotationMatrix(rotationVector, (err: BusinessError.BusinessError, data: Array<number>) => { 3381 if (err) { 3382 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 3383 return; 3384 } 3385 for (let i = 0; i < data.length; i++) { 3386 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 3387 } 3388 }) 3389} catch (error) { 3390 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3391 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 3392} 3393``` 3394 3395## sensor.getRotationMatrix<sup>9+</sup> 3396 3397getRotationMatrix(rotationVector: Array<number>): Promise<Array<number>> 3398 3399根据旋转矢量获取旋转矩阵,使用Promise异步方式返回结果。 3400 3401**系统能力**:SystemCapability.Sensors.Sensor 3402 3403**参数:** 3404 3405| 参数名 | 类型 | 必填 | 说明 | 3406| -------------- | ------------------- | ---- | -------------- | 3407| rotationVector | Array<number> | 是 | 旋转矢量。 | 3408 3409**返回值:** 3410 3411| 类型 | 说明 | 3412| ---------------------------------- | -------------- | 3413| Promise<Array<number>> | Promise对象,使用异步方式返回旋转矩阵。 | 3414 3415**错误码**: 3416 3417以下错误码的详细介绍请参见 [sensor.getRotationMatrix错误码](../errorcodes/errorcode-sensor.md)。 3418 3419| 错误码ID | 错误信息 | 3420| -------- | ------------------ | 3421| 14500101 | Service exception. | 3422 3423**示例:** 3424 3425```ts 3426import sensor from "@ohos.sensor" 3427import BusinessError from "@ohos.base" 3428 3429try { 3430 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 3431 const promise = sensor.getRotationMatrix(rotationVector); 3432 promise.then((data: Array<number>) => { 3433 for (let i = 0; i < data.length; i++) { 3434 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 3435 } 3436 }, (err: BusinessError.BusinessError) => { 3437 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 3438 }); 3439} catch (error) { 3440 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3441 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 3442} 3443``` 3444 3445## sensor.transformRotationMatrix<sup>9+</sup> 3446 3447transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions, 3448 callback: AsyncCallback<Array<number>>): void 3449 3450根据指定坐标系映射旋转矩阵,使用Callback异步方式返回结果。 3451 3452**系统能力**:SystemCapability.Sensors.Sensor 3453 3454**参数:** 3455 3456| 参数名 | 类型 | 必填 | 说明 | 3457| ---------------- | ----------------------------------------- | ---- | ---------------------- | 3458| inRotationVector | Array<number> | 是 | 旋转矩阵。 | 3459| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 指定坐标系方向。 | 3460| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回映射后的旋转矩阵。 | 3461 3462**错误码**: 3463 3464以下错误码的详细介绍请参见 [sensor.transformRotationMatrix错误码](../errorcodes/errorcode-sensor.md)。 3465 3466| 错误码ID | 错误信息 | 3467| -------- | ------------------ | 3468| 14500101 | Service exception. | 3469 3470**示例:** 3471 3472```ts 3473import sensor from "@ohos.sensor" 3474import BusinessError from "@ohos.base" 3475 3476try { 3477 let rotationMatrix = [ 3478 1, 0, 0, 3479 0, 0.87, -0.50, 3480 0, 0.50, 0.87 3481 ]; 3482 sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }, (err: BusinessError.BusinessError, data: Array<number>) => { 3483 if (err) { 3484 console.error(`Failed to transform rotationMatrix. Code: ${err.code}, message: ${err.message}`); 3485 return; 3486 } 3487 for (let i = 0; i < data.length; i++) { 3488 console.info('Succeeded in getting data[' + i + '] = ' + data[i]); 3489 } 3490 }) 3491} catch (error) { 3492 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3493 console.error(`Failed to transform rotationMatrix. Code: ${e.code}, message: ${e.message}`); 3494} 3495``` 3496 3497## sensor.transformRotationMatrix<sup>9+</sup> 3498 3499transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>> 3500 3501根据指定坐标系映射旋转矩阵,使用Promise异步方式返回结果。 3502 3503**系统能力**:SystemCapability.Sensors.Sensor 3504 3505**参数:** 3506 3507| 参数名 | 类型 | 必填 | 说明 | 3508| ---------------- | ----------------------------------------- | ---- | ---------------- | 3509| inRotationVector | Array<number> | 是 | 旋转矩阵。 | 3510| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 指定坐标系方向。 | 3511 3512**返回值:** 3513 3514| 类型 | 说明 | 3515| ---------------------------------- | ---------------------- | 3516| Promise<Array<number>> | Promise对象,使用异步方式返回转换后的旋转矩阵。 | 3517 3518**错误码**: 3519 3520以下错误码的详细介绍请参见 [sensor.transformRotationMatrix错误码](../errorcodes/errorcode-sensor.md)。 3521 3522| 错误码ID | 错误信息 | 3523| -------- | ------------------ | 3524| 14500101 | Service exception. | 3525 3526**示例:** 3527 3528```ts 3529import sensor from "@ohos.sensor" 3530import BusinessError from "@ohos.base" 3531 3532try { 3533 let rotationMatrix = [ 3534 1, 0, 0, 3535 0, 0.87, -0.50, 3536 0, 0.50, 0.87 3537 ]; 3538 const promise = sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }); 3539 promise.then((data: Array<number>) => { 3540 for (let i = 0; i < data.length; i++) { 3541 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 3542 } 3543 }, (err: BusinessError.BusinessError) => { 3544 console.error(`Failed to transform rotationMatrix. Code: ${err.code}, message: ${err.message}`); 3545 }); 3546} catch (error) { 3547 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3548 console.error(`Failed to transform rotationMatrix. Code: ${e.code}, message: ${e.message}`); 3549} 3550``` 3551 3552## sensor.getQuaternion<sup>9+</sup> 3553 3554getQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 3555 3556根据旋转向量计算归一化四元数,使用Callback异步方式返回结果。 3557 3558**系统能力**:SystemCapability.Sensors.Sensor 3559 3560**参数:** 3561 3562| 参数名 | 类型 | 必填 | 说明 | 3563| -------------- | ---------------------------------------- | ---- | -------------- | 3564| rotationVector | Array<number> | 是 | 旋转矢量。 | 3565| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回归一化四元数。 | 3566 3567**错误码**: 3568 3569以下错误码的详细介绍请参见 [sensor.getQuaternion错误码](../errorcodes/errorcode-sensor.md)。 3570 3571| 错误码ID | 错误信息 | 3572| -------- | ------------------ | 3573| 14500101 | Service exception. | 3574 3575**示例:** 3576 3577```ts 3578import sensor from "@ohos.sensor" 3579import BusinessError from "@ohos.base" 3580 3581try { 3582 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 3583 sensor.getQuaternion(rotationVector, (err: BusinessError.BusinessError, data: Array<number>) => { 3584 if (err) { 3585 console.error(`Failed to get quaternion. Code: ${err.code}, message: ${err.message}`); 3586 return; 3587 } 3588 for (let i = 0; i < data.length; i++) { 3589 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 3590 } 3591 }) 3592} catch (error) { 3593 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3594 console.error(`Failed to get quaternion. Code: ${e.code}, message: ${e.message}`); 3595} 3596``` 3597 3598## sensor.getQuaternion<sup>9+</sup> 3599 3600getQuaternion(rotationVector: Array<number>): Promise<Array<number>> 3601 3602根据旋转向量计算归一化四元数,使用Promise异步方式返回结果。 3603 3604**系统能力**:SystemCapability.Sensors.Sensor 3605 3606**参数:** 3607 3608| 参数名 | 类型 | 必填 | 说明 | 3609| -------------- | ------------------- | ---- | -------------- | 3610| rotationVector | Array<number> | 是 | 旋转矢量。 | 3611 3612**返回值:** 3613 3614| 类型 | 说明 | 3615| ---------------------------------- | ------------ | 3616| Promise<Array<number>> | Promise,使用异步方式对象返归一化回四元数。 | 3617 3618**错误码**: 3619 3620以下错误码的详细介绍请参见 [sensor.getQuaternion错误码](../errorcodes/errorcode-sensor.md)。 3621 3622| 错误码ID | 错误信息 | 3623| -------- | ------------------ | 3624| 14500101 | Service exception. | 3625 3626**示例:** 3627 3628```ts 3629import sensor from "@ohos.sensor" 3630import BusinessError from "@ohos.base" 3631 3632try { 3633 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 3634 const promise = sensor.getQuaternion(rotationVector); 3635 promise.then((data: Array<number>) => { 3636 for (let i = 0; i < data.length; i++) { 3637 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 3638 } 3639 }, (err: BusinessError.BusinessError) => { 3640 console.error(`Failed to get quaternion. Code: ${err.code}, message: ${err.message}`); 3641 }); 3642} catch (error) { 3643 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3644 console.error(`Failed to get quaternion. Code: ${e.code}, message: ${e.message}`); 3645} 3646``` 3647 3648## sensor.getOrientation<sup>9+</sup> 3649 3650getOrientation(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 3651 3652根据旋转矩阵计算设备方向,使用Callback异步方式返回结果。 3653 3654**系统能力**:SystemCapability.Sensors.Sensor 3655 3656**参数:** 3657 3658| 参数名 | 类型 | 必填 | 说明 | 3659| -------------- | ---------------------------------------- | ---- | --------------------------------- | 3660| rotationMatrix | Array<number> | 是 | 旋转矩阵。 | 3661| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回围绕z、x、y轴方向的旋转角度。 | 3662 3663**错误码**: 3664 3665以下错误码的详细介绍请参见 [sensor.getOrientation错误码](../errorcodes/errorcode-sensor.md)。 3666 3667| 错误码ID | 错误信息 | 3668| -------- | ------------------ | 3669| 14500101 | Service exception. | 3670 3671**示例:** 3672 3673```ts 3674import sensor from "@ohos.sensor" 3675import BusinessError from "@ohos.base" 3676 3677try { 3678 let preRotationMatrix = [ 3679 1, 0, 0, 3680 0, 0.87, -0.50, 3681 0, 0.50, 0.87 3682 ]; 3683 sensor.getOrientation(preRotationMatrix, (err: BusinessError.BusinessError, data: Array<number>) => { 3684 if (err) { 3685 console.error(`Failed to get orientation. Code: ${err.code}, message: ${err.message}`); 3686 return; 3687 } 3688 if (data.length < 3) { 3689 console.error("Failed to get orientation, length" + data.length); 3690 } 3691 console.info("Succeeded in getting data. Z: " + data[0]); 3692 console.info("Succeeded in getting data. X: " + data[1]); 3693 console.info("Succeeded in getting data. Y: " + data[2]); 3694 }) 3695} catch (error) { 3696 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3697 console.error(`Failed to get orientation. Code: ${e.code}, message: ${e.message}`); 3698} 3699``` 3700 3701## sensor.getOrientation<sup>9+</sup> 3702 3703getOrientation(rotationMatrix: Array<number>): Promise<Array<number>> 3704 3705根据旋转矩阵计算设备的方向,使用Promise异步方式返回结果。 3706 3707**系统能力**:SystemCapability.Sensors.Sensor 3708 3709**参数:** 3710 3711| 参数名 | 类型 | 必填 | 说明 | 3712| -------------- | ------------------- | ---- | -------------- | 3713| rotationMatrix | Array<number> | 是 | 旋转矩阵。 | 3714 3715**返回值:** 3716 3717| 类型 | 说明 | 3718| ---------------------------------- | --------------------------------- | 3719| Promise<Array<number>> | Promise对象,使用异步方式返回围绕z、x、y轴方向的旋转角度。 | 3720 3721**错误码**: 3722 3723以下错误码的详细介绍请参见 [sensor.getOrientation错误码](../errorcodes/errorcode-sensor.md)。 3724 3725| 错误码ID | 错误信息 | 3726| -------- | ------------------ | 3727| 14500101 | Service exception. | 3728 3729**示例:** 3730 3731```ts 3732import sensor from '@ohos.sensor'; 3733import BusinessError from '@ohos.base'; 3734 3735try { 3736 let preRotationMatrix = [ 3737 1, 0, 0, 3738 0, 0.87, -0.50, 3739 0, 0.50, 0.87 3740 ]; 3741 const promise = sensor.getOrientation(preRotationMatrix); 3742 promise.then((data: Array<number>) => { 3743 for (let i = 0; i < data.length; i++) { 3744 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 3745 } 3746 }, (err: BusinessError.BusinessError) => { 3747 console.error(`Failed to getOrientatin. Code: ${err.code}, message: ${err.message}`); 3748 }); 3749} catch (error) { 3750 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3751 console.error(`Failed to getOrientatin Code: ${e.code}, message: ${e.message}`); 3752} 3753``` 3754 3755## sensor.getRotationMatrix<sup>9+</sup> 3756 3757getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void 3758 3759根据重力矢量和地磁矢量计算旋转矩阵,使用Callback异步方式返回结果。 3760 3761**系统能力**:SystemCapability.Sensors.Sensor 3762 3763**参数:** 3764 3765| 参数名 | 类型 | 必填 | 说明 | 3766| ----------- | ------------------------------------------------------------ | ---- | -------------- | 3767| gravity | Array<number> | 是 | 重力矢量。 | 3768| geomagnetic | Array<number> | 是 | 地磁矢量。 | 3769| callback | AsyncCallback<[RotationMatrixResponse](#rotationmatrixresponse)> | 是 | 回调函数,异步返回旋转矩阵。 | 3770 3771**错误码**: 3772 3773以下错误码的详细介绍请参见 [sensor.getRotationMatrix错误码](../errorcodes/errorcode-sensor.md)。 3774 3775| 错误码ID | 错误信息 | 3776| -------- | ------------------ | 3777| 14500101 | Service exception. | 3778 3779**示例:** 3780 3781```ts 3782import sensor from '@ohos.sensor'; 3783import BusinessError from '@ohos.base'; 3784 3785try { 3786 let gravity = [-0.27775216, 0.5351276, 9.788099]; 3787 let geomagnetic = [210.87253, -78.6096, -111.44444]; 3788 sensor.getRotationMatrix(gravity, geomagnetic, (err: BusinessError.BusinessError, data: sensor.RotationMatrixResponse) => { 3789 if (err) { 3790 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 3791 return; 3792 } 3793 console.info('Succeeded in getting rotationMatrix' + JSON.stringify(data)); 3794 }) 3795} catch (error) { 3796 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3797 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 3798} 3799``` 3800 3801## sensor.getRotationMatrix<sup>9+</sup> 3802 3803getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>): Promise<RotationMatrixResponse> 3804 3805根据重力矢量和地磁矢量计算旋转矩阵,使用Promise异步方式返回结果。 3806 3807**系统能力**:SystemCapability.Sensors.Sensor 3808 3809**参数:** 3810 3811| 参数名 | 类型 | 必填 | 说明 | 3812| ----------- | ------------------- | ---- | -------------- | 3813| gravity | Array<number> | 是 | 重力向量。 | 3814| geomagnetic | Array<number> | 是 | 地磁矢量。 | 3815 3816**返回值:** 3817 3818| 类型 | 说明 | 3819| ------------------------------------------------------------ | -------------- | 3820| Promise<[RotationMatrixResponse](#rotationmatrixresponse)> | Promise对象,使用异步方式返回旋转矩阵。 | 3821 3822**错误码**: 3823 3824以下错误码的详细介绍请参见 [sensor.getRotationMatrix错误码](../errorcodes/errorcode-sensor.md)。 3825 3826| 错误码ID | 错误信息 | 3827| -------- | ------------------ | 3828| 14500101 | Service exception. | 3829 3830**示例:** 3831 3832```ts 3833import sensor from '@ohos.sensor'; 3834import BusinessError from '@ohos.base'; 3835 3836try { 3837 let gravity = [-0.27775216, 0.5351276, 9.788099]; 3838 let geomagnetic = [210.87253, -78.6096, -111.44444]; 3839 const promise = sensor.getRotationMatrix(gravity, geomagnetic); 3840 promise.then((data: sensor.RotationMatrixResponse) => { 3841 console.info('Succeeded in getting rotationMatrix' + JSON.stringify(data)); 3842 }, (err: BusinessError.BusinessError) => { 3843 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 3844 }); 3845} catch (error) { 3846 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3847 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 3848} 3849``` 3850 3851## sensor.getSensorList<sup>9+</sup> 3852 3853getSensorList(callback: AsyncCallback<Array<Sensor>>): void 3854 3855获取设备上的所有传感器信息,使用Callback异步方式返回结果。 3856 3857**系统能力**:SystemCapability.Sensors.Sensor 3858 3859**参数:** 3860 3861| 参数名 | 类型 | 必填 | 说明 | 3862| -------- | ---------------------------------------------- | ---- | ---------------- | 3863| callback | AsyncCallback<Array<[Sensor](#sensor9)>> | 是 | 回调函数,异步返回传感器属性列表。 | 3864 3865**错误码**: 3866 3867以下错误码的详细介绍请参见 [sensor.getSensorList错误码](../errorcodes/errorcode-sensor.md)。 3868 3869| 错误码ID | 错误信息 | 3870| -------- | ------------------ | 3871| 14500101 | Service exception. | 3872 3873**示例:** 3874 3875```ts 3876import sensor from '@ohos.sensor'; 3877import BusinessError from '@ohos.base'; 3878 3879try { 3880 sensor.getSensorList((err: BusinessError.BusinessError, data: Array<sensor.Sensor>) => { 3881 if (err) { 3882 console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); 3883 return; 3884 } 3885 for (let i = 0; i < data.length; i++) { 3886 console.info('Succeeded in getting data[' + i + ']: ' + JSON.stringify(data[i])); 3887 } 3888 }); 3889} catch (error) { 3890 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3891 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 3892} 3893``` 3894 3895## sensor.getSensorList<sup>9+</sup> 3896 3897 getSensorList(): Promise<Array<Sensor>> 3898 3899获取设备上的所有传感器信息,使用Promise异步方式返回结果。 3900 3901**系统能力**:SystemCapability.Sensors.Sensor 3902 3903**返回值:** 3904 3905| 参数名 | 类型 | 必填 | 说明 | 3906| ------- | ---------------------------------------- | ---- | ---------------- | 3907| promise | Promise<Array<[Sensor](#sensor9)>> | 是 | Promise对象,使用异步方式返回传感器属性列表。 | 3908 3909**错误码**: 3910 3911以下错误码的详细介绍请参见 [sensor.getSensorList错误码](../errorcodes/errorcode-sensor.md)。 3912 3913| 错误码ID | 错误信息 | 3914| -------- | ------------------ | 3915| 14500101 | Service exception. | 3916 3917**示例:** 3918 3919```ts 3920import sensor from '@ohos.sensor'; 3921import BusinessError from '@ohos.base'; 3922 3923try { 3924 sensor.getSensorList().then((data: Array<sensor.Sensor>) => { 3925 for (let i = 0; i < data.length; i++) { 3926 console.info('Succeeded in getting data[' + i + ']: ' + JSON.stringify(data[i])); 3927 } 3928 }, (err: BusinessError.BusinessError) => { 3929 console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); 3930 }); 3931} catch (error) { 3932 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3933 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 3934} 3935``` 3936 3937## sensor.getSingleSensor<sup>9+</sup> 3938 3939getSingleSensor(type: SensorId, callback: AsyncCallback<Sensor>): void 3940 3941获取指定传感器类型的属性信息,使用Callback异步方式返回结果。 3942 3943**系统能力**:SystemCapability.Sensors.Sensor 3944 3945**参数:** 3946 3947| 参数名 | 类型 | 必填 | 说明 | 3948| -------- | --------------------------------------- | ---- | ---------------- | 3949| type | [SensorId](#sensorid9) | 是 | 指定传感器类型。 | 3950| callback | AsyncCallback<[Sensor](#sensor9)> | 是 | 回调函数,异步返回指定传感器的属性信息。 | 3951 3952**错误码**: 3953 3954以下错误码的详细介绍请参见 [sensor.getSingleSensor错误码](../errorcodes/errorcode-sensor.md)。 3955 3956| 错误码ID | 错误信息 | 3957| -------- | ------------------ | 3958| 14500101 | Service exception. | 3959 3960**示例:** 3961 3962```ts 3963import sensor from '@ohos.sensor'; 3964import BusinessError from '@ohos.base'; 3965 3966try { 3967 sensor.getSingleSensor(sensor.SensorId.ACCELEROMETER, (err: BusinessError.BusinessError, data: sensor.Sensor) => { 3968 if (err) { 3969 console.error(`Failed to get singleSensor. Code: ${err.code}, message: ${err.message}`); 3970 return; 3971 } 3972 console.info('Succeeded in getting sensor: ' + JSON.stringify(data)); 3973 }); 3974} catch (error) { 3975 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 3976 console.error(`Failed to get singleSensor. Code: ${e.code}, message: ${e.message}`); 3977} 3978``` 3979 3980## sensor.getSingleSensor<sup>9+</sup> 3981 3982 getSingleSensor(type: SensorId): Promise<Sensor> 3983 3984获取指定类型的传感器信息,使用Promise异步方式返回结果。 3985 3986**系统能力**:SystemCapability.Sensors.Sensor 3987 3988**参数:** 3989 3990| 参数名 | 类型 | 必填 | 说明 | 3991| ------ | ---------------------- | ---- | ------------ | 3992| type | [SensorId](#sensorid9) | 是 | 传感器类型。 | 3993 3994**返回值:** 3995 3996| 参数名 | 类型 | 必填 | 说明 | 3997| ------- | --------------------------------- | ---- | ---------------------------- | 3998| promise | Promise<[Sensor](#sensor9)> | 是 | 使用异步方式返回传感器信息。 | 3999 4000**错误码**: 4001 4002以下错误码的详细介绍请参见 [sensor.getSingleSensor错误码](../errorcodes/errorcode-sensor.md)。 4003 4004| 错误码ID | 错误信息 | 4005| -------- | ------------------ | 4006| 14500101 | Service exception. | 4007 4008**示例:** 4009 4010```ts 4011import sensor from '@ohos.sensor'; 4012import BusinessError from '@ohos.base'; 4013 4014try { 4015 sensor.getSingleSensor(sensor.SensorId.ACCELEROMETER).then((data: sensor.Sensor) => { 4016 console.info('Succeeded in getting sensor: ' + JSON.stringify(data)); 4017 }, (err: BusinessError.BusinessError) => { 4018 console.error(`Failed to get singleSensor . Code: ${err.code}, message: ${err.message}`); 4019 }); 4020} catch (error) { 4021 let e: BusinessError.BusinessError = error as BusinessError.BusinessError; 4022 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 4023} 4024``` 4025 4026## SensorId<sup>9+</sup> 4027 4028表示当前支持订阅或取消订阅的传感器类型。 4029 4030**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4031 4032| 名称 | 值 | 说明 | 4033| --------------------------- | ---- | ---------------------- | 4034| ACCELEROMETER | 1 | 加速度传感器。 | 4035| GYROSCOPE | 2 | 陀螺仪传感器。 | 4036| AMBIENT_LIGHT | 5 | 环境光传感器。 | 4037| MAGNETIC_FIELD | 6 | 磁场传感器。 | 4038| BAROMETER | 8 | 气压计传感器。 | 4039| HALL | 10 | 霍尔传感器。 | 4040| PROXIMITY | 12 | 接近光传感器。 | 4041| HUMIDITY | 13 | 湿度传感器。 | 4042| COLOR<sup>10+</sup> | 14 | 颜色传感器。<br>系统API:此接口为系统接口 | 4043| SAR<sup>10+</sup> | 15 | 吸收比率传感器。<br>系统API:此接口为系统接口 | 4044| ORIENTATION | 256 | 方向传感器。 | 4045| GRAVITY | 257 | 重力传感器。 | 4046| LINEAR_ACCELEROMETER | 258 | 线性加速度传感器。 | 4047| ROTATION_VECTOR | 259 | 旋转矢量传感器。 | 4048| AMBIENT_TEMPERATURE | 260 | 环境温度传感器。 | 4049| MAGNETIC_FIELD_UNCALIBRATED | 261 | 未校准磁场传感器。 | 4050| GYROSCOPE_UNCALIBRATED | 263 | 未校准陀螺仪传感器。 | 4051| SIGNIFICANT_MOTION | 264 | 有效运动传感器。 | 4052| PEDOMETER_DETECTION | 265 | 计步检测传感器。 | 4053| PEDOMETER | 266 | 计步传感器。 | 4054| HEART_RATE | 278 | 心率传感器。 | 4055| WEAR_DETECTION | 280 | 佩戴检测传感器。 | 4056| ACCELEROMETER_UNCALIBRATED | 281 | 未校准加速度计传感器。 | 4057 4058## SensorType<sup>(deprecated)</sup> 4059 4060表示要订阅或取消订阅的传感器类型。 4061 4062**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4063 4064 4065| 名称 | 值 | 说明 | 4066| ------------------------------------------ | ---- | ---------------------- | 4067| SENSOR_TYPE_ID_ACCELEROMETER | 1 | 加速度传感器。 | 4068| SENSOR_TYPE_ID_GYROSCOPE | 2 | 陀螺仪传感器。 | 4069| SENSOR_TYPE_ID_AMBIENT_LIGHT | 5 | 环境光传感器。 | 4070| SENSOR_TYPE_ID_MAGNETIC_FIELD | 6 | 磁场传感器。 | 4071| SENSOR_TYPE_ID_BAROMETER | 8 | 气压计传感器。 | 4072| SENSOR_TYPE_ID_HALL | 10 | 霍尔传感器。 | 4073| SENSOR_TYPE_ID_PROXIMITY | 12 | 接近光传感器。 | 4074| SENSOR_TYPE_ID_HUMIDITY | 13 | 湿度传感器。 | 4075| SENSOR_TYPE_ID_ORIENTATION | 256 | 方向传感器。 | 4076| SENSOR_TYPE_ID_GRAVITY | 257 | 重力传感器。 | 4077| SENSOR_TYPE_ID_LINEAR_ACCELERATION | 258 | 线性加速度传感器。 | 4078| SENSOR_TYPE_ID_ROTATION_VECTOR | 259 | 旋转矢量传感器。 | 4079| SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 260 | 环境温度传感器。 | 4080| SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 261 | 未校准磁场传感器。 | 4081| SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 263 | 未校准陀螺仪传感器。 | 4082| SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 264 | 有效运动传感器。 | 4083| SENSOR_TYPE_ID_PEDOMETER_DETECTION | 265 | 计步检测传感器。 | 4084| SENSOR_TYPE_ID_PEDOMETER | 266 | 计步传感器。 | 4085| SENSOR_TYPE_ID_HEART_RATE | 278 | 心率传感器。 | 4086| SENSOR_TYPE_ID_WEAR_DETECTION | 280 | 佩戴检测传感器。 | 4087| SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 281 | 未校准加速度计传感器。 | 4088 4089## Response 4090 4091传感器数据的时间戳。 4092 4093**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4094 4095| 名称 | 类型 | 可读 | 可写 | 说明 | 4096| --------- | ------ | ---- | ---- | ------------------------ | 4097| timestamp | number | 是 | 是 | 传感器数据上报的时间戳。 | 4098 4099## Sensor<sup>9+</sup> 4100 4101指示传感器信息。 4102 4103**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4104 4105| 名称 | 类型 | 可读 | 可写 | 说明 | 4106| --------------- | -------- | ---------------------- | ---------------------- | ---------------------- | 4107| sensorName | string | 是 | 否 | 传感器名称。 | 4108| vendorName | string | 是 | 否 | 传感器供应商。 | 4109| firmwareVersion | string | 是 | 否 | 传感器固件版本。 | 4110| hardwareVersion | string | 是 | 否 | 传感器硬件版本。 | 4111| sensorId | number | 是 | 否 | 传感器类型id。 | 4112| maxRange | number | 是 | 否 | 传感器测量范围的最大值。 | 4113| minSamplePeriod | number | 是 | 否 | 允许的最小采样周期。 | 4114| maxSamplePeriod | number | 是 | 否 | 允许的最大采样周期。 | 4115| precision | number | 是 | 否 | 传感器精度。 | 4116| power | number | 是 | 否 | 传感器功率的估计值,单位:mA。 | 4117 4118## ColorResponse<sup>10+</sup> 4119 4120颜色传感器数据,继承于[Response](#response)。 4121 4122**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4123 4124**系统API**:此接口为系统接口 4125 4126 4127| 名称 | 类型 | 可读 | 可写 | 说明 | 4128| ---------------- | ------ | ---- | ---- | ----------------------------- | 4129| lightIntensity | number | 是 | 是 | 表示光的强度,单位 : 勒克斯。 | 4130| colorTemperature | number | 是 | 是 | 表示色温,单位 : 开尔文。 | 4131 4132## SarResponse<sup>10+</sup> 4133 4134吸收比率传感器数据,继承于[Response](#response)。 4135 4136**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4137 4138**系统API**:此接口为系统接口 4139 4140 4141| 名称 | 类型 | 可读 | 可写 | 说明 | 4142| --------------- | ------ | ---- | ---- | ------------------------------- | 4143| absorptionRatio | number | 是 | 是 | 表示具体的吸收率,单位 : W/kg。 | 4144 4145## AccelerometerResponse 4146 4147加速度传感器数据,继承于[Response](#response)。 4148 4149**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4150 4151 4152| 名称 | 类型 | 可读 | 可写 | 说明 | 4153| ---- | ------ | ---- | ---- | ------------------------------------ | 4154| x | number | 是 | 是 | 施加在设备x轴的加速度,单位 : m/s²。 | 4155| y | number | 是 | 是 | 施加在设备y轴的加速度,单位 : m/s²。 | 4156| z | number | 是 | 是 | 施加在设备z轴的加速度,单位 : m/s²。 | 4157 4158 4159## LinearAccelerometerResponse 4160 4161线性加速度传感器数据,继承于[Response](#response)。 4162 4163**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4164 4165 4166| 名称 | 类型 | 可读 | 可写 | 说明 | 4167| ---- | ------ | ---- | ---- | ---------------------------------------- | 4168| x | number | 是 | 是 | 施加在设备x轴的线性加速度,单位 : m/s²。 | 4169| y | number | 是 | 是 | 施加在设备y轴的线性加速度,单位 : m/s²。 | 4170| z | number | 是 | 是 | 施加在设备z轴的线性加速度,单位 : m/s²。 | 4171 4172 4173## AccelerometerUncalibratedResponse 4174 4175未校准加速度计传感器数据,继承于[Response](#response)。 4176 4177**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4178 4179 4180| 名称 | 类型 | 可读 | 可写 | 说明 | 4181| ----- | ------ | ---- | ---- | ------------------------------------------------ | 4182| x | number | 是 | 是 | 施加在设备x轴未校准的加速度,单位 : m/s²。 | 4183| y | number | 是 | 是 | 施加在设备y轴未校准的加速度,单位 : m/s²。 | 4184| z | number | 是 | 是 | 施加在设备z轴未校准的加速度,单位 : m/s²。 | 4185| biasX | number | 是 | 是 | 施加在设备x轴未校准的加速度偏量,单位 : m/s²。 | 4186| biasY | number | 是 | 是 | 施加在设备上y轴未校准的加速度偏量,单位 : m/s²。 | 4187| biasZ | number | 是 | 是 | 施加在设备z轴未校准的加速度偏量,单位 : m/s²。 | 4188 4189 4190## GravityResponse 4191 4192重力传感器数据,继承于[Response](#response)。 4193 4194**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4195 4196 4197| 名称 | 类型 | 可读 | 可写 | 说明 | 4198| ---- | ------ | ---- | ---- | ---------------------------------------- | 4199| x | number | 是 | 是 | 施加在设备x轴的重力加速度,单位 : m/s²。 | 4200| y | number | 是 | 是 | 施加在设备y轴的重力加速度,单位 : m/s²。 | 4201| z | number | 是 | 是 | 施加在设备z轴的重力加速度,单位 : m/s²。 | 4202 4203 4204## OrientationResponse 4205 4206方向传感器数据,继承于[Response](#response)。 4207 4208**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4209 4210 4211| 名称 | 类型 | 可读 | 可写 | 说明 | 4212| ----- | ------ | ---- | ---- | --------------------------------- | 4213| alpha | number | 是 | 是 | 设备围绕Z轴的旋转角度,单位:度。 | 4214| beta | number | 是 | 是 | 设备围绕X轴的旋转角度,单位:度。 | 4215| gamma | number | 是 | 是 | 设备围绕Y轴的旋转角度,单位:度。 | 4216 4217 4218## RotationVectorResponse 4219 4220旋转矢量传感器数据,继承于[Response](#response)。 4221 4222**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4223 4224 4225| 名称 | 类型 | 可读 | 可写 | 说明 | 4226| ---- | ------ | ---- | ---- | ----------------- | 4227| x | number | 是 | 是 | 旋转矢量x轴分量。 | 4228| y | number | 是 | 是 | 旋转矢量y轴分量。 | 4229| z | number | 是 | 是 | 旋转矢量z轴分量。 | 4230| w | number | 是 | 是 | 标量。 | 4231 4232 4233## GyroscopeResponse 4234 4235陀螺仪传感器数据,继承于[Response](#response)。 4236 4237**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4238 4239 4240| 名称 | 类型 | 可读 | 可写 | 说明 | 4241| ---- | ------ | ---- | ---- | -------------------------------- | 4242| x | number | 是 | 是 | 设备x轴的旋转角速度,单位rad/s。 | 4243| y | number | 是 | 是 | 设备y轴的旋转角速度,单位rad/s。 | 4244| z | number | 是 | 是 | 设备z轴的旋转角速度,单位rad/s。 | 4245 4246 4247## GyroscopeUncalibratedResponse 4248 4249未校准陀螺仪传感器数据,继承于[Response](#response)。 4250 4251**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4252 4253 4254| 名称 | 类型 | 可读 | 可写 | 说明 | 4255| ----- | ------ | ---- | ---- | ------------------------------------------ | 4256| x | number | 是 | 是 | 设备x轴未校准的旋转角速度,单位rad/s。 | 4257| y | number | 是 | 是 | 设备y轴未校准的旋转角速度,单位rad/s。 | 4258| z | number | 是 | 是 | 设备z轴未校准的旋转角速度,单位rad/s。 | 4259| biasX | number | 是 | 是 | 设备x轴未校准的旋转角速度偏量,单位rad/s。 | 4260| biasY | number | 是 | 是 | 设备y轴未校准的旋转角速度偏量,单位rad/s。 | 4261| biasZ | number | 是 | 是 | 设备z轴未校准的旋转角速度偏量,单位rad/s。 | 4262 4263 4264## SignificantMotionResponse 4265 4266有效运动传感器数据,继承于[Response](#response)。 4267 4268**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4269 4270 4271| 名称 | 类型 | 可读 | 可写 | 说明 | 4272| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 4273| scalar | number | 是 | 是 | 表示剧烈运动程度。测量三个物理轴(x、y 和 z)上,设备是否存在大幅度运动;若存在大幅度运动则数据上报为1。 | 4274 4275 4276## ProximityResponse 4277 4278接近光传感器数据,继承于[Response](#response)。 4279 4280**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4281 4282 4283| 名称 | 类型 | 可读 | 可写 | 说明 | 4284| -------- | ------ | ---- | ---- | ---------------------------------------------------------- | 4285| distance | number | 是 | 是 | 可见物体与设备显示器的接近程度。0表示接近,大于0表示远离。 | 4286 4287 4288## LightResponse 4289 4290环境光传感器数据,继承于[Response](#response)。 4291 4292**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4293 4294 4295| 名称 | 类型 | 可读 | 可写 | 说明 | 4296| --------- | ------ | ---- | ---- | ---------------------- | 4297| intensity | number | 是 | 是 | 光强(单位:勒克斯)。 | 4298 4299 4300## HallResponse 4301 4302霍尔传感器数据,继承于[Response](#response)。 4303 4304**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4305 4306 4307| 名称 | 类型 | 可读 | 可写 | 说明 | 4308| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 4309| status | number | 是 | 是 | 显示霍尔状态。测量设备周围是否存在磁力吸引,0表示没有,大于0表示有。 | 4310 4311 4312## MagneticFieldResponse 4313 4314磁场传感器数据,继承于[Response](#response)。 4315 4316**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4317 4318 4319| 名称 | 类型 | 可读 | 可写 | 说明 | 4320| ---- | ------ | ---- | ---- | ---------------------------- | 4321| x | number | 是 | 是 | x轴环境磁场强度,单位 : μT。 | 4322| y | number | 是 | 是 | y轴环境磁场强度,单位 : μT。 | 4323| z | number | 是 | 是 | z轴环境磁场强度,单位 : μT。 | 4324 4325 4326## MagneticFieldUncalibratedResponse 4327 4328未校准磁场传感器数据,继承于[Response](#response)。 4329 4330**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4331 4332 4333| 名称 | 类型 | 可读 | 可写 | 说明 | 4334| ----- | ------ | ---- | ---- | -------------------------------------- | 4335| x | number | 是 | 是 | x轴未校准环境磁场强度,单位 : μT。 | 4336| y | number | 是 | 是 | y轴未校准环境磁场强度,单位 : μT。 | 4337| z | number | 是 | 是 | z轴未校准环境磁场强度,单位 : μT。 | 4338| biasX | number | 是 | 是 | x轴未校准环境磁场强度偏量,单位 : μT。 | 4339| biasY | number | 是 | 是 | y轴未校准环境磁场强度偏量,单位 : μT。 | 4340| biasZ | number | 是 | 是 | z轴未校准环境磁场强度偏量,单位 : μT。 | 4341 4342 4343## PedometerResponse 4344 4345计步传感器数据,继承于[Response](#response)。 4346 4347**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4348 4349 4350| 名称 | 类型 | 可读 | 可写 | 说明 | 4351| ----- | ------ | ---- | ---- | ---------------- | 4352| steps | number | 是 | 是 | 用户的行走步数。 | 4353 4354 4355## HumidityResponse 4356 4357湿度传感器数据,继承于[Response](#response)。 4358 4359**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4360 4361 4362| 名称 | 类型 | 可读 | 可写 | 说明 | 4363| -------- | ------ | ---- | ---- | --------------------------------------------------------- | 4364| humidity | number | 是 | 是 | 湿度值。测量环境的相对湿度,以百分比 (%) 表示。 | 4365 4366 4367## PedometerDetectionResponse 4368 4369计步检测传感器数据,继承于[Response](#response)。 4370 4371**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4372 4373 4374| 名称 | 类型 | 可读 | 可写 | 说明 | 4375| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 4376| scalar | number | 是 | 是 | 计步器检测。检测用户的计步动作,如果取值为1则代表用户产生了计步行走的动作,取值为0则代表用户没有发生运动。 | 4377 4378 4379## AmbientTemperatureResponse 4380 4381温度传感器数据,继承于[Response](#response)。 4382 4383**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4384 4385 4386| 名称 | 类型 | 可读 | 可写 | 说明 | 4387| ----------- | ------ | ---- | ---- | -------------------------- | 4388| temperature | number | 是 | 是 | 环境温度(单位:摄氏度)。 | 4389 4390 4391## BarometerResponse 4392 4393气压计传感器数据,继承于[Response](#response)。 4394 4395**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4396 4397 4398| 名称 | 类型 | 可读 | 可写 | 说明 | 4399| -------- | ------ | ---- | ---- | ---------------------- | 4400| pressure | number | 是 | 是 | 压力值(单位:百帕)。 | 4401 4402 4403## HeartRateResponse 4404 4405心率传感器数据,继承于[Response](#response)。 4406 4407**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4408 4409 4410| 名称 | 类型 | 可读 | 可写 | 说明 | 4411| --------- | ------ | ---- | ---- | --------------------------------------- | 4412| heartRate | number | 是 | 是 | 心率值。测量用户的心率数值,单位:bpm。 | 4413 4414 4415## WearDetectionResponse 4416 4417佩戴检测传感器数据,继承于[Response](#response)。 4418 4419**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4420 4421 4422| 名称 | 类型 | 可读 | 可写 | 说明 | 4423| ----- | ------ | ---- | ---- | ------------------------------------------------ | 4424| value | number | 是 | 是 | 表示设备是否被穿戴(1表示已穿戴,0表示未穿戴)。 | 4425 4426 4427## Options 4428 4429设置传感器上报频率。 4430 4431**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4432 4433| 名称 | 类型 | 可读 | 可写 | 说明 | 4434| -------- | ------ | ---- | ---- | ------------------------------------------- | 4435| interval | number | 是 | 是 | 表示传感器的上报频率,默认值为200000000ns。 | 4436 4437## RotationMatrixResponse 4438 4439设置旋转矩阵响应对象。 4440 4441**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4442 4443| 名称 | 类型 | 可读 | 可写 | 说明 | 4444| ----------- | ------------------- | ---- | ---- | ---------- | 4445| rotation | Array<number> | 是 | 是 | 旋转矩阵。 | 4446| inclination | Array<number> | 是 | 是 | 倾斜矩阵。 | 4447 4448 4449## CoordinatesOptions 4450 4451设置坐标选项对象。 4452 4453**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4454 4455| 名称 | 类型 | 可读 | 可写 | 说明 | 4456| ---- | ------ | ---- | ---- | ----------- | 4457| x | number | 是 | 是 | x坐标方向。 | 4458| y | number | 是 | 是 | y坐标方向。 | 4459 4460 4461## GeomagneticResponse 4462 4463设置地磁响应对象。 4464 4465**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4466 4467| 名称 | 类型 | 可读 | 可写 | 说明 | 4468| --------------- | ------ | ---- | ---- | -------------------------------------------------- | 4469| x | number | 是 | 是 | 地磁场的北分量。 | 4470| y | number | 是 | 是 | 地磁场的东分量。 | 4471| z | number | 是 | 是 | 地磁场的垂直分量。 | 4472| geomagneticDip | number | 是 | 是 | 地磁倾角,即地球磁场线与水平面的夹角。 | 4473| deflectionAngle | number | 是 | 是 | 地磁偏角,即地磁北方向与正北方向在水平面上的角度。 | 4474| levelIntensity | number | 是 | 是 | 地磁场的水平强度。 | 4475| totalIntensity | number | 是 | 是 | 地磁场的总强度。 | 4476 4477## LocationOptions 4478 4479指示地理位置。 4480 4481**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor 4482 4483| 名称 | 类型 | 可读 | 可写 | 说明 | 4484| --------- | ------ | ---- | ---- | ---------- | 4485| latitude | number | 是 | 是 | 纬度。 | 4486| longitude | number | 是 | 是 | 经度。 | 4487| altitude | number | 是 | 是 | 海拔高度。 | 4488 4489## sensor.on<sup>(deprecated)</sup> 4490 4491### ACCELEROMETER<sup>(deprecated)</sup> 4492 4493on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>,options?: Options): void 4494 4495监听加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4496 4497从API version 9 开始不再维护,建议使用[sensor.on.ACCELEROMETER](#accelerometer9)代替。 4498 4499**需要权限**:ohos.permission.ACCELEROMETER 4500 4501**系统能力**:SystemCapability.Sensors.Sensor 4502 4503**参数:** 4504 4505| 参数名 | 类型 | 必填 | 说明 | 4506| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4507| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ACCELEROMETER | 是 | 要订阅的加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。 | 4508| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 注册加速度传感器的回调函数,上报的数据类型为AccelerometerResponse。 | 4509| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4510 4511**示例:** 4512 4513```ts 4514import sensor from '@ohos.sensor'; 4515 4516sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 4517 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4518 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4519 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4520}, 4521 { interval: 100000000 } 4522); 4523``` 4524 4525### LINEAR_ACCELERATION<sup>(deprecated)</sup> 4526 4527on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>, options?: Options): void 4528 4529监听线性加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4530 4531从API version 9 开始不再维护,建议使用[sensor.on.LINEAR_ACCELEROMETER](#linear_accelerometer9)代替。 4532 4533**需要权限**:ohos.permission.ACCELEROMETER 4534 4535**系统能力**:SystemCapability.Sensors.Sensor 4536 4537**参数:** 4538 4539| 参数名 | 类型 | 必填 | 说明 | 4540| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4541| type | [SensorType](#sensortype).SENSOR_TYPE_ID_LINEAR_ACCELERATION | 是 | 要订阅的线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。 | 4542| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 注册线性加速度传感器的回调函数,上报的数据类型为LinearAccelerometerResponse。 | 4543| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4544 4545### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 4546 4547on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void 4548 4549监听未校准加速度计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4550 4551从API version 9 开始不再维护,建议使用[sensor.on.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9)代替。 4552 4553**需要权限**:ohos.permission.ACCELEROMETER 4554 4555**系统能力**:SystemCapability.Sensors.Sensor 4556 4557**参数:** 4558 4559| 参数名 | 类型 | 必填 | 说明 | 4560| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4561| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 是 | 要订阅的未校准加速度计传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。 | 4562| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 注册未校准加速度计传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。 | 4563| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4564 4565**示例:** 4566 4567```ts 4568import sensor from '@ohos.sensor'; 4569 4570sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 4571 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4572 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4573 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4574 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 4575 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 4576 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 4577}, 4578 { interval: 100000000 } 4579); 4580 4581``` 4582 4583### GRAVITY<sup>(deprecated)</sup> 4584 4585on(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>,options?: Options): void 4586 4587监听重力传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4588 4589从API version 9 开始不再维护,建议使用[sensor.on.GRAVITY](#gravity9)代替。 4590 4591**系统能力**:SystemCapability.Sensors.Sensor 4592 4593**参数:** 4594 4595| 参数名 | 类型 | 必填 | 说明 | 4596| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------------- | 4597| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GRAVITY | 是 | 要订阅的重力传感器类型为SENSOR_TYPE_ID_GRAVITY。 | 4598| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 注册重力传感器的回调函数,上报的数据类型为GravityResponse。 | 4599| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4600 4601**示例:** 4602 4603```ts 4604import sensor from '@ohos.sensor'; 4605 4606sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, (data: sensor.GravityResponse) => { 4607 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4608 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4609 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4610}, 4611 { interval: 100000000 } 4612); 4613``` 4614 4615### GYROSCOPE<sup>(deprecated)</sup> 4616 4617on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void 4618 4619监听陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4620 4621从API version 9 开始不再维护,建议使用[sensor.on.GYROSCOPE](#gyroscope9)代替。 4622 4623**需要权限**:ohos.permission.GYROSCOPE 4624 4625**系统能力**:SystemCapability.Sensors.Sensor 4626 4627**参数:** 4628 4629| 参数名 | 类型 | 必填 | 说明 | 4630| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4631| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GYROSCOPE | 是 | 要订阅的陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。 | 4632| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 注册陀螺仪传感器的回调函数,上报的数据类型为GyroscopeResponse。 | 4633| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4634 4635**示例:** 4636 4637```ts 4638import sensor from '@ohos.sensor'; 4639 4640sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, (data: sensor.GyroscopeResponse) => { 4641 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4642 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4643 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4644}, 4645 { interval: 100000000 } 4646); 4647``` 4648 4649### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 4650 4651on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback:Callback<GyroscopeUncalibratedResponse>, options?: Options): void 4652 4653监听未校准陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4654 4655从API version 9 开始不再维护,建议使用[sensor.on.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9)代替。 4656 4657**需要权限**:ohos.permission.GYROSCOPE 4658 4659**系统能力**:SystemCapability.Sensors.Sensor 4660 4661**参数:** 4662 4663| 参数名 | 类型 | 必填 | 说明 | 4664| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4665| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 是 | 要订阅的未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。 | 4666| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 注册未校准陀螺仪传感器的回调函数,上报的数据类型为GyroscopeUncalibratedResponse。 | 4667| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4668 4669**示例:** 4670 4671```ts 4672import sensor from '@ohos.sensor'; 4673 4674sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 4675 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4676 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4677 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4678 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 4679 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 4680 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 4681}, 4682 { interval: 100000000 } 4683); 4684``` 4685 4686### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 4687 4688on(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, options?: Options): void 4689 4690监听大幅动作传感器数据变化。如果多次调用该接口,仅最后一次调用生效。 4691 4692从API version 9 开始不再维护,建议使用[sensor.on.SIGNIFICANT_MOTION](#significant_motion9) 代替。 4693 4694**系统能力**:SystemCapability.Sensors.Sensor 4695 4696**参数:** 4697 4698| 参数名 | 类型 | 必填 | 说明 | 4699| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4700| type | [SensorType](#sensortype).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 是 | 要订阅的大幅动作传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。 | 4701| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 注册有效运动传感器的回调函数,上报的数据类型为SignificantMotionResponse。 | 4702| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4703 4704**示例:** 4705 4706```ts 4707import sensor from '@ohos.sensor'; 4708 4709sensor.on(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 4710 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 4711}, 4712 { interval: 100000000 } 4713); 4714``` 4715 4716### PEDOMETER_DETECTION<sup>(deprecated)</sup> 4717 4718on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, options?: Options): void 4719 4720监听计步检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4721 4722从API version 9 开始不再维护,建议使用[sensor.on.PEDOMETER_DETECTION](#pedometer_detection9)代替。 4723 4724**需要权限**:ohos.permission.ACTIVITY_MOTION 4725 4726**系统能力**:SystemCapability.Sensors.Sensor 4727 4728**参数:** 4729 4730| 参数名 | 类型 | 必填 | 说明 | 4731| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4732| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PEDOMETER_DETECTION | 是 | 要订阅的计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。 | 4733| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 注册计步检测传感器的回调函数,上报的数据类型为PedometerDetectionResponse。 | 4734| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4735 4736**示例:** 4737 4738```ts 4739import sensor from '@ohos.sensor'; 4740 4741sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 4742 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 4743}, 4744 { interval: 100000000 } 4745); 4746``` 4747 4748### PEDOMETER<sup>(deprecated)</sup> 4749 4750on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void 4751 4752监听计步传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4753 4754从API version 9 开始不再维护,建议使用[sensor.on.PEDOMETER](#pedometer9)代替。 4755 4756**需要权限**:ohos.permission.ACTIVITY_MOTION 4757 4758**系统能力**:SystemCapability.Sensors.Sensor 4759 4760**参数:** 4761 4762| 参数名 | 类型 | 必填 | 说明 | 4763| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4764| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PEDOMETER | 是 | 要订阅的计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。 | 4765| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 注册计步传感器的回调函数,上报的数据类型为PedometerResponse。 | 4766| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4767 4768**示例:** 4769 4770```ts 4771import sensor from '@ohos.sensor'; 4772 4773sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, (data: sensor.PedometerResponse) => { 4774 console.info('Succeeded in invoking on. Steps: ' + data.steps); 4775}, 4776 { interval: 100000000 } 4777); 4778``` 4779 4780### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 4781 4782on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback:Callback<AmbientTemperatureResponse>, options?: Options): void 4783 4784监听环境温度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4785 4786从API version 9 开始不再维护,建议使用[sensor.on.AMBIENT_TEMPERATURE](#ambient_temperature9)代替。 4787 4788**系统能力**:SystemCapability.Sensors.Sensor 4789 4790**参数:** 4791 4792| 参数名 | 类型 | 必填 | 说明 | 4793| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4794| type | [SensorType](#sensortype).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 是 | 要订阅的环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。 | 4795| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 注册环境温度传感器的回调函数,上报的数据类型为AmbientTemperatureResponse。 | 4796| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4797 4798**示例:** 4799 4800```ts 4801import sensor from '@ohos.sensor'; 4802 4803sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 4804 console.info('Succeeded in invoking on. Temperature: ' + data.temperature); 4805}, 4806 { interval: 100000000 } 4807); 4808``` 4809 4810### MAGNETIC_FIELD<sup>(deprecated)</sup> 4811 4812on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>,options?: Options): void 4813 4814监听磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4815 4816从API version 9 开始不再维护,建议使用[sensor.on.MAGNETIC_FIELD](#magnetic_field9)代替。 4817 4818**系统能力**:SystemCapability.Sensors.Sensor 4819 4820**参数:** 4821 4822| 参数名 | 类型 | 必填 | 说明 | 4823| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4824| type | [SensorType](#sensortype).SENSOR_TYPE_ID_MAGNETIC_FIELD | 是 | 要订阅的磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。 | 4825| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 注册磁场传感器的回调函数,上报的数据类型为MagneticFieldResponse。 | 4826| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4827 4828**示例:** 4829 4830```ts 4831import sensor from '@ohos.sensor'; 4832 4833sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 4834 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4835 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4836 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4837}, 4838 { interval: 100000000 } 4839); 4840``` 4841 4842### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 4843 4844on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void 4845 4846监听未校准磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4847 4848从API version 9 开始不再维护,建议使用[sensor.on.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9)代替。 4849 4850**系统能力**:SystemCapability.Sensors.Sensor 4851 4852**参数:** 4853 4854| 参数名 | 类型 | 必填 | 说明 | 4855| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4856| type | [SensorType](#sensortype).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 是 | 要订阅的未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。 | 4857| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 注册未校准磁场传感器的回调函数,上报的数据类型为MagneticFieldUncalibratedResponse。 | 4858| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4859 4860**示例:** 4861 4862```ts 4863import sensor from '@ohos.sensor'; 4864 4865sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 4866 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 4867 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 4868 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 4869 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 4870 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 4871 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 4872}, 4873 { interval: 100000000 } 4874); 4875``` 4876 4877### PROXIMITY<sup>(deprecated)</sup> 4878 4879on(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: Callback<ProximityResponse>,options?: Options): void 4880 4881监听接近光传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4882 4883从API version 9 开始不再维护,建议使用[sensor.on.PROXIMITY](#proximity9)代替。 4884 4885**系统能力**:SystemCapability.Sensors.Sensor 4886 4887**参数:** 4888 4889| 参数名 | 类型 | 必填 | 说明 | 4890| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4891| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PROXIMITY | 是 | 要订阅的接近光传感器类型为SENSOR_TYPE_ID_PROXIMITY。 | 4892| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 注册接近光传感器的回调函数,上报的数据类型为ProximityResponse。 | 4893| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当接近光事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 4894 4895**示例:** 4896 4897```ts 4898import sensor from '@ohos.sensor'; 4899 4900sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, (data: sensor.ProximityResponse) => { 4901 console.info('Succeeded in invoking on. Distance: ' + data.distance); 4902}, 4903 { interval: 100000000 } 4904); 4905``` 4906 4907### HUMIDITY<sup>(deprecated)</sup> 4908 4909on(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: Callback<HumidityResponse>,options?: Options): void 4910 4911监听湿度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4912 4913从API version 9 开始不再维护,建议使用[sensor.on.HUMIDITY](#humidity9)代替。 4914 4915**系统能力**:SystemCapability.Sensors.Sensor 4916 4917**参数:** 4918 4919| 参数名 | 类型 | 必填 | 说明 | 4920| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 4921| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HUMIDITY | 是 | 要订阅的湿度传感器类型为SENSOR_TYPE_ID_HUMIDITY。 | 4922| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 注册湿度传感器的回调函数,上报的数据类型为HumidityResponse。 | 4923| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4924 4925**示例:** 4926 4927```ts 4928import sensor from '@ohos.sensor'; 4929 4930sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, (data: sensor.HumidityResponse) => { 4931 console.info('Succeeded in invoking on. Humidity: ' + data.humidity); 4932}, 4933 { interval: 100000000 } 4934); 4935``` 4936 4937### BAROMETER<sup>(deprecated)</sup> 4938 4939on(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: Callback<BarometerResponse>,options?: Options): void 4940 4941监听气压计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4942 4943从API version 9 开始不再维护,建议使用[sensor.on.BAROMETER](#barometer9)代替。 4944 4945**系统能力**:SystemCapability.Sensors.Sensor 4946 4947**参数:** 4948 4949| 参数名 | 类型 | 必填 | 说明 | 4950| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4951| type | [SensorType](#sensortype).SENSOR_TYPE_ID_BAROMETER | 是 | 要订阅的气压计传感器类型为SENSOR_TYPE_ID_BAROMETER。 | 4952| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 注册气压计传感器的回调函数,上报的数据类型为BarometerResponse。 | 4953| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 4954 4955**示例:** 4956 4957```ts 4958import sensor from '@ohos.sensor'; 4959 4960sensor.on(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, (data: sensor.BarometerResponse) => { 4961 console.info('Succeeded in invoking on. Atmospheric pressure: ' + data.pressure); 4962}, 4963 { interval: 100000000 } 4964); 4965``` 4966 4967### HALL<sup>(deprecated)</sup> 4968 4969on(type: SensorType.SENSOR_TYPE_ID_HALL, callback: Callback<HallResponse>, options?: Options): void 4970 4971监听霍尔传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 4972 4973从API version 9 开始不再维护,建议使用[sensor.on.HALL](#hall9)代替。 4974 4975**系统能力**:SystemCapability.Sensors.Sensor 4976 4977**参数:** 4978 4979| 参数名 | 类型 | 必填 | 说明 | 4980| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 4981| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HALL | 是 | 要订阅的霍尔传感器类型为SENSOR_TYPE_ID_HALL。 | 4982| callback | Callback<[HallResponse](#hallresponse)> | 是 | 注册霍尔传感器的回调函数,上报的数据类型为 HallResponse。 | 4983| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当霍尔事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 4984 4985**示例:** 4986 4987```ts 4988import sensor from '@ohos.sensor'; 4989 4990sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HALL, (data: sensor.HallResponse) => { 4991 console.info('Succeeded in invoking on. Status: ' + data.status); 4992}, 4993 { interval: 100000000 } 4994); 4995``` 4996 4997### AMBIENT_LIGHT<sup>(deprecated)</sup> 4998 4999on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: Callback<LightResponse>, options?: Options): void 5000 5001监听环境光传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 5002 5003从API version 9 开始不再维护,建议使用[sensor.on.AMBIENT_LIGHT](#ambient_light9)代替。 5004 5005**系统能力**:SystemCapability.Sensors.Sensor 5006 5007**参数:** 5008 5009| 参数名 | 类型 | 必填 | 说明 | 5010| -------- | ------------------------------------------------------ | ---- | ----------------------------------------------------------- | 5011| type | [SensorType](#sensortype).SENSOR_TYPE_ID_AMBIENT_LIGHT | 是 | 要订阅的环境光传感器类型为SENSOR_TYPE_ID_AMBIENT_LIGHT。 | 5012| callback | Callback<[LightResponse](#lightresponse)> | 是 | 注册环境光传感器的回调函数,上报的数据类型为LightResponse。 | 5013| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 5014 5015**示例:** 5016 5017```ts 5018import sensor from '@ohos.sensor'; 5019 5020sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, (data: sensor.LightResponse) => { 5021 console.info('Succeeded in invoking on. Illumination: ' + data.intensity); 5022}, 5023 { interval: 100000000 } 5024); 5025``` 5026 5027### ORIENTATION<sup>(deprecated)</sup> 5028 5029on(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: Callback<OrientationResponse>, options?: Options): void 5030 5031监听方向传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 5032 5033从API version 9 开始不再维护,建议使用[sensor.on.ORIENTATION](#orientation9)代替。 5034 5035**系统能力**:SystemCapability.Sensors.Sensor 5036 5037**参数:** 5038 5039| 参数名 | 类型 | 必填 | 说明 | 5040| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5041| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ORIENTATION | 是 | 要订阅的方向传感器类型为SENSOR_TYPE_ID_ORIENTATION | 5042| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 注册方向传感器的回调函数,上报的数据类型为OrientationResponse。 | 5043| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 5044 5045**示例:** 5046 5047```ts 5048import sensor from '@ohos.sensor'; 5049 5050sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, (data: sensor.OrientationResponse) => { 5051 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 5052 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 5053 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 5054}, 5055 { interval: 100000000 } 5056); 5057``` 5058 5059### HEART_RATE<sup>(deprecated)</sup> 5060 5061on(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback<HeartRateResponse>, options?: Options): void 5062 5063监听心率传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 5064 5065从API version 9 开始不再维护,建议使用[sensor.on.HEART_RATE](#heart_rate9)代替。 5066 5067**需要权限**:ohos.permission.HEALTH_DATA 5068 5069**系统能力**:SystemCapability.Sensors.Sensor 5070 5071**参数:** 5072 5073| 参数名 | 类型 | 必填 | 说明 | 5074| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5075| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HEART_RATE | 是 | 要订阅的心率传感器类型为SENSOR_TYPE_ID_HEART_RATE。 | 5076| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 注册心率传感器的回调函数,上报的数据类型为HeartRateResponse。 | 5077| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 5078 5079### ROTATION_VECTOR<sup>(deprecated)</sup> 5080 5081on(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR,callback: Callback<RotationVectorResponse>,options?: Options): void 5082 5083监听旋转矢量传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 5084 5085从API version 9 开始不再维护,建议使用[sensor.on.ROTATION_VECTOR](#rotation_vector9)代替。 5086 5087**系统能力**:SystemCapability.Sensors.Sensor 5088 5089**参数:** 5090 5091| 参数名 | 类型 | 必填 | 说明 | 5092| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5093| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ROTATION_VECTOR | 是 | 要订阅的旋转矢量传感器类型为SENSOR_TYPE_ID_ROTATION_VECTOR。 | 5094| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 注册旋转矢量传感器的回调函数,上报的数据类型为RotationVectorResponse。 | 5095| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 5096 5097**示例:** 5098 5099```ts 5100import sensor from '@ohos.sensor'; 5101 5102sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 5103 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 5104 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 5105 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 5106 console.info('Succeeded in invoking on. Scalar quantity: ' + data.w); 5107}, 5108 { interval: 100000000 } 5109); 5110``` 5111 5112### WEAR_DETECTION<sup>(deprecated)</sup> 5113 5114on(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback<WearDetectionResponse>,options?: Options): void 5115 5116监听所佩戴的检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 5117 5118从API version 9 开始不再维护,建议使用[sensor.on.WEAR_DETECTION](#wear_detection9)代替。 5119 5120**系统能力**:SystemCapability.Sensors.Sensor 5121 5122**参数:** 5123 5124| 参数名 | 类型 | 必填 | 说明 | 5125| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5126| type | [SensorType](#sensortype).SENSOR_TYPE_ID_WEAR_DETECTION | 是 | 要订阅的佩戴检测传感器类型为SENSOR_TYPE_ID_WEAR_DETECTION。 | 5127| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 注册佩戴检测传感器的回调函数,上报的数据类型为WearDetectionResponse。 | 5128| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 5129 5130**示例:** 5131 5132```ts 5133import sensor from '@ohos.sensor'; 5134 5135sensor.on(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 5136 console.info('Succeeded in invoking on. Wear status: ' + data.value); 5137}, 5138 { interval: 100000000 } 5139); 5140``` 5141 5142## sensor.once<sup>(deprecated)</sup> 5143 5144### ACCELEROMETER<sup>(deprecated)</sup> 5145 5146once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>): void 5147 5148监听加速度传感器的数据变化一次。 5149 5150从API version 9 开始不再维护,建议使用[sensor.once.ACCELEROMETER](#accelerometer9-1)代替。 5151 5152**需要权限**:ohos.permission.ACCELEROMETER 5153 5154**系统能力**:SystemCapability.Sensors.Sensor 5155 5156**参数:** 5157 5158| 参数名 | 类型 | 必填 | 说明 | 5159| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5160| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ACCELEROMETER | 是 | 加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。 | 5161| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 注册一次加速度传感器的回调函数,上报的数据类型为AccelerometerResponse。 | 5162 5163**示例:** 5164 5165```ts 5166import sensor from '@ohos.sensor'; 5167 5168sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 5169 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5170 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5171 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5172}); 5173``` 5174 5175### LINEAR_ACCELERATION<sup>(deprecated)</sup> 5176 5177once(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>): void 5178 5179监听线性加速度传感器数据变化一次。 5180 5181从API version 9 开始不再维护,建议使用[sensor.once.LINEAR_ACCELEROMETER](#linear_accelerometer9-1)代替。 5182 5183**需要权限**:ohos.permission.ACCELERATION 5184 5185**系统能力**:SystemCapability.Sensors.Sensor 5186 5187**参数:** 5188 5189| 参数名 | 类型 | 必填 | 说明 | 5190| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5191| type | [SensorType](#sensortype).SENSOR_TYPE_ID_LINEAR_ACCELERATION | 是 | 线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。 | 5192| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 注册一次线性加速度传感器的回调函数,上报的数据类型为LinearAccelerometerResponse。 | 5193 5194### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 5195 5196once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>): void 5197 5198监听未校准加速度传感器的数据变化一次。 5199 5200从API version 9 开始不再维护,建议使用[sensor.once.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9-1)代替。 5201 5202**需要权限**:ohos.permission.ACCELEROMETER 5203 5204**系统能力**:SystemCapability.Sensors.Sensor 5205 5206**参数:** 5207 5208| 参数名 | 类型 | 必填 | 说明 | 5209| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5210| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 是 | 未校准加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。 | 5211| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 注册一次未校准加速度传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。 | 5212 5213**示例:** 5214 5215```ts 5216import sensor from '@ohos.sensor'; 5217 5218sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 5219 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5220 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5221 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5222 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 5223 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 5224 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 5225}); 5226``` 5227 5228### GRAVITY<sup>(deprecated)</sup> 5229 5230once(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>): void 5231 5232监听重力传感器的数据变化一次。 5233 5234从API version 9 开始不再维护,建议使用[sensor.once.GRAVITY](#gravity9-1)代替。 5235 5236**系统能力**:SystemCapability.Sensors.Sensor 5237 5238**参数:** 5239 5240| 参数名 | 类型 | 必填 | 说明 | 5241| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 5242| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GRAVITY | 是 | 重力传感器类型为SENSOR_TYPE_ID_GRAVITY。 | 5243| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 注册一次重力传感器的回调函数,上报的数据类型为GravityResponse。 | 5244 5245**示例:** 5246 5247```ts 5248import sensor from '@ohos.sensor'; 5249 5250sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, (data: sensor.GravityResponse) => { 5251 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5252 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5253 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5254 }); 5255``` 5256 5257### GYROSCOPE<sup>(deprecated)</sup> 5258 5259once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>): void 5260 5261监听陀螺仪传感器的数据变化一次。 5262 5263从API version 9 开始不再维护,建议使用[sensor.once.GYROSCOPE](#gyroscope9-1)代替。 5264 5265**需要权限**:ohos.permission.GYROSCOPE 5266 5267**系统能力**:SystemCapability.Sensors.Sensor 5268 5269**参数:** 5270 5271| 参数名 | 类型 | 必填 | 说明 | 5272| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5273| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GYROSCOPE | 是 | 陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。 | 5274| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 注册一次陀螺仪传感器的回调函数,上报的数据类型为GyroscopeResponse。 | 5275 5276**示例:** 5277 5278```ts 5279import sensor from '@ohos.sensor'; 5280 5281sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, (data: sensor.GyroscopeResponse) => { 5282 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5283 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5284 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5285}); 5286``` 5287 5288### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 5289 5290once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback: Callback<GyroscopeUncalibratedResponse>): void 5291 5292监听未校准陀螺仪传感器的数据变化一次。 5293 5294从API version 9 开始不再维护,建议使用[sensor.once.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9-1)代替。 5295 5296**需要权限**:ohos.permission.GYROSCOPE 5297 5298**系统能力**:SystemCapability.Sensors.Sensor 5299 5300**参数:** 5301 5302| 参数名 | 类型 | 必填 | 说明 | 5303| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5304| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 是 | 未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。 | 5305| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 注册一次未校准陀螺仪传感器的回调函数,上报的数据类型为GyroscopeUncalibratedResponse。 | 5306 5307**示例:** 5308```ts 5309import sensor from '@ohos.sensor'; 5310 5311sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 5312 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5313 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5314 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5315 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 5316 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 5317 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 5318}); 5319``` 5320 5321### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 5322 5323once(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION,callback: Callback<SignificantMotionResponse>): void 5324 5325监听有效运动传感器的数据变化一次。 5326 5327从API version 9 开始不再维护,建议使用[sensor.once.SIGNIFICANT_MOTION](#significant_motion9-1)代替。 5328 5329**系统能力**:SystemCapability.Sensors.Sensor 5330 5331**参数:** 5332 5333| 参数名 | 类型 | 必填 | 说明 | 5334| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5335| type | [SensorType](#sensortype).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 是 | 有效运动传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。 | 5336| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 注册一次有效运动传感器的回调函数,上报的数据类型为SignificantMotionResponse。 | 5337 5338**示例:** 5339 5340```ts 5341import sensor from '@ohos.sensor'; 5342 5343sensor.once(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 5344 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 5345}); 5346``` 5347 5348### PEDOMETER_DETECTION<sup>(deprecated)</sup> 5349 5350once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION,callback: Callback<PedometerDetectionResponse>): void 5351 5352监听计步检测传感器数据变化一次。 5353 5354从API version 9 开始不再维护,建议使用[sensor.once.PEDOMETER_DETECTION](#pedometer_detection9-1)代替。 5355 5356**需要权限**:ohos.permission.ACTIVITY_MOTION 5357 5358**系统能力**:SystemCapability.Sensors.Sensor 5359 5360**参数:** 5361 5362| 参数名 | 类型 | 必填 | 说明 | 5363| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5364| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PEDOMETER_DETECTION | 是 | 计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。 | 5365| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 注册一次计步检测传感器的回调函数,上报的数据类型为PedometerDetectionResponse。 | 5366 5367**示例:** 5368 5369```ts 5370import sensor from '@ohos.sensor'; 5371 5372sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 5373 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 5374}); 5375``` 5376 5377### PEDOMETER<sup>(deprecated)</sup> 5378 5379once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>): void 5380 5381监听计步器传感器数据变化一次。 5382 5383从API version 9 开始不再维护,建议使用[sensor.once.PEDOMETER](#pedometer9-1)代替。 5384 5385**需要权限**:ohos.permission.ACTIVITY_MOTION 5386 5387**系统能力**:SystemCapability.Sensors.Sensor 5388 5389**参数:** 5390 5391| 参数名 | 类型 | 必填 | 说明 | 5392| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5393| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PEDOMETER | 是 | 计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。 | 5394| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 注册一次计步传感器的回调函数,上报的数据类型为PedometerResponse。 | 5395 5396**示例:** 5397 5398```ts 5399import sensor from '@ohos.sensor'; 5400 5401sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, (data: sensor.PedometerResponse) => { 5402 console.info('Succeeded in invoking once. Steps: ' + data.steps); 5403}); 5404``` 5405 5406### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 5407 5408once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback: Callback<AmbientTemperatureResponse>): void 5409 5410监听环境温度传感器数据变化一次。 5411 5412从API version 9 开始不再维护,建议使用[sensor.once.AMBIENT_TEMPERATURE](#ambient_temperature9-1)代替。 5413 5414**系统能力**:SystemCapability.Sensors.Sensor 5415 5416**参数:** 5417 5418| 参数名 | 类型 | 必填 | 说明 | 5419| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5420| type | [SensorType](#sensortype).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 是 | 环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。 | 5421| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 注册一次环境温度传感器的回调函数,上报的数据类型为AmbientTemperatureResponse。 | 5422 5423**示例:** 5424 5425```ts 5426import sensor from '@ohos.sensor'; 5427 5428sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 5429 console.info('Succeeded in invoking once. Temperature: ' + data.temperature); 5430}); 5431``` 5432 5433### MAGNETIC_FIELD<sup>(deprecated)</sup> 5434 5435once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>): void 5436 5437监听磁场传感器数据变化一次。 5438 5439从API version 9 开始不再维护,建议使用[sensor.once.MAGNETIC_FIELD](#magnetic_field9-1)代替。 5440 5441**系统能力**:SystemCapability.Sensors.Sensor 5442 5443**参数:** 5444 5445| 参数名 | 类型 | 必填 | 说明 | 5446| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5447| type | [SensorType](#sensortype).SENSOR_TYPE_ID_MAGNETIC_FIELD | 是 | 磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。 | 5448| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 注册一次磁场传感器的回调函数,上报的数据类型为MagneticFieldResponse。 | 5449 5450**示例:** 5451 5452```ts 5453import sensor from '@ohos.sensor'; 5454 5455sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 5456 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5457 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5458 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5459}); 5460``` 5461 5462### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 5463 5464once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>): void 5465 5466监听未校准磁场传感器数据变化一次。 5467 5468从API version 9 开始不再维护,建议使用[sensor.once.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9-1)代替。 5469 5470**系统能力**:SystemCapability.Sensors.Sensor 5471 5472**参数:** 5473 5474| 参数名 | 类型 | 必填 | 说明 | 5475| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5476| type | [SensorType](#sensortype).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 是 | 未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。 | 5477| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 注册一次未校准磁场传感器的回调函数,上报的数据类型为MagneticFieldUncalibratedResponse。 | 5478 5479**示例:** 5480 5481```ts 5482import sensor from '@ohos.sensor'; 5483 5484sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 5485 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5486 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5487 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5488 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 5489 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 5490 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 5491}); 5492``` 5493 5494### PROXIMITY<sup>(deprecated)</sup> 5495 5496once(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: Callback<ProximityResponse>): void 5497 5498监听接近光传感器数据变化一次。 5499 5500从API version 9 开始不再维护,建议使用[sensor.once.PROXIMITY](#proximity9-1)代替。 5501 5502**系统能力**:SystemCapability.Sensors.Sensor 5503 5504**参数:** 5505 5506| 参数名 | 类型 | 必填 | 说明 | 5507| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5508| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PROXIMITY | 是 | 接近光传感器类型为SENSOR_TYPE_ID_PROXIMITY。 | 5509| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 注册一次接近光传感器的回调函数,上报的数据类型为ProximityResponse。 | 5510 5511**示例:** 5512 5513```ts 5514import sensor from '@ohos.sensor'; 5515 5516sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, (data: sensor.ProximityResponse) => { 5517 console.info('Succeeded in invoking once. Distance: ' + data.distance); 5518} 5519); 5520``` 5521 5522### HUMIDITY<sup>(deprecated)</sup> 5523 5524once(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: Callback<HumidityResponse>): void 5525 5526监听湿度传感器数据变化一次。 5527 5528从API version 9 开始不再维护,建议使用[sensor.once.HUMIDITY](#humidity9-1)代替。 5529 5530**系统能力**:SystemCapability.Sensors.Sensor 5531 5532**参数:** 5533 5534| 参数名 | 类型 | 必填 | 说明 | 5535| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 5536| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HUMIDITY | 是 | 湿度传感器类型为SENSOR_TYPE_ID_HUMIDITY。 | 5537| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 注册一次湿度传感器的回调函数,上报的数据类型为HumidityResponse。 | 5538 5539**示例:** 5540 5541```ts 5542import sensor from '@ohos.sensor'; 5543 5544sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, (data: sensor.HumidityResponse) => { 5545 console.info('Succeeded in invoking once. Humidity: ' + data.humidity); 5546}); 5547``` 5548 5549### BAROMETER<sup>(deprecated)</sup> 5550 5551once(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: Callback<BarometerResponse>): void 5552 5553监听气压计传感器数据变化一次。 5554 5555从API version 9 开始不再维护,建议使用[sensor.once.BAROMETER](#barometer9-1)代替。 5556 5557**系统能力**:SystemCapability.Sensors.Sensor 5558 5559**参数:** 5560 5561| 参数名 | 类型 | 必填 | 说明 | 5562| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5563| type | [SensorType](#sensortype).SENSOR_TYPE_ID_BAROMETER | 是 | 气压计传感器类型为SENSOR_TYPE_ID_BAROMETER。 | 5564| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 注册一次气压计传感器的回调函数,上报的数据类型为BarometerResponse。 | 5565 5566**示例:** 5567 5568```ts 5569import sensor from '@ohos.sensor'; 5570 5571sensor.once(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, (data: sensor.BarometerResponse) => { 5572 console.info('Succeeded in invoking once. Atmospheric pressure: ' + data.pressure); 5573}); 5574``` 5575 5576### HALL<sup>(deprecated)</sup> 5577 5578once(type: SensorType.SENSOR_TYPE_ID_HALL, callback: Callback<HallResponse>): void 5579 5580监听霍尔传感器数据变化一次。 5581 5582从API version 9 开始不再维护,建议使用[sensor.once.HALL](#hall9-1)代替。 5583 5584**系统能力**:SystemCapability.Sensors.Sensor 5585 5586**参数:** 5587 5588| 参数名 | 类型 | 必填 | 说明 | 5589| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 5590| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HALL | 是 | 霍尔传感器类型为SENSOR_TYPE_ID_HALL。 | 5591| callback | Callback<[HallResponse](#hallresponse)> | 是 | 注册一次霍尔传感器的回调函数,上报的数据类型为HallResponse。 | 5592 5593**示例:** 5594 5595```ts 5596import sensor from '@ohos.sensor'; 5597 5598sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HALL, (data: sensor.HallResponse) => { 5599 console.info('Succeeded in invoking once. Status: ' + data.status); 5600}); 5601``` 5602 5603### AMBIENT_LIGHT<sup>(deprecated)</sup> 5604 5605once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: Callback<LightResponse>): void 5606 5607监听环境光传感器数据变化一次。 5608 5609从API version 9 开始不再维护,建议使用[sensor.once.AMBIENT_LIGHT](#ambient_light9-1)代替。 5610 5611**系统能力**:SystemCapability.Sensors.Sensor 5612 5613**参数:** 5614 5615| 参数名 | 类型 | 必填 | 说明 | 5616| -------- | ------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5617| type | [SensorType](#sensortype).SENSOR_TYPE_ID_AMBIENT_LIGHT | 是 | 环境光传感器类型为SENSOR_TYPE_ID_AMBIENT_LIGHT。 | 5618| callback | Callback<[LightResponse](#lightresponse)> | 是 | 注册一次环境光传感器的回调函数,上报的数据类型为LightResponse。 | 5619 5620**示例:** 5621 5622```ts 5623import sensor from '@ohos.sensor'; 5624 5625sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, (data: sensor.LightResponse) => { 5626 console.info('Succeeded in invoking once. invoking once. Illumination: ' + data.intensity); 5627}); 5628``` 5629 5630### ORIENTATION<sup>(deprecated)</sup> 5631 5632once(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: Callback<OrientationResponse>): void 5633 5634监听方向传感器数据变化一次。 5635 5636从API version 9 开始不再维护,建议使用[sensor.once.ORIENTATION](#orientation9-1)代替。 5637 5638**系统能力**:SystemCapability.Sensors.Sensor 5639 5640**参数:** 5641 5642| 参数名 | 类型 | 必填 | 说明 | 5643| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5644| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ORIENTATION | 是 | 方向传感器类型为SENSOR_TYPE_ID_ORIENTATION。 | 5645| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 注册一次方向传感器的回调函数,上报的数据类型为OrientationResponse。 | 5646 5647**示例:** 5648 5649```ts 5650import sensor from '@ohos.sensor'; 5651 5652sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, (data: sensor.OrientationResponse) => { 5653 console.info('Succeeded in invoking the device rotateing at an angle around the X axis: ' + data.beta); 5654 console.info('Succeeded in invoking the device rotateing at an angle around the Y axis: ' + data.gamma); 5655 console.info('Succeeded in invoking the device rotateing at an angle around the Z axis: ' + data.alpha); 5656}); 5657``` 5658 5659### ROTATION_VECTOR<sup>(deprecated)</sup> 5660 5661once(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback: Callback<RotationVectorResponse>): void 5662 5663监听旋转矢量传感器数据变化一次。 5664 5665从API version 9 开始不再维护,建议使用[sensor.once.ROTATION_VECTOR](#rotation_vector9-1)代替。 5666 5667**系统能力**:SystemCapability.Sensors.Sensor 5668 5669**参数:** 5670 5671| 参数名 | 类型 | 必填 | 说明 | 5672| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5673| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ROTATION_VECTOR | 是 | 旋转矢量传感器类型为SENSOR_TYPE_ID_ROTATION_VECTOR。 | 5674| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 注册一次旋转矢量传感器的回调函数,上报的数据类型为RotationVectorResponse。 | 5675 5676**示例:** 5677 5678```ts 5679import sensor from '@ohos.sensor'; 5680 5681sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 5682 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 5683 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 5684 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 5685 console.info('Succeeded in invoking once. Scalar quantity: ' + data.w); 5686}); 5687``` 5688 5689### HEART_RATE<sup>(deprecated)</sup> 5690 5691once(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback<HeartRateResponse>): void 5692 5693监听心率传感器数据变化一次。 5694 5695从API version 9 开始不再维护,建议使用[sensor.once.HEART_RATE](#heart_rate9-1)代替。 5696 5697**需要权限**:ohos.permission.HEART_RATE 5698 5699**系统能力**:SystemCapability.Sensors.Sensor 5700 5701**参数:** 5702 5703| 参数名 | 类型 | 必填 | 说明 | 5704| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5705| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HEART_RATE | 是 | 心率传感器类型为SENSOR_TYPE_ID_HEART_RATE。 | 5706| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 注册一次心率传感器的回调函数,上报的数据类型为HeartRateResponse。 | 5707 5708### WEAR_DETECTION<sup>(deprecated)</sup> 5709 5710once(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback<WearDetectionResponse>): void 5711 5712监听所佩戴的检测传感器的数据变化一次。 5713 5714从API version 9 开始不再维护,建议使用[sensor.once.WEAR_DETECTION](#wear_detection9-1)代替。 5715 5716**系统能力**:SystemCapability.Sensors.Sensor 5717 5718**参数:** 5719 5720| 参数名 | 类型 | 必填 | 说明 | 5721| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5722| type | [SensorType](#sensortype).SENSOR_TYPE_ID_WEAR_DETECTION | 是 | 佩戴检测传感器类型为SENSOR_TYPE_ID_WEAR_DETECTION。 | 5723| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 注册一次穿戴检测传感器的回调函数,上报的数据类型为WearDetectionResponse。 | 5724 5725**示例:** 5726```ts 5727import sensor from '@ohos.sensor'; 5728 5729sensor.once(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 5730 console.info("Succeeded in invoking once. Wear status: " + data.value); 5731}); 5732``` 5733 5734## sensor.off<sup>(deprecated)</sup> 5735 5736### ACCELEROMETER<sup>(deprecated)</sup> 5737 5738off(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback?: Callback<AccelerometerResponse>): void 5739 5740取消订阅传感器数据。 5741 5742从API version 9 开始不再维护,建议使用[sensor.off.ACCELEROMETER](#accelerometer9-2)代替。 5743 5744**需要权限**:ohos.permission.ACCELEROMETER 5745 5746**系统能力**:SystemCapability.Sensors.Sensor 5747 5748**参数:** 5749 5750| 参数名 | 类型 | 必填 | 说明 | 5751| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5752| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ACCELEROMETER | 是 | 要取消订阅的加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。 | 5753| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5754 5755**示例:** 5756 5757```ts 5758import sensor from '@ohos.sensor'; 5759 5760function callback(data: sensor.AccelerometerResponse) { 5761 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 5762 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 5763 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 5764} 5765 5766sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 5767``` 5768 5769### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 5770 5771off(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, callback?: Callback<AccelerometerUncalibratedResponse>): void 5772 5773取消订阅传感器数据。 5774 5775从API version 9 开始不再维护,建议使用[sensor.off.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9-2)代替。 5776 5777**需要权限**:ohos.permission.ACCELEROMETER 5778 5779**系统能力**:SystemCapability.Sensors.Sensor 5780 5781**参数:** 5782 5783| 参数名 | 类型 | 必填 | 说明 | 5784| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5785| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 是 | 要取消订阅的未校准加速度计传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。 | 5786| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5787 5788**示例:** 5789 5790```ts 5791import sensor from '@ohos.sensor'; 5792 5793function callback(data: sensor.AccelerometerUncalibratedResponse) { 5794 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 5795 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 5796 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 5797 console.info('Succeeded in invoking off. X-coordinate bias: ' + data.biasX); 5798 console.info('Succeeded in invoking off. Y-coordinate bias: ' + data.biasY); 5799 console.info('Succeeded in invoking off. Z-coordinate bias: ' + data.biasZ); 5800} 5801 5802sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, callback); 5803``` 5804 5805### AMBIENT_LIGHT<sup>(deprecated)</sup> 5806 5807off(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback?: Callback<LightResponse>): void 5808 5809取消订阅传感器数据。 5810 5811从API version 9 开始不再维护,建议使用[sensor.off.AMBIENT_LIGHT](#ambient_light9-2)代替。 5812 5813**系统能力**:SystemCapability.Sensors.Sensor 5814 5815**参数:** 5816 5817| 参数名 | 类型 | 必填 | 说明 | 5818| -------- | ------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5819| type | [SensorType](#sensortype).SENSOR_TYPE_ID_AMBIENT_LIGHT | 是 | 要取消订阅的环境光传感器类型为SENSOR_TYPE_ID_AMBIENT_LIGHT。 | 5820| callback | Callback<[LightResponse](#lightresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5821 5822**示例:** 5823 5824```ts 5825import sensor from '@ohos.sensor'; 5826 5827function callback(data: sensor.LightResponse) { 5828 console.info('Succeeded in invoking off. Illumination: ' + data.intensity); 5829} 5830 5831sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); 5832``` 5833 5834### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 5835 5836off(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, callback?: Callback<AmbientTemperatureResponse>): void 5837 5838取消订阅传感器数据。 5839 5840从API version 9 开始不再维护,建议使用[sensor.off.AMBIENT_TEMPERATURE](#ambient_temperature9-2)代替。 5841 5842**系统能力**:SystemCapability.Sensors.Sensor 5843 5844**参数:** 5845 5846| 参数名 | 类型 | 必填 | 说明 | 5847| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5848| type | [SensorType](#sensortype).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 是 | 要取消订阅的环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。 | 5849| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5850 5851**示例:** 5852 5853```ts 5854import sensor from '@ohos.sensor'; 5855 5856function callback(data: sensor.AmbientTemperatureResponse) { 5857 console.info('Succeeded in invoking off. Temperature: ' + data.temperature); 5858} 5859 5860sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, callback); 5861``` 5862 5863### BAROMETER<sup>(deprecated)</sup> 5864 5865off(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback?: Callback<BarometerResponse>): void 5866 5867取消订阅传感器数据。 5868 5869从API version 9 开始不再维护,建议使用[sensor.off.BAROMETER](#barometer9-2)代替。 5870 5871**系统能力**:SystemCapability.Sensors.Sensor 5872 5873**参数:** 5874 5875| 参数名 | 类型 | 必填 | 说明 | 5876| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5877| type | [SensorType](#sensortype).SENSOR_TYPE_ID_BAROMETER | 是 | 要取消订阅的气压计传感器类型为SENSOR_TYPE_ID_BAROMETER。 | 5878| callback | Callback<[BarometerResponse](#barometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5879 5880**示例:** 5881 5882```ts 5883import sensor from '@ohos.sensor'; 5884 5885function callback(data: sensor.BarometerResponse) { 5886 console.info('Succeeded in invoking off. Atmospheric pressure: ' + data.pressure); 5887} 5888 5889sensor.off(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, callback); 5890``` 5891 5892### GRAVITY<sup>(deprecated)</sup> 5893 5894off(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback?: Callback<GravityResponse>): void 5895 5896取消订阅传感器数据。 5897 5898从API version 9 开始不再维护,建议使用[sensor.off.GRAVITY](#gravity9-2)代替。 5899 5900**系统能力**:SystemCapability.Sensors.Sensor 5901 5902**参数:** 5903 5904| 参数名 | 类型 | 必填 | 说明 | 5905| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 5906| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GRAVITY | 是 | 要取消订阅的重力传感器类型为SENSOR_TYPE_ID_GRAVITY。 | 5907| callback | Callback<[GravityResponse](#gravityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5908 5909**示例:** 5910 5911```ts 5912import sensor from '@ohos.sensor'; 5913 5914function callback(data: sensor.GravityResponse) { 5915 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 5916 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 5917 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 5918} 5919 5920sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, callback); 5921``` 5922 5923### GYROSCOPE<sup>(deprecated)</sup> 5924 5925off(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback?: Callback<GyroscopeResponse>): void 5926 5927取消订阅传感器数据。 5928 5929从API version 9 开始不再维护,建议使用[sensor.off.GYROSCOPE](#gyroscope9-2)代替。 5930 5931**需要权限**:ohos.permission.GYROSCOPE 5932 5933**系统能力**:SystemCapability.Sensors.Sensor 5934 5935**参数:** 5936 5937| 参数名 | 类型 | 必填 | 说明 | 5938| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5939| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GYROSCOPE | 是 | 要取消订阅的陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。 | 5940| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5941 5942**示例:** 5943 5944```ts 5945import sensor from '@ohos.sensor'; 5946 5947function callback(data: sensor.GyroscopeResponse) { 5948 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 5949 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 5950 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 5951} 5952 5953sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback); 5954``` 5955 5956### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 5957 5958off(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, callback?: Callback<GyroscopeUncalibratedResponse>): void 5959 5960取消订阅传感器数据。 5961 5962从API version 9 开始不再维护,建议使用[sensor.off.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9-2)代替。 5963 5964**需要权限**:ohos.permission.GYROSCOPE 5965 5966**系统能力**:SystemCapability.Sensors.Sensor 5967 5968**参数:** 5969 5970| 参数名 | 类型 | 必填 | 说明 | 5971| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5972| type | [SensorType](#sensortype).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 是 | 要取消订阅的未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。 | 5973| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 5974 5975**示例:** 5976 5977```ts 5978import sensor from '@ohos.sensor'; 5979 5980function callback(data: sensor.GyroscopeUncalibratedResponse) { 5981 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 5982 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 5983 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 5984} 5985 5986sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, callback); 5987``` 5988 5989### HALL<sup>(deprecated)</sup> 5990 5991off(type: SensorType.SENSOR_TYPE_ID_HALL, callback?: Callback<HallResponse>): void 5992 5993取消订阅传感器数据。 5994 5995从API version 9 开始不再维护,建议使用[sensor.off.HALL](#hall9-2)代替。 5996 5997**系统能力**:SystemCapability.Sensors.Sensor 5998 5999**参数:** 6000 6001| 参数名 | 类型 | 必填 | 说明 | 6002| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 6003| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HALL | 是 | 要取消订阅的霍尔传感器类型为SENSOR_TYPE_ID_HALL。 | 6004| callback | Callback<[HallResponse](#hallresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6005 6006**示例:** 6007 6008```ts 6009import sensor from '@ohos.sensor'; 6010 6011function callback(data: sensor.HallResponse) { 6012 console.info('Succeeded in invoking off. Status: ' + data.status); 6013} 6014 6015sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HALL, callback); 6016``` 6017 6018### HEART_RATE<sup>(deprecated)</sup> 6019 6020off(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback?: Callback<HeartRateResponse>): void 6021 6022取消订阅传感器数据。 6023 6024从API version 9 开始不再维护,建议使用[sensor.off.HEART_RATE](#heart_rate9-2)代替。 6025 6026**需要权限**:ohos.permission.HEALTH_DATA 6027 6028**系统能力**:SystemCapability.Sensors.Sensor 6029 6030**参数:** 6031 6032| 参数名 | 类型 | 必填 | 说明 | 6033| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 6034| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HEART_RATE | 是 | 要取消订阅的心率传感器类型为SENSOR_TYPE_ID_HEART_RATE。 | 6035| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6036 6037**示例:** 6038 6039```ts 6040import sensor from '@ohos.sensor'; 6041 6042function callback(data: sensor.HeartRateResponse) { 6043 console.info('Succeeded in invoking off. Humidity: ' + data.heartRate); 6044} 6045 6046sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, callback); 6047``` 6048 6049### HUMIDITY<sup>(deprecated)</sup> 6050 6051off(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback?: Callback<HumidityResponse>): void 6052 6053取消订阅传感器数据。 6054 6055从API version 9 开始不再维护,建议使用[sensor.off.HUMIDITY](#humidity9-2)代替。 6056 6057**系统能力**:SystemCapability.Sensors.Sensor 6058 6059**参数:** 6060 6061| 参数名 | 类型 | 必填 | 说明 | 6062| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 6063| type | [SensorType](#sensortype).SENSOR_TYPE_ID_HUMIDITY | 是 | 要取消订阅的湿度传感器类型为SENSOR_TYPE_ID_HUMIDITY。 | 6064| callback | Callback<[HumidityResponse](#humidityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6065 6066**示例:** 6067 6068```ts 6069import sensor from '@ohos.sensor'; 6070 6071function callback(data: sensor.HumidityResponse) { 6072 console.info('Succeeded in invoking off. Humidity: ' + data.humidity); 6073} 6074 6075sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, callback); 6076``` 6077 6078### LINEAR_ACCELERATION<sup>(deprecated)</sup> 6079 6080off(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback?: Callback<LinearAccelerometerResponse>): void 6081 6082取消订阅传感器数据。 6083 6084从API version 9 开始不再维护,建议使用[sensor.off.LINEAR_ACCELEROMETER](#linear_accelerometer9-2)代替。 6085 6086**需要权限**:ohos.permission.ACCELEROMETER 6087 6088**系统能力**:SystemCapability.Sensors.Sensor 6089 6090**参数:** 6091 6092| 参数名 | 类型 | 必填 | 说明 | 6093| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6094| type | [SensorType](#sensortype).SENSOR_TYPE_ID_LINEAR_ACCELERATION | 是 | 要取消订阅的线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。 | 6095| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6096 6097**示例:** 6098 6099```ts 6100import sensor from '@ohos.sensor'; 6101 6102function callback(data: sensor.LinearAccelerometerResponse) { 6103 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 6104 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 6105 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 6106} 6107 6108sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback); 6109``` 6110 6111### MAGNETIC_FIELD<sup>(deprecated)</sup> 6112 6113 off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback?: Callback<MagneticFieldResponse>): void 6114 6115取消订阅传感器数据。 6116 6117从API version 9 开始不再维护,建议使用[sensor.off.MAGNETIC_FIELD](#magnetic_field9-2)代替。 6118 6119**系统能力**:SystemCapability.Sensors.Sensor 6120 6121**参数:** 6122 6123| 参数名 | 类型 | 必填 | 说明 | 6124| ---------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6125| type | [SensorType](#sensortype).SENSOR_TYPE_ID_MAGNETIC_FIELD | 是 | 要取消订阅的磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。 | 6126| callbackcallback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6127 6128**示例:** 6129 6130```ts 6131import sensor from '@ohos.sensor'; 6132 6133function callback(data: sensor.MagneticFieldResponse) { 6134 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 6135 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 6136 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 6137} 6138 6139sensor.off(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback); 6140``` 6141 6142### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 6143 6144 off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, callback?: Callback<MagneticFieldUncalibratedResponse>): void 6145 6146取消订阅传感器数据。 6147 6148从API version 9 开始不再维护,建议使用[sensor.off.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9-2)代替。 6149 6150**系统能力**:SystemCapability.Sensors.Sensor 6151 6152**参数:** 6153 6154| 参数名 | 类型 | 必填 | 说明 | 6155| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6156| type | [SensorType](#sensortype).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 是 | 要取消订阅的未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。 | 6157| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6158 6159**示例:** 6160 6161```ts 6162import sensor from '@ohos.sensor'; 6163 6164function callback(data: sensor.MagneticFieldUncalibratedResponse) { 6165 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 6166 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 6167 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 6168 console.info('Succeeded in invoking off. X-coordinate bias: ' + data.biasX); 6169 console.info('Succeeded in invoking off. Y-coordinate bias: ' + data.biasY); 6170 console.info('Succeeded in invoking off. Z-coordinate bias: ' + data.biasZ); 6171} 6172 6173sensor.off(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, callback); 6174``` 6175 6176### ORIENTATION<sup>(deprecated)</sup> 6177 6178 off(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback?: Callback<OrientationResponse>): void 6179 6180取消订阅传感器数据。 6181 6182从API version 9 开始不再维护,建议使用[sensor.off.ORIENTATION](#orientation9-2)代替。 6183 6184**系统能力**:SystemCapability.Sensors.Sensor 6185 6186**参数:** 6187 6188| 参数名 | 类型 | 必填 | 说明 | 6189| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 6190| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ORIENTATION | 是 | 要取消订阅的方向传感器类型为SENSOR_TYPE_ID_ORIENTATION | 6191| callback | Callback<[OrientationResponse](#orientationresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6192 6193**示例:** 6194 6195```ts 6196import sensor from '@ohos.sensor'; 6197 6198function callback(data: sensor.OrientationResponse) { 6199 console.info('Succeeded in invoking off. The device rotates at an angle around the X axis: ' + data.beta); 6200 console.info('Succeeded in invoking off. The device rotates at an angle around the Y axis: ' + data.gamma); 6201 console.info('Succeeded in invoking off. The device rotates at an angle around the Z axis: ' + data.alpha); 6202} 6203 6204sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, callback); 6205``` 6206 6207### PEDOMETER<sup>(deprecated)</sup> 6208 6209off(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback?: Callback<PedometerResponse>): void 6210 6211取消订阅传感器数据。 6212 6213从API version 9 开始不再维护,建议使用[sensor.off.PEDOMETER](#pedometer9-2)代替。 6214 6215**需要权限**:ohos.permission.ACTIVITY_MOTION 6216 6217**系统能力**:SystemCapability.Sensors.Sensor 6218 6219**参数:** 6220 6221| 参数名 | 类型 | 必填 | 说明 | 6222| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 6223| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PEDOMETER | 是 | 要取消订阅的计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。 | 6224| callback | Callback<[PedometerResponse](#pedometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6225 6226**示例:** 6227 6228```ts 6229import sensor from '@ohos.sensor'; 6230 6231function callback(data: sensor.PedometerResponse) { 6232 console.info('Succeeded in invoking off. Steps: ' + data.steps); 6233} 6234 6235sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, callback); 6236``` 6237 6238### PEDOMETER_DETECTION<sup>(deprecated)</sup> 6239 6240off(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback?: Callback<PedometerDetectionResponse>): void 6241 6242取消订阅传感器数据。 6243 6244从API version 9 开始不再维护,建议使用[sensor.off.PEDOMETER_DETECTION](#pedometer_detection9-2)代替。 6245 6246**需要权限**:ohos.permission.ACTIVITY_MOTION 6247 6248**系统能力**:SystemCapability.Sensors.Sensor 6249 6250**参数:** 6251 6252| 参数名 | 类型 | 必填 | 说明 | 6253| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6254| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PEDOMETER_DETECTION | 是 | 要取消订阅的计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。 | 6255| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6256 6257**示例:** 6258 6259```ts 6260import sensor from '@ohos.sensor'; 6261 6262function callback(data: sensor.PedometerDetectionResponse) { 6263 console.info('Succeeded in invoking off. Scalar data: ' + data.scalar); 6264} 6265 6266sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback); 6267``` 6268 6269### PROXIMITY<sup>(deprecated)</sup> 6270 6271off(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback?: Callback<ProximityResponse>): void 6272 6273取消订阅传感器数据。 6274 6275从API version 9 开始不再维护,建议使用[sensor.off.PROXIMITY](#proximity9-2)代替。 6276 6277**系统能力**:SystemCapability.Sensors.Sensor 6278 6279**参数:** 6280 6281| 参数名 | 类型 | 必填 | 说明 | 6282| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 6283| type | [SensorType](#sensortype).SENSOR_TYPE_ID_PROXIMITY | 是 | 要取消订阅的接近光传感器类型为SENSOR_TYPE_ID_PROXIMITY。 | 6284| callback | Callback<[ProximityResponse](#proximityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6285 6286**示例:** 6287 6288```ts 6289import sensor from '@ohos.sensor'; 6290 6291function callback(data: sensor.ProximityResponse) { 6292 console.info('Succeeded in invoking off. Distance: ' + data.distance); 6293} 6294 6295sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, callback); 6296``` 6297 6298### ROTATION_VECTOR<sup>(deprecated)</sup> 6299 6300off(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback?: Callback<RotationVectorResponse>): void 6301 6302取消订阅传感器数据。 6303 6304从API version 9 开始不再维护,建议使用[sensor.off.ROTATION_VECTOR](#rotation_vector9-2)代替。 6305 6306**系统能力**:SystemCapability.Sensors.Sensor 6307 6308**参数:** 6309 6310| 参数名 | 类型 | 必填 | 说明 | 6311| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6312| type | [SensorType](#sensortype).SENSOR_TYPE_ID_ROTATION_VECTOR | 是 | 要取消订阅的旋转矢量传感器类型为SENSOR_TYPE_ID_ROTATION_VECTOR。 | 6313| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6314 6315**示例:** 6316 6317```ts 6318import sensor from '@ohos.sensor'; 6319 6320function callback(data: sensor.RotationVectorResponse) { 6321 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 6322 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 6323 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 6324 console.info('Succeeded in invoking off. Scalar quantity: ' + data.w); 6325} 6326 6327sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 6328``` 6329 6330### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 6331 6332off(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback?: Callback<SignificantMotionResponse>): void 6333 6334取消订阅传感器数据。 6335 6336从API version 9 开始不再维护,建议使用[sensor.off.SIGNIFICANT_MOTION](#significant_motion9-2)代替。 6337 6338**系统能力**:SystemCapability.Sensors.Sensor 6339 6340**参数:** 6341 6342| 参数名 | 类型 | 必填 | 说明 | 6343| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6344| type | [SensorType](#sensortype).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 是 | 要取消订阅的大幅动作传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。 | 6345| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6346 6347**示例:** 6348 6349```ts 6350import sensor from '@ohos.sensor'; 6351 6352function callback(data: sensor.SignificantMotionResponse) { 6353 console.info('Succeeded in invoking off. Scalar data: ' + data.scalar); 6354} 6355 6356sensor.off(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback); 6357``` 6358 6359### WEAR_DETECTION<sup>(deprecated)</sup> 6360 6361off(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback?: Callback<WearDetectionResponse>): void 6362 6363取消订阅传感器数据。 6364 6365从API version 9 开始不再维护,建议使用[sensor.off.WEAR_DETECTION](#wear_detection9-2)代替。 6366 6367**系统能力**:SystemCapability.Sensors.Sensor 6368 6369**参数:** 6370 6371| 参数名 | 类型 | 必填 | 说明 | 6372| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6373| type | [SensorType](#sensortype).SENSOR_TYPE_ID_WEAR_DETECTION | 是 | 要取消订阅的佩戴检测传感器类型为SENSOR_TYPE_ID_WEAR_DETECTION。 | 6374| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 6375 6376**示例:** 6377 6378```ts 6379import sensor from '@ohos.sensor'; 6380 6381function accCallback(data: sensor.WearDetectionResponse) { 6382 console.info('Succeeded in invoking off. Wear status: ' + data.value); 6383} 6384 6385sensor.off(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, accCallback); 6386``` 6387 6388## sensor.transformCoordinateSystem<sup>(deprecated)</sup> 6389 6390transformCoordinateSystem(inRotationVector: Array<number>, coordinates: CoordinatesOptions, callback: AsyncCallback<Array<number>>): void 6391 6392旋转提供的旋转矩阵,使其可以以不同的方式表示坐标系,使用Callback异步方式返回结果。 6393 6394从API version 9 开始不再维护,建议使用[sensor.transformRotationMatrix](#sensortransformrotationmatrix9)代替。 6395 6396**系统能力**:SystemCapability.Sensors.Sensor 6397 6398**参数:** 6399 6400| 参数名 | 类型 | 必填 | 说明 | 6401| ---------------- | ----------------------------------------- | ---- | -------------------------- | 6402| inRotationVector | Array<number> | 是 | 表示旋转矩阵。 | 6403| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 表示坐标系方向。 | 6404| callback | AsyncCallback<Array<number>> | 是 | 异步返回转换后的旋转矩阵。 | 6405 6406**示例:** 6407 6408```ts 6409import sensor from '@ohos.sensor'; 6410import BusinessError from '@ohos.base'; 6411 6412sensor.transformCoordinateSystem([1, 0, 0, 0, 1, 0, 0, 0, 1], { x: 2, y: 3 }, 6413 (err: BusinessError.BusinessError, data: Array<number>) => { 6414 if (err) { 6415 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 6416 return; 6417 } 6418 console.info("Succeeded in starting Operation. Data obtained: " + data); 6419 for (let i = 0; i < data.length; i++) { 6420 console.info("Succeeded in getting transformCoordinateSystem data[ " + i + "] = " + data[i]); 6421 } 6422}) 6423``` 6424## sensor.transformCoordinateSystem<sup>(deprecated)</sup> 6425 6426transformCoordinateSystem(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>> 6427 6428旋转提供的旋转矩阵,使其可以以不同的方式表示坐标系,使用Promise异步方式返回结果。 6429 6430从API version 9 开始不再维护,建议使用[sensor.transformRotationMatrix](#sensortransformrotationmatrix9-1)代替。 6431 6432**系统能力**:SystemCapability.Sensors.Sensor 6433 6434**参数:** 6435 6436| 参数名 | 类型 | 必填 | 说明 | 6437| ---------------- | ---------------------------------------- | ---- | -------- | 6438| inRotationVector | Array<number> | 是 | 表示旋转矩阵。 | 6439| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 表示坐标系方向。 | 6440 6441**返回值:** 6442 6443| 类型 | 说明 | 6444| ---------------------------------- | ---------------------------------- | 6445| Promise<Array<number>> | 使用异步方式返回转换后的旋转矩阵。 | 6446 6447**示例:** 6448 6449```ts 6450import sensor from '@ohos.sensor'; 6451import BusinessError from '@ohos.base'; 6452 6453const promise = sensor.transformCoordinateSystem([1, 0, 0, 0, 1, 0, 0, 0, 1], { x: 2, y: 3 }); 6454promise.then((data: Array<number>) => { 6455 console.info("Succeeded in starting Operation"); 6456 for (let i = 0; i < data.length; i++) { 6457 console.info("Succeeded in getting transformCoordinateSystem data[ " + i + "] = " + data[i]); 6458 } 6459}).catch((err: BusinessError.BusinessError) => { 6460 console.error(`Failed to operate.`); 6461}) 6462``` 6463 6464## sensor.getGeomagneticField<sup>(deprecated)</sup> 6465 6466getGeomagneticField(locationOptions: LocationOptions, timeMillis: number, callback: AsyncCallback<GeomagneticResponse>): void 6467 6468获取地球上特定位置的地磁场,使用callback异步方式返回结果。 6469 6470从API version 9 开始不再维护,建议使用[sensor.getGeomagneticInfo](#sensorgetgeomagneticinfo9)代替。 6471 6472**系统能力**:SystemCapability.Sensors.Sensor 6473 6474**参数:** 6475 6476| 参数名 | 类型 | 必填 | 说明 | 6477| --------------- | ------------------------------------------------------------ | ---- | ---------------------------------- | 6478| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置。 | 6479| timeMillis | number | 是 | 表示获取磁偏角的时间,单位为毫秒。 | 6480| callback | AsyncCallback<[GeomagneticResponse](#geomagneticresponse)> | 是 | 异步返回磁场信息。 | 6481 6482**示例:** 6483 6484```ts 6485import sensor from '@ohos.sensor'; 6486import BusinessError from '@ohos.base'; 6487 6488sensor.getGeomagneticField({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000, 6489 (err: BusinessError.BusinessError, data: sensor.GeomagneticResponse) => { 6490 if (err) { 6491 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 6492 return; 6493 } 6494 console.info('Succeeded in getting sensor_getGeomagneticField_callback x: ' + data.x + ',y: ' + data.y + ',z: ' + 6495 data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + 6496 ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); 6497}); 6498``` 6499## sensor.getGeomagneticField<sup>(deprecated)</sup> 6500 6501getGeomagneticField(locationOptions: LocationOptions, timeMillis: number): Promise<GeomagneticResponse> 6502 6503获取地球上特定位置的地磁场,使用Promise异步方式返回结果。 6504 6505从API version 9 开始不再维护,建议使用[sensor.getGeomagneticInfo](#sensorgetgeomagneticinfo9-1)代替。 6506 6507**系统能力**:SystemCapability.Sensors.Sensor 6508 6509**参数:** 6510 6511| 参数名 | 类型 | 必填 | 说明 | 6512| --------------- | ----------------------------------- | ---- | ----------------- | 6513| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置。 | 6514| timeMillis | number | 是 | 表示获取磁偏角的时间,单位为毫秒。 | 6515 6516**返回值:** 6517| 类型 | 说明 | 6518| ---------------------------------------------------------- | -------------------------- | 6519| Promise<[GeomagneticResponse](#geomagneticresponse)> | 使用异步方式返回磁场信息。 | 6520 6521**示例:** 6522 6523```ts 6524import sensor from '@ohos.sensor'; 6525import BusinessError from '@ohos.base'; 6526 6527const promise = sensor.getGeomagneticField({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000); 6528promise.then((data: sensor.GeomagneticResponse) => { 6529 console.info('Succeeded in getting sensor_getGeomagneticField_promise x: ' + data.x + ',y: ' + data.y + ',z: ' + 6530 data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + 6531 ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); 6532}).catch((reason: BusinessError.BusinessError) => { 6533 console.error(`Failed to operate.`); 6534}) 6535``` 6536 6537## sensor.getAltitude<sup>(deprecated)</sup> 6538 6539getAltitude(seaPressure: number, currentPressure: number, callback: AsyncCallback<number>): void 6540 6541根据气压值获取设备所在的海拔高度,使用Callback异步方式返回结果。 6542 6543从API version 9 开始不再维护,建议使用[sensor.getDeviceAltitude](#sensorgetdevicealtitude9)代替。 6544 6545**系统能力**:SystemCapability.Sensors.Sensor 6546 6547**参数:** 6548 6549| 参数名 | 类型 | 必填 | 说明 | 6550| --------------- | --------------------------- | ---- | -------------------------------------- | 6551| seaPressure | number | 是 | 表示海平面气压值,单位为hPa。 | 6552| currentPressure | number | 是 | 表示设备所在高度的气压值,单位为hPa。 | 6553| callback | AsyncCallback<number> | 是 | 异步返回设备所在的海拔高度,单位为米。 | 6554 6555**示例:** 6556 6557```ts 6558import sensor from '@ohos.sensor'; 6559import BusinessError from '@ohos.base'; 6560 6561sensor.getAltitude(0, 200, (err: BusinessError.BusinessError, data: number) => { 6562 if (err) { 6563 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 6564 return; 6565 } 6566 console.info("Succeeded in getting getAltitude interface get data: " + data); 6567}); 6568``` 6569 6570## sensor.getAltitude<sup>(deprecated)</sup> 6571 6572getAltitude(seaPressure: number, currentPressure: number): Promise<number> 6573 6574根据气压值获取设备所在的海拔高度,使用Promise异步方式返回结果。 6575 6576从API version 9 开始不再维护,建议使用[sensor.getDeviceAltitude](#sensorgetdevicealtitude9-1)代替。 6577 6578**系统能力**:SystemCapability.Sensors.Sensor 6579 6580**参数:** 6581 6582| 参数名 | 类型 | 必填 | 说明 | 6583| --------------- | ------ | ---- | -------------------- | 6584| seaPressure | number | 是 | 表示海平面气压值,单位为hPa。 | 6585| currentPressure | number | 是 | 表示设备所在高度的气压值,单位为hPa。 | 6586 6587**返回值:** 6588 6589| 类型 | 说明 | 6590| --------------------- | ------------------------------------------------ | 6591| Promise<number> | 使用异步方式返回设备所在的海拔高度(单位:米)。 | 6592 6593**示例:** 6594 6595```ts 6596import sensor from '@ohos.sensor'; 6597import BusinessError from '@ohos.base'; 6598 6599const promise = sensor.getAltitude(0, 200); 6600promise.then((data: number) => { 6601 console.info('Succeeded in getting sensor_getAltitude_Promise success', data); 6602}).catch((err: BusinessError.BusinessError) => { 6603 console.error(`Failed to operate.`); 6604}) 6605``` 6606 6607 6608## sensor.getGeomagneticDip<sup>(deprecated)</sup> 6609 6610getGeomagneticDip(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void 6611 6612根据倾斜矩阵计算地磁倾斜角,使用Callback异步方式返回结果。 6613 6614从API version 9 开始不再维护,建议使用[sensor.getInclination](#sensorgetinclination9)代替。 6615 6616**系统能力**:SystemCapability.Sensors.Sensor 6617 6618**参数:** 6619 6620| 参数名 | 类型 | 必填 | 说明 | 6621| ----------------- | --------------------------- | ---- | -------------------------------- | 6622| inclinationMatrix | Array<number> | 是 | 表示倾斜矩阵。 | 6623| callback | AsyncCallback<number> | 是 | 异步返回地磁倾斜角,单位为弧度。 | 6624 6625**示例:** 6626 6627```ts 6628import sensor from '@ohos.sensor'; 6629import BusinessError from '@ohos.base'; 6630 6631sensor.getGeomagneticDip([1, 0, 0, 0, 1, 0, 0, 0, 1], (err: BusinessError.BusinessError, data: number) => { 6632 if (err) { 6633 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 6634 return; 6635 } 6636 console.info("Succeeded in getting getGeomagneticDip interface get data: " + data); 6637}) 6638``` 6639 6640## sensor.getGeomagneticDip<sup>(deprecated)</sup> 6641 6642getGeomagneticDip(inclinationMatrix: Array<number>): Promise<number> 6643 6644根据倾斜矩阵计算地磁倾斜角,使用Promise异步方式返回结果。 6645 6646从API version 9 开始不再维护,建议使用[sensor.getInclination](#sensorgetinclination9-1)代替。 6647 6648**系统能力**:SystemCapability.Sensors.Sensor 6649 6650**参数:** 6651 6652| 参数名 | 类型 | 必填 | 说明 | 6653| ----------------- | ------------------- | ---- | ------- | 6654| inclinationMatrix | Array<number> | 是 | 表示倾斜矩阵。 | 6655 6656**返回值:** 6657 6658| 类型 | 说明 | 6659| --------------------- | ---------------------------------------- | 6660| Promise<number> | 使用异步方式返回地磁倾斜角,单位为弧度。 | 6661 6662**示例:** 6663 6664```ts 6665import sensor from '@ohos.sensor'; 6666import BusinessError from '@ohos.base'; 6667 6668const promise = sensor.getGeomagneticDip([1, 0, 0, 0, 1, 0, 0, 0, 1]); 6669promise.then((data: number) => { 6670 console.info('Succeeded in get GeomagneticDip_promise', data); 6671}).catch((err: BusinessError.BusinessError) => { 6672 console.error(`Failed to operate.`); 6673}) 6674``` 6675 6676## sensor. getAngleModify<sup>(deprecated)</sup> 6677 6678getAngleModify(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 6679 6680获取两个旋转矩阵之间的角度变化,使用Callback异步方式返回结果。 6681 6682从API version 9 开始不再维护,建议使用[sensor.getAngleVariation](#sensorgetanglevariation9)代替。 6683 6684**系统能力**:SystemCapability.Sensors.Sensor 6685 6686**参数:** 6687 6688| 参数名 | 类型 | 必填 | 说明 | 6689| --------------------- | ---------------------------------------- | ---- | ------------------------------------- | 6690| currentRotationMatrix | Array<number> | 是 | 表示当前旋转矩阵。 | 6691| preRotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 6692| callback | AsyncCallback<Array<number>> | 是 | 异步返回z、x、y轴方向的旋转角度变化。 | 6693 6694**示例:** 6695 6696```ts 6697import sensor from '@ohos.sensor'; 6698import BusinessError from '@ohos.base'; 6699 6700sensor.getAngleModify([1, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0.87, -0.50, 0, 0.50, 0.87], 6701 (err: BusinessError.BusinessError, data: Array<number>) => { 6702 if (err) { 6703 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 6704 return; 6705 } 6706 for (let i = 0; i < data.length; i++) { 6707 console.info("data[" + i + "]: " + data[i]); 6708 } 6709}) 6710``` 6711 6712## sensor. getAngleModify<sup>(deprecated)</sup> 6713 6714getAngleModify(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>> 6715 6716获取两个旋转矩阵之间的角度变化,使用Promise异步方式返回结果。 6717 6718从API version 9 开始不再维护,建议使用[sensor.getAngleVariation](#sensorgetanglevariation9-1)代替。 6719 6720**系统能力**:SystemCapability.Sensors.Sensor 6721 6722**参数:** 6723 6724| 参数名 | 类型 | 必填 | 说明 | 6725| --------------------- | ------------------- | ---- | --------- | 6726| currentRotationMatrix | Array<number> | 是 | 表示当前旋转矩阵。 | 6727| preRotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 6728 6729**返回值:** 6730 6731| 类型 | 说明 | 6732| ---------------------------------- | --------------------------------------------- | 6733| Promise<Array<number>> | 使用异步方式返回z、x、y轴方向的旋转角度变化。 | 6734 6735**示例:** 6736 6737```ts 6738import sensor from '@ohos.sensor'; 6739import BusinessError from '@ohos.base'; 6740 6741const 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]); 6742promise.then((data: Array<number>) => { 6743 console.info('Succeeded in getting AngleModify_promise.'); 6744 for (let i = 0; i < data.length; i++) { 6745 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 6746 } 6747}).catch((reason: BusinessError.BusinessError) => { 6748 let e: BusinessError.BusinessError = reason as BusinessError.BusinessError; 6749 console.info("Succeeded in getting promise::catch", e); 6750}) 6751``` 6752 6753## sensor.createRotationMatrix<sup>(deprecated)</sup> 6754 6755createRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 6756 6757将旋转矢量转换为旋转矩阵,使用Callback异步方式返回结果。 6758 6759从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9)代替。 6760 6761**系统能力**:SystemCapability.Sensors.Sensor 6762 6763**参数:** 6764 6765| 参数名 | 类型 | 必填 | 说明 | 6766| -------------- | ---------------------------------------- | ---- | ------------------ | 6767| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 6768| callback | AsyncCallback<Array<number>> | 是 | 异步返回旋转矩阵。 | 6769 6770**示例:** 6771 6772```ts 6773import sensor from '@ohos.sensor'; 6774import BusinessError from '@ohos.base'; 6775 6776sensor.createRotationMatrix([0.20046076, 0.21907, 0.73978853, 0.60376877], 6777 (err: BusinessError.BusinessError, data: Array<number>) => { 6778 if (err) { 6779 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 6780 return; 6781 } 6782 for (let i = 0; i < data.length; i++) { 6783 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 6784 } 6785}) 6786``` 6787 6788## sensor.createRotationMatrix<sup>(deprecated)</sup> 6789 6790createRotationMatrix(rotationVector: Array<number>): Promise<Array<number>> 6791 6792将旋转矢量转换为旋转矩阵,使用Promise异步方式返回结果。 6793 6794从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9-1)代替。 6795 6796**系统能力**:SystemCapability.Sensors.Sensor 6797 6798**参数:** 6799 6800| 参数名 | 类型 | 必填 | 说明 | 6801| -------------- | ------------------- | ---- | ------- | 6802| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 6803 6804**返回值:** 6805 6806| 类型 | 说明 | 6807| ---------------------------------- | -------------------------- | 6808| Promise<Array<number>> | 使用异步方式返回旋转矩阵。 | 6809 6810**示例:** 6811 6812 ```ts 6813import sensor from '@ohos.sensor'; 6814import BusinessError from '@ohos.base'; 6815 6816const promise = sensor.createRotationMatrix([0.20046076, 0.21907, 0.73978853, 0.60376877]); 6817promise.then((data: Array<number>) => { 6818 console.info('Succeeded in getting createRotationMatrix_promise'); 6819 for (let i = 0; i < data.length; i++) { 6820 console.info("data[" + i + "]: " + data[i]); 6821 } 6822}).catch((reason: BusinessError.BusinessError) => { 6823 console.info("Succeeded in getting promise::catch", reason); 6824}) 6825 ``` 6826 6827## sensor.createQuaternion<sup>(deprecated)</sup> 6828 6829createQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 6830 6831将旋转矢量转换为四元数,使用Callback异步方式返回结果。 6832 6833从API version 9 开始不再维护,建议使用[sensor.getQuaternion](#sensorgetquaternion9)代替。 6834 6835**系统能力**:SystemCapability.Sensors.Sensor 6836 6837**参数:** 6838 6839| 参数名 | 类型 | 必填 | 说明 | 6840| -------------- | ---------------------------------------- | ---- | ---------------- | 6841| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 6842| callback | AsyncCallback<Array<number>> | 是 | 异步返回四元数。 | 6843 6844**示例:** 6845 6846```ts 6847import sensor from '@ohos.sensor'; 6848import BusinessError from '@ohos.base'; 6849 6850sensor.createQuaternion([0.20046076, 0.21907, 0.73978853, 0.60376877], 6851 (err: BusinessError.BusinessError, data: Array<number>) => { 6852 if (err) { 6853 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 6854 return; 6855 } 6856 for (let i = 0; i < data.length; i++) { 6857 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 6858 } 6859}) 6860``` 6861 6862## sensor.createQuaternion<sup>(deprecated)</sup> 6863 6864createQuaternion(rotationVector: Array<number>): Promise<Array<number>> 6865 6866将旋转矢量转换为四元数,使用Promise异步方式返回结果。 6867 6868从API version 9 开始不再维护,建议使用[sensor.getQuaternion](#sensorgetquaternion9-1)代替。 6869 6870**系统能力**:SystemCapability.Sensors.Sensor 6871 6872**参数:** 6873 6874| 参数名 | 类型 | 必填 | 说明 | 6875| -------------- | ------------------- | ---- | ------- | 6876| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 6877 6878**返回值:** 6879 6880| 类型 | 说明 | 6881| ---------------------------------- | ------------------------ | 6882| Promise<Array<number>> | 使用异步方式返回四元数。 | 6883 6884**示例:** 6885 6886```ts 6887import sensor from '@ohos.sensor'; 6888import BusinessError from '@ohos.base'; 6889 6890const promise = sensor.createQuaternion([0.20046076, 0.21907, 0.73978853, 0.60376877]); 6891promise.then((data: Array<number>) => { 6892 console.info('Succeeded in getting createQuaternion_promise'); 6893 for (let i = 0; i < data.length; i++) { 6894 console.info("data[" + i + "]: " + data[i]); 6895 } 6896}).catch((err: BusinessError.BusinessError) => { 6897 console.info(`Failed to get promise.`); 6898}) 6899``` 6900 6901## sensor.getDirection<sup>(deprecated)</sup> 6902 6903getDirection(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 6904 6905根据旋转矩阵计算设备的方向,使用Callback异步方式返回结果。 6906 6907从API version 9 开始不再维护,建议使用[sensor.getOrientation](#sensorgetorientation9)代替。 6908 6909**系统能力**:SystemCapability.Sensors.Sensor 6910 6911**参数:** 6912 6913| 参数名 | 类型 | 必填 | 说明 | 6914| -------------- | ---------------------------------------- | ---- | ------------------------------------- | 6915| rotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 6916| callback | AsyncCallback<Array<number>> | 是 | 异步返回围绕z、x、y轴方向的旋转角度。 | 6917 6918**示例:** 6919 6920```ts 6921import sensor from '@ohos.sensor'; 6922import BusinessError from '@ohos.base'; 6923 6924sensor.getDirection([1, 0, 0, 0, 1, 0, 0, 0, 1], (err: BusinessError.BusinessError, data: Array<number>) => { 6925 if (err) { 6926 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 6927 return; 6928 } 6929 console.info("Succeeded in getting getDirection interface get data: " + data); 6930 for (let i = 1; i < data.length; i++) { 6931 console.info("Succeeded in getting sensor_getDirection_callback" + data[i]); 6932 } 6933}) 6934``` 6935 6936## sensor.getDirection<sup>(deprecated)</sup> 6937 6938getDirection(rotationMatrix: Array<number>): Promise<Array<number>> 6939 6940根据旋转矩阵计算设备的方向,使用Promise异步方式返回结果。 6941 6942从API version 9 开始不再维护,建议使用[sensor.getOrientation](#sensorgetorientation9-1)代替。 6943 6944**系统能力**:SystemCapability.Sensors.Sensor 6945 6946**参数:** 6947 6948| 参数名 | 类型 | 必填 | 说明 | 6949| -------------- | ------------------- | ---- | ------- | 6950| rotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 6951 6952**返回值:** 6953 6954| 类型 | 说明 | 6955| ---------------------------------- | --------------------------------------------- | 6956| Promise<Array<number>> | 使用异步方式返回围绕z、x、y轴方向的旋转角度。 | 6957 6958**示例:** 6959 6960```ts 6961import sensor from '@ohos.sensor'; 6962import BusinessError from '@ohos.base'; 6963 6964const promise = sensor.getDirection([1, 0, 0, 0, 1, 0, 0, 0, 1]); 6965promise.then((data: Array<number>) => { 6966 console.info('Succeeded in getting sensor_getAltitude_Promise', data); 6967 for (let i = 1; i < data.length; i++) { 6968 console.info("Succeeded in getting sensor_getDirection_promise" + data[i]); 6969 } 6970}).catch((err: BusinessError.BusinessError) => { 6971 console.info(`Failed to get promise.`); 6972}) 6973``` 6974 6975## sensor.createRotationMatrix<sup>(deprecated)</sup> 6976 6977createRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void 6978 6979根据重力矢量和地磁矢量计算旋转矩阵,使用Callback异步方式返回结果。 6980 6981从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9-2)代替。 6982 6983**系统能力**:SystemCapability.Sensors.Sensor 6984 6985**参数:** 6986 6987| 参数名 | 类型 | 必填 | 说明 | 6988| ----------- | ------------------------------------------------------------ | ---- | ------------------ | 6989| gravity | Array<number> | 是 | 表示重力向量。 | 6990| geomagnetic | Array<number> | 是 | 表示地磁矢量。 | 6991| callback | AsyncCallback<[RotationMatrixResponse](#rotationmatrixresponse)> | 是 | 异步返回旋转矩阵。 | 6992 6993**示例:** 6994 6995```ts 6996import sensor from '@ohos.sensor'; 6997import BusinessError from '@ohos.base'; 6998 6999sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444], 7000 (err: BusinessError.BusinessError, data: sensor.RotationMatrixResponse) => { 7001 if (err) { 7002 console.error(`Failed to get create rotationMatrix. Code: ${err.code}, message: ${err.message}`); 7003 return; 7004 } 7005 console.info(JSON.stringify(data)); 7006}) 7007``` 7008 7009## sensor.createRotationMatrix<sup>(deprecated)</sup> 7010 7011createRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>): Promise<RotationMatrixResponse> 7012 7013根据重力矢量和地磁矢量计算旋转矩阵,使用Promise异步方式返回结果。 7014 7015从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9-3)代替。 7016 7017**系统能力**:SystemCapability.Sensors.Sensor 7018 7019**参数:** 7020 7021| 参数名 | 类型 | 必填 | 说明 | 7022| ----------- | ------------------- | ---- | ------- | 7023| gravity | Array<number> | 是 | 表示重力向量。 | 7024| geomagnetic | Array<number> | 是 | 表示地磁矢量。 | 7025 7026**返回值:** 7027 7028| 类型 | 说明 | 7029| ------------------------------------------------------------ | -------------------------- | 7030| Promise<[RotationMatrixResponse](#rotationmatrixresponse)> | 使用异步方式返回旋转矩阵。 | 7031 7032**示例:** 7033 7034```ts 7035import sensor from '@ohos.sensor'; 7036import BusinessError from '@ohos.base'; 7037 7038const promise = sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444]); 7039promise.then((data: sensor.RotationMatrixResponse) => { 7040 console.info(JSON.stringify(data)); 7041}).catch((err: BusinessError.BusinessError) => { 7042 console.info(`Failed to get promise.`); 7043}) 7044``` 7045