1# @ohos.sensor (传感器) 2<!--Kit: Sensor Service Kit--> 3<!--Subsystem: Sensors--> 4<!--Owner: @dilligencer--> 5<!--Designer: @butterls--> 6<!--Tester: @murphy84--> 7<!--Adviser: @hu-zhiqiong--> 8 9sensor模块提供了获取传感器数据的能力,包括获取传感器属性列表,订阅传感器数据,以及一些通用的传感器算法。 10 11> **说明:** 12> 13> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。订阅前可使用[getSingleSensor](#sensorgetsinglesensor9)接口获取该传感器的信息,订阅传感器数据时确保on订阅和off取消订阅成对出现。 14 15 16## 导入模块 17 18```ts 19import { sensor } from '@kit.SensorServiceKit'; 20``` 21## sensor.on 22 23### ACCELEROMETER<sup>9+</sup> 24 25on(type: SensorId.ACCELEROMETER, callback: Callback<AccelerometerResponse>, options?: Options): void 26 27订阅加速度传感器数据。 28 29**需要权限**:ohos.permission.ACCELEROMETER 30 31**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 32 33**系统能力**:SystemCapability.Sensors.Sensor 34 35**参数**: 36 37| 参数名 | 类型 | 必填 | 说明 | 38| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 39| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 40| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerResponse。 | 41| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 42 43**错误码**: 44 45以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 46 47| 错误码ID | 错误信息 | 48| -------- | ------------------------------------------------------------ | 49| 201 | Permission denied. | 50| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 51| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 52 53**示例**: 54 55```ts 56import { sensor } from '@kit.SensorServiceKit'; 57import { BusinessError } from '@kit.BasicServicesKit'; 58 59// 使用try catch对可能出现的异常进行捕获 60try { 61 sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 62 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 63 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 64 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 65 }, { interval: 100000000 }); 66 setTimeout(() => { 67 sensor.off(sensor.SensorId.ACCELEROMETER); 68 }, 500); 69} catch (error) { 70 let e: BusinessError = error as BusinessError; 71 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 72} 73``` 74 75### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 76 77on(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void 78 79订阅未校准加速度传感器数据。 80 81**需要权限**:ohos.permission.ACCELEROMETER 82 83**系统能力**:SystemCapability.Sensors.Sensor 84 85**参数**: 86 87| 参数名 | 类型 | 必填 | 说明 | 88| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 89| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 90| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerUncalibratedResponse。 | 91| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 92 93**错误码**: 94 95以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 96 97| 错误码ID | 错误信息 | 98| -------- | ------------------------------------------------------------ | 99| 201 | Permission denied. | 100| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 101| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 102 103**示例**: 104 105```ts 106import { sensor } from '@kit.SensorServiceKit'; 107import { BusinessError } from '@kit.BasicServicesKit'; 108 109// 使用try catch对可能出现的异常进行捕获 110try { 111 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 112 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 113 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 114 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 115 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 116 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 117 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 118 }, { interval: 100000000 }); 119 setTimeout(() => { 120 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED); 121 }, 500); 122} catch (error) { 123 let e: BusinessError = error as BusinessError; 124 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 125} 126``` 127 128### AMBIENT_LIGHT<sup>9+</sup> 129 130on(type: SensorId.AMBIENT_LIGHT, callback: Callback<LightResponse>, options?: Options): void 131 132订阅环境光传感器数据。 133 134**系统能力**:SystemCapability.Sensors.Sensor 135 136**参数**: 137 138| 参数名 | 类型 | 必填 | 说明 | 139| -------- | ----------------------------------------------- | ---- | ----------------------------------------------------------- | 140| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 141| callback | Callback<[LightResponse](#lightresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LightResponse。 | 142| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 143 144**错误码**: 145 146以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 147 148| 错误码ID | 错误信息 | 149| -------- | ------------------------------------------------------------ | 150| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 151| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 152 153**示例**: 154 155```ts 156import { sensor } from '@kit.SensorServiceKit'; 157import { BusinessError } from '@kit.BasicServicesKit'; 158 159// 使用try catch对可能出现的异常进行捕获 160try { 161 sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => { 162 console.info('Succeeded in getting the ambient light intensity: ' + data.intensity); 163 }, { interval: 100000000 }); 164 setTimeout(() => { 165 sensor.off(sensor.SensorId.AMBIENT_LIGHT); 166 }, 500); 167} catch (error) { 168 let e: BusinessError = error as BusinessError; 169 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 170} 171``` 172 173### AMBIENT_TEMPERATURE<sup>9+</sup> 174 175on(type: SensorId.AMBIENT_TEMPERATURE, callback: Callback<AmbientTemperatureResponse>, options?: Options): void 176 177订阅温度传感器数据。 178 179**系统能力**:SystemCapability.Sensors.Sensor 180 181**参数**: 182 183| 参数名 | 类型 | 必填 | 说明 | 184| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 185| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 186| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AmbientTemperatureResponse。 | 187| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 188 189**错误码**: 190 191以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 192 193| 错误码ID | 错误信息 | 194| -------- | ------------------------------------------------------------ | 195| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 196| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 197 198**示例**: 199 200```ts 201import { sensor } from '@kit.SensorServiceKit'; 202import { BusinessError } from '@kit.BasicServicesKit'; 203 204// 使用try catch对可能出现的异常进行捕获 205try { 206 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 207 console.info('Succeeded in invoking on. Temperature: ' + data.temperature); 208 }, { interval: 100000000 }); 209 setTimeout(() => { 210 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE); 211 }, 500); 212} catch (error) { 213 let e: BusinessError = error as BusinessError; 214 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 215} 216``` 217 218### BAROMETER<sup>9+</sup> 219 220on(type: SensorId.BAROMETER, callback: Callback<BarometerResponse>, options?: Options): void 221 222订阅气压计传感器数据。 223 224**系统能力**:SystemCapability.Sensors.Sensor 225 226**参数**: 227 228| 参数名 | 类型 | 必填 | 说明 | 229| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 230| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 231| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为BarometerResponse。 | 232| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 233 234**错误码**: 235 236以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 237 238| 错误码ID | 错误信息 | 239| -------- | ------------------------------------------------------------ | 240| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 241| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 242 243**示例**: 244 245```ts 246import { sensor } from '@kit.SensorServiceKit'; 247import { BusinessError } from '@kit.BasicServicesKit'; 248 249// 使用try catch对可能出现的异常进行捕获 250try { 251 sensor.on(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => { 252 console.info('Succeeded in invoking on. Atmospheric pressure: ' + data.pressure); 253 }, { interval: 100000000 }); 254 setTimeout(() => { 255 sensor.off(sensor.SensorId.BAROMETER); 256 }, 500); 257} catch (error) { 258 let e: BusinessError = error as BusinessError; 259 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 260} 261``` 262 263### GRAVITY<sup>9+</sup> 264 265on(type: SensorId.GRAVITY, callback: Callback<GravityResponse>, options?: Options): void 266 267订阅重力传感器数据。 268 269**系统能力**:SystemCapability.Sensors.Sensor 270 271**参数**: 272 273| 参数名 | 类型 | 必填 | 说明 | 274| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------------- | 275| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 276| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GravityResponse。 | 277| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 278 279**错误码**: 280 281以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 282 283| 错误码ID | 错误信息 | 284| -------- | ------------------------------------------------------------ | 285| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 286| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 287 288**示例**: 289 290```ts 291import { sensor } from '@kit.SensorServiceKit'; 292import { BusinessError } from '@kit.BasicServicesKit'; 293 294// 使用try catch对可能出现的异常进行捕获 295try { 296 sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 297 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 298 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 299 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 300 }, { interval: 100000000 }); 301 setTimeout(() => { 302 sensor.off(sensor.SensorId.GRAVITY); 303 }, 500); 304} catch (error) { 305 let e: BusinessError = error as BusinessError; 306 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 307} 308``` 309 310### GYROSCOPE<sup>9+</sup> 311 312on(type: SensorId.GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void 313 314订阅校准的陀螺仪传感器数据。 315 316**需要权限**:ohos.permission.GYROSCOPE 317 318**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 319 320**系统能力**:SystemCapability.Sensors.Sensor 321 322**参数**: 323 324| 参数名 | 类型 | 必填 | 说明 | 325| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 326| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 327| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeResponse。 | 328| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 329 330**错误码**: 331 332以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 333 334| 错误码ID | 错误信息 | 335| -------- | ------------------------------------------------------------ | 336| 201 | Permission denied. | 337| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 338| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 339 340**示例**: 341 342```ts 343import { sensor } from '@kit.SensorServiceKit'; 344import { BusinessError } from '@kit.BasicServicesKit'; 345 346// 使用try catch对可能出现的异常进行捕获 347try { 348 sensor.on(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => { 349 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 350 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 351 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 352 }, { interval: 100000000 }); 353 setTimeout(() => { 354 sensor.off(sensor.SensorId.GYROSCOPE); 355 }, 500); 356} catch (error) { 357 let e: BusinessError = error as BusinessError; 358 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 359} 360``` 361 362### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 363 364on(type: SensorId.GYROSCOPE_UNCALIBRATED, callback: Callback<GyroscopeUncalibratedResponse>, 365 options?: Options): void 366 367订阅未校准陀螺仪传感器数据。 368 369**需要权限**:ohos.permission.GYROSCOPE 370 371**系统能力**:SystemCapability.Sensors.Sensor 372 373**参数**: 374 375| 参数名 | 类型 | 必填 | 说明 | 376| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 377| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 378| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeUncalibratedResponse。 | 379| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 380 381**错误码**: 382 383以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 384 385| 错误码ID | 错误信息 | 386| -------- | ------------------------------------------------------------ | 387| 201 | Permission denied. | 388| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 389| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 390 391**示例**: 392 393```ts 394import { sensor } from '@kit.SensorServiceKit'; 395import { BusinessError } from '@kit.BasicServicesKit'; 396 397// 使用try catch对可能出现的异常进行捕获 398try { 399 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 400 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 401 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 402 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 403 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 404 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 405 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 406 }, { interval: 100000000 }); 407 setTimeout(() => { 408 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED); 409 }, 500); 410} catch (error) { 411 let e: BusinessError = error as BusinessError; 412 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 413} 414 415``` 416 417### HALL<sup>9+</sup> 418 419on(type: SensorId.HALL, callback: Callback<HallResponse>, options?: Options): void 420 421订阅霍尔传感器数据。 422 423**系统能力**:SystemCapability.Sensors.Sensor 424 425**参数**: 426 427| 参数名 | 类型 | 必填 | 说明 | 428| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 429| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 430| callback | Callback<[HallResponse](#hallresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HallResponse。 | 431| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当霍尔事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 432 433**错误码**: 434 435以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 436 437| 错误码ID | 错误信息 | 438| -------- | ------------------------------------------------------------ | 439| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 440| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 441 442**示例**: 443 444```ts 445import { sensor } from '@kit.SensorServiceKit'; 446import { BusinessError } from '@kit.BasicServicesKit'; 447 448// 使用try catch对可能出现的异常进行捕获 449try { 450 sensor.on(sensor.SensorId.HALL, (data: sensor.HallResponse) => { 451 console.info('Succeeded in invoking on. Hall status: ' + data.status); 452 }, { interval: 100000000 }); 453 setTimeout(() => { 454 sensor.off(sensor.SensorId.HALL); 455 }, 500); 456} catch (error) { 457 let e: BusinessError = error as BusinessError; 458 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 459} 460 461``` 462 463### HEART_RATE<sup>9+</sup> 464 465on(type: SensorId.HEART_RATE, callback: Callback<HeartRateResponse>, options?: Options): void 466 467订阅心率传感器数据。 468 469**需要权限**:ohos.permission.READ_HEALTH_DATA 470 471**系统能力**:SystemCapability.Sensors.Sensor 472 473**参数**: 474 475| 参数名 | 类型 | 必填 | 说明 | 476| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 477| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 478| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HeartRateResponse。 | 479| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 480 481**错误码**: 482 483以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 484 485| 错误码ID | 错误信息 | 486| -------- | ------------------------------------------------------------ | 487| 201 | Permission denied. | 488| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 489| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 490 491**示例**: 492 493```ts 494import { sensor } from '@kit.SensorServiceKit'; 495import { BusinessError } from '@kit.BasicServicesKit'; 496 497// 使用try catch对可能出现的异常进行捕获 498try { 499 sensor.on(sensor.SensorId.HEART_RATE, (data: sensor.HeartRateResponse) => { 500 console.info('Succeeded in invoking on. Heart rate: ' + data.heartRate); 501 }, { interval: 100000000 }); 502 setTimeout(() => { 503 sensor.off(sensor.SensorId.HEART_RATE); 504 }, 500); 505} catch (error) { 506 let e: BusinessError = error as BusinessError; 507 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 508} 509``` 510 511### HUMIDITY<sup>9+</sup> 512 513on(type: SensorId.HUMIDITY, callback: Callback<HumidityResponse>, options?: Options): void 514 515订阅湿度传感器数据。 516 517**系统能力**:SystemCapability.Sensors.Sensor 518 519**参数**: 520 521| 参数名 | 类型 | 必填 | 说明 | 522| -------- | ----------------------------------------------------- | ---- | ----------------------------------------------------------- | 523| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 524| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HumidityResponse。 | 525| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 526 527**错误码**: 528 529以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 530 531| 错误码ID | 错误信息 | 532| -------- | ------------------------------------------------------------ | 533| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 534| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 535 536**示例**: 537 538```ts 539import { sensor } from '@kit.SensorServiceKit'; 540import { BusinessError } from '@kit.BasicServicesKit'; 541 542// 使用try catch对可能出现的异常进行捕获 543try { 544 sensor.on(sensor.SensorId.HUMIDITY, (data: sensor.HumidityResponse) => { 545 console.info('Succeeded in invoking on. Humidity: ' + data.humidity); 546 }, { interval: 100000000 }); 547 setTimeout(() => { 548 sensor.off(sensor.SensorId.HUMIDITY); 549 }, 500); 550} catch (error) { 551 let e: BusinessError = error as BusinessError; 552 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 553} 554``` 555 556### LINEAR_ACCELEROMETER<sup>9+</sup> 557 558on(type: SensorId.LINEAR_ACCELEROMETER, callback: Callback<LinearAccelerometerResponse>, 559 options?: Options): void 560 561订阅线性加速度传感器数据。 562 563**需要权限**:ohos.permission.ACCELEROMETER 564 565**系统能力**:SystemCapability.Sensors.Sensor 566 567**参数**: 568 569| 参数名 | 类型 | 必填 | 说明 | 570| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 571| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELEROMETER。 | 572| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LinearAccelerometerResponse。 | 573| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 574 575**错误码**: 576 577以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 578 579| 错误码ID | 错误信息 | 580| -------- | ------------------------------------------------------------ | 581| 201 | Permission denied. | 582| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 583| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 584 585**示例**: 586 587```ts 588import { sensor } from '@kit.SensorServiceKit'; 589import { BusinessError } from '@kit.BasicServicesKit'; 590 591// 使用try catch对可能出现的异常进行捕获 592try { 593 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, (data: sensor.LinearAccelerometerResponse) => { 594 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 595 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 596 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 597 }, { interval: 100000000 }); 598 setTimeout(() => { 599 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER); 600 }, 500); 601} catch (error) { 602 let e: BusinessError = error as BusinessError; 603 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 604} 605``` 606 607### MAGNETIC_FIELD<sup>9+</sup> 608 609on(type: SensorId.MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>, options?: Options): void 610 611订阅地磁传感器数据。 612 613**系统能力**:SystemCapability.Sensors.Sensor 614 615**参数**: 616 617| 参数名 | 类型 | 必填 | 说明 | 618| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 619| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 620| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldResponse。 | 621| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 622 623**错误码**: 624 625以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 626 627| 错误码ID | 错误信息 | 628| -------- | ------------------------------------------------------------ | 629| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 630| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 631 632**示例**: 633 634```ts 635import { sensor } from '@kit.SensorServiceKit'; 636import { BusinessError } from '@kit.BasicServicesKit'; 637 638// 使用try catch对可能出现的异常进行捕获 639try { 640 sensor.on(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 641 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 642 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 643 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 644 }, { interval: 100000000 }); 645 setTimeout(() => { 646 sensor.off(sensor.SensorId.MAGNETIC_FIELD); 647 }, 500); 648} catch (error) { 649 let e: BusinessError = error as BusinessError; 650 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 651} 652``` 653 654### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 655 656on(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void 657 658订阅未校准地磁传感器数据。 659 660**系统能力**:SystemCapability.Sensors.Sensor 661 662**参数**: 663 664| 参数名 | 类型 | 必填 | 说明 | 665| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 666| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 667| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldUncalibratedResponse。 | 668| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 669 670**错误码**: 671 672以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 673 674| 错误码ID | 错误信息 | 675| -------- | ------------------------------------------------------------ | 676| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 677| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 678 679**示例**: 680 681```ts 682import { sensor } from '@kit.SensorServiceKit'; 683import { BusinessError } from '@kit.BasicServicesKit'; 684 685// 使用try catch对可能出现的异常进行捕获 686try { 687 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 688 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 689 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 690 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 691 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 692 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 693 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 694 }, { interval: 100000000 }); 695 setTimeout(() => { 696 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED); 697 }, 500); 698} catch (error) { 699 let e: BusinessError = error as BusinessError; 700 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 701} 702``` 703 704### ORIENTATION<sup>9+</sup> 705 706on(type: SensorId.ORIENTATION, callback: Callback<OrientationResponse>, options?: Options): void 707 708订阅方向传感器数据。 709 710> **说明:** 711> 712> 调用本接口的应用或服务可以通过提示用户使用8字校准法来提高应用获取的方向传感器的精度,此传感器理论误差正负5度,具体的精度根据不同的驱动及算法实现可能存在差异。 713 714**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 715 716**系统能力**:SystemCapability.Sensors.Sensor 717 718**参数**: 719 720| 参数名 | 类型 | 必填 | 说明 | 721| -------- | ----------------------------------------------------------- | ---- | ----------------------------------------------------------- | 722| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 723| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 回调函数,异步上报的传感器数据固定为OrientationResponse。 | 724| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 725 726**错误码**: 727 728以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 729 730| 错误码ID | 错误信息 | 731| -------- | ------------------------------------------------------------ | 732| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 733| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 734 735**示例**: 736 737```ts 738import { sensor } from '@kit.SensorServiceKit'; 739import { BusinessError } from '@kit.BasicServicesKit'; 740 741// 使用try catch对可能出现的异常进行捕获 742try { 743 sensor.on(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => { 744 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 745 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 746 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 747 }, { interval: 100000000 }); 748 setTimeout(() => { 749 sensor.off(sensor.SensorId.ORIENTATION); 750 }, 500); 751} catch (error) { 752 let e: BusinessError = error as BusinessError; 753 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 754} 755``` 756 757### PEDOMETER<sup>9+</sup> 758 759on(type: SensorId.PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void 760 761订阅计步器传感器数据。计步传感器数据上报有一定延迟,延迟时间由具体的实现产品决定。 762 763**需要权限**:ohos.permission.ACTIVITY_MOTION 764 765**系统能力**:SystemCapability.Sensors.Sensor 766 767**参数**: 768 769| 参数名 | 类型 | 必填 | 说明 | 770| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 771| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 772| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerResponse。 | 773| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 774 775**错误码**: 776 777以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 778 779| 错误码ID | 错误信息 | 780| -------- | ------------------------------------------------------------ | 781| 201 | Permission denied. | 782| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 783| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 784 785**示例**: 786 787```ts 788import { sensor } from '@kit.SensorServiceKit'; 789import { BusinessError } from '@kit.BasicServicesKit'; 790 791// 使用try catch对可能出现的异常进行捕获 792try { 793 sensor.on(sensor.SensorId.PEDOMETER, (data: sensor.PedometerResponse) => { 794 console.info('Succeeded in invoking on. Step count: ' + data.steps); 795 }, { interval: 100000000 }); 796 setTimeout(() => { 797 sensor.off(sensor.SensorId.PEDOMETER); 798 }, 500); 799} catch (error) { 800 let e: BusinessError = error as BusinessError; 801 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 802} 803``` 804 805### PEDOMETER_DETECTION<sup>9+</sup> 806 807on(type: SensorId.PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, 808 options?: Options): void 809 810订阅计步检测器传感器数据。 811 812**需要权限**:ohos.permission.ACTIVITY_MOTION 813 814**系统能力**:SystemCapability.Sensors.Sensor 815 816**参数**: 817 818| 参数名 | 类型 | 必填 | 说明 | 819| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 820| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 821| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerDetectionResponse。 | 822| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 823 824**错误码**: 825 826以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 827 828| 错误码ID | 错误信息 | 829| -------- | ------------------------------------------------------------ | 830| 201 | Permission denied. | 831| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 832| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 833 834**示例**: 835 836```ts 837import { sensor } from '@kit.SensorServiceKit'; 838import { BusinessError } from '@kit.BasicServicesKit'; 839 840// 使用try catch对可能出现的异常进行捕获 841try { 842 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 843 console.info('Succeeded in invoking on. Pedometer scalar: ' + data.scalar); 844 }, { interval: 100000000 }); 845 setTimeout(() => { 846 sensor.off(sensor.SensorId.PEDOMETER_DETECTION); 847 }, 500); 848} catch (error) { 849 let e: BusinessError = error as BusinessError; 850 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 851} 852``` 853 854### PROXIMITY<sup>9+</sup> 855 856on(type: SensorId.PROXIMITY, callback: Callback<ProximityResponse>, options?: Options): void 857 858订阅接近光传感器数据。 859 860**系统能力**:SystemCapability.Sensors.Sensor 861 862**参数**: 863 864| 参数名 | 类型 | 必填 | 说明 | 865| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 866| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 867| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为ProximityResponse。 | 868| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当接近光事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 869 870**错误码**: 871 872以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 873 874| 错误码ID | 错误信息 | 875| -------- | ------------------------------------------------------------ | 876| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 877| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 878 879**示例**: 880 881```ts 882import { sensor } from '@kit.SensorServiceKit'; 883import { BusinessError } from '@kit.BasicServicesKit'; 884 885// 使用try catch对可能出现的异常进行捕获 886try { 887 sensor.on(sensor.SensorId.PROXIMITY, (data: sensor.ProximityResponse) => { 888 console.info('Succeeded in invoking on. Distance: ' + data.distance); 889 }, { interval: 100000000 }); 890 setTimeout(() => { 891 sensor.off(sensor.SensorId.PROXIMITY); 892 }, 500); 893} catch (error) { 894 let e: BusinessError = error as BusinessError; 895 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 896} 897``` 898 899### ROTATION_VECTOR<sup>9+</sup> 900 901on(type: SensorId.ROTATION_VECTOR, callback: Callback<RotationVectorResponse>, 902 options?: Options): void 903 904订阅旋转矢量传感器数据。 905 906**系统能力**:SystemCapability.Sensors.Sensor 907 908**参数**: 909 910| 参数名 | 类型 | 必填 | 说明 | 911| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 912| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 913| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 回调函数,异步上报的传感器数据固定为RotationVectorResponse。 | 914| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 915 916**错误码**: 917 918以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 919 920| 错误码ID | 错误信息 | 921| -------- | ------------------------------------------------------------ | 922| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 923| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 924 925**示例**: 926 927```ts 928import { sensor } from '@kit.SensorServiceKit'; 929import { BusinessError } from '@kit.BasicServicesKit'; 930 931// 使用try catch对可能出现的异常进行捕获 932try { 933 sensor.on(sensor.SensorId.ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 934 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 935 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 936 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 937 console.info('Succeeded in invoking on. Scalar quantity: ' + data.w); 938 }, { interval: 100000000 }); 939 setTimeout(() => { 940 sensor.off(sensor.SensorId.ROTATION_VECTOR); 941 }, 500); 942} catch (error) { 943 let e: BusinessError = error as BusinessError; 944 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 945} 946``` 947 948### SIGNIFICANT_MOTION<sup>9+</sup> 949 950on(type: SensorId.SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, 951 options?: Options): void 952 953订阅有效运动传感器数据。 954 955**系统能力**:SystemCapability.Sensors.Sensor 956 957**参数**: 958 959| 参数名 | 类型 | 必填 | 说明 | 960| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 961| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 962| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为SignificantMotionResponse。 | 963| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 964 965**错误码**: 966 967以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 968 969| 错误码ID | 错误信息 | 970| -------- | ------------------------------------------------------------ | 971| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 972| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 973 974**示例**: 975 976```ts 977import { sensor } from '@kit.SensorServiceKit'; 978import { BusinessError } from '@kit.BasicServicesKit'; 979 980// 使用try catch对可能出现的异常进行捕获 981try { 982 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 983 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 984 }, { interval: 100000000 }); 985 setTimeout(() => { 986 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION); 987 }, 500); 988} catch (error) { 989 let e: BusinessError = error as BusinessError; 990 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 991} 992``` 993 994### WEAR_DETECTION<sup>9+</sup> 995 996on(type: SensorId.WEAR_DETECTION, callback: Callback<WearDetectionResponse>, 997 options?: Options): void 998 999订阅佩戴检测传感器数据。 1000 1001**系统能力**:SystemCapability.Sensors.Sensor 1002 1003**参数**: 1004 1005| 参数名 | 类型 | 必填 | 说明 | 1006| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1007| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 1008| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为WearDetectionResponse。 | 1009| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 1010 1011**错误码**: 1012 1013以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1014 1015| 错误码ID | 错误信息 | 1016| -------- | ------------------------------------------------------------ | 1017| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3.Parameter verification failed. | 1018| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1019 1020**示例**: 1021 1022```ts 1023import { sensor } from '@kit.SensorServiceKit'; 1024import { BusinessError } from '@kit.BasicServicesKit'; 1025 1026// 使用try catch对可能出现的异常进行捕获 1027try { 1028 sensor.on(sensor.SensorId.WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 1029 console.info('Succeeded in invoking on. Wear status: ' + data.value); 1030 }, { interval: 100000000 }); 1031 setTimeout(() => { 1032 sensor.off(sensor.SensorId.WEAR_DETECTION); 1033 }, 500); 1034} catch (error) { 1035 let e: BusinessError = error as BusinessError; 1036 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 1037} 1038``` 1039 1040### sensorStatusChange<sup>19+</sup> 1041 1042on(type: 'sensorStatusChange', callback: Callback<SensorStatusEvent>): void 1043 1044监听传感器上线下线状态的变化,callback返回传感器状态事件数据。 1045 1046**系统能力**:SystemCapability.Sensors.Sensor 1047 1048**参数**: 1049 1050| 参数名 | 类型 | 必填 | 说明 | 1051| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1052| type | 'sensorStatusChange' | 是 | 固定传入'sensorStatusChange',状态监听固定参数。 | 1053| callback | Callback<[SensorStatusEvent](#sensorstatusevent19)> | 是 | 回调函数,异步上报的传感器事件数据SensorStatusEvent。 | 1054 1055**错误码**: 1056 1057以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1058 1059| 错误码ID | 错误信息 | 1060| -------- | ------------------------------------------------------------ | 1061| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1062 1063**示例**: 1064 1065```ts 1066import { sensor } from '@kit.SensorServiceKit'; 1067import { BusinessError } from '@kit.BasicServicesKit'; 1068 1069// 使用try catch对可能出现的异常进行捕获 1070try { 1071 sensor.on('sensorStatusChange', (data: sensor.SensorStatusEvent) => { 1072 console.info('sensorStatusChange : ' + JSON.stringify(data)); 1073 }); 1074 setTimeout(() => { 1075 sensor.off('sensorStatusChange'); 1076 }, 5000); 1077} catch (error) { 1078 let e: BusinessError = error as BusinessError; 1079 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 1080} 1081``` 1082 1083 1084## sensor.once<sup>9+</sup> 1085 1086### ACCELEROMETER<sup>9+</sup> 1087 1088once(type: SensorId.ACCELEROMETER, callback: Callback<AccelerometerResponse>): void 1089 1090获取一次加速度传感器数据。 1091 1092**需要权限**:ohos.permission.ACCELEROMETER 1093 1094**系统能力**:SystemCapability.Sensors.Sensor 1095 1096**参数**: 1097 1098| 参数名 | 类型 | 必填 | 说明 | 1099| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1100| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 1101| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerResponse。 | 1102 1103**错误码**: 1104 1105以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1106 1107| 错误码ID | 错误信息 | 1108| -------- | ------------------------------------------------------------ | 1109| 201 | Permission denied. | 1110| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1111| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1112 1113**示例**: 1114 1115```ts 1116import { sensor } from '@kit.SensorServiceKit'; 1117import { BusinessError } from '@kit.BasicServicesKit'; 1118 1119// 使用try catch对可能出现的异常进行捕获 1120try { 1121 sensor.once(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 1122 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1123 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1124 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1125 }); 1126} catch (error) { 1127 let e: BusinessError = error as BusinessError; 1128 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1129} 1130``` 1131 1132### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 1133 1134once(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback: Callback<AccelerometerUncalibratedResponse>): void 1135 1136获取一次未校准加速度传感器数据。 1137 1138**需要权限**:ohos.permission.ACCELEROMETER 1139 1140**系统能力**:SystemCapability.Sensors.Sensor 1141 1142**参数**: 1143 1144| 参数名 | 类型 | 必填 | 说明 | 1145| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1146| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 1147| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AccelerometerUncalibratedResponse。 | 1148 1149**错误码**: 1150 1151以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1152 1153| 错误码ID | 错误信息 | 1154| -------- | ------------------------------------------------------------ | 1155| 201 | Permission denied. | 1156| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1157| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1158 1159**示例**: 1160 1161```ts 1162import { sensor } from '@kit.SensorServiceKit'; 1163import { BusinessError } from '@kit.BasicServicesKit'; 1164 1165// 使用try catch对可能出现的异常进行捕获 1166try { 1167 sensor.once(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 1168 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1169 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1170 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1171 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1172 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1173 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1174 }); 1175} catch (error) { 1176 let e: BusinessError = error as BusinessError; 1177 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1178} 1179``` 1180 1181### AMBIENT_LIGHT<sup>9+</sup> 1182 1183once(type: SensorId.AMBIENT_LIGHT, callback: Callback<LightResponse>): void 1184 1185获取一次环境光传感器数据。 1186 1187**系统能力**:SystemCapability.Sensors.Sensor 1188 1189**参数**: 1190 1191| 参数名 | 类型 | 必填 | 说明 | 1192| -------- | ----------------------------------------------- | ---- | --------------------------------------------------- | 1193| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 1194| callback | Callback<[LightResponse](#lightresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LightResponse。 | 1195 1196**错误码**: 1197 1198以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1199 1200| 错误码ID | 错误信息 | 1201| -------- | ------------------------------------------------------------ | 1202| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1203| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1204 1205**示例**: 1206 1207```ts 1208import { sensor } from '@kit.SensorServiceKit'; 1209import { BusinessError } from '@kit.BasicServicesKit'; 1210 1211// 使用try catch对可能出现的异常进行捕获 1212try { 1213 sensor.once(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => { 1214 console.info('Succeeded in invoking once. the ambient light intensity: ' + data.intensity); 1215 }); 1216} catch (error) { 1217 let e: BusinessError = error as BusinessError; 1218 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1219} 1220``` 1221 1222### AMBIENT_TEMPERATURE<sup>9+</sup> 1223 1224once(type: SensorId.AMBIENT_TEMPERATURE, callback: Callback<AmbientTemperatureResponse>): void 1225 1226获取一次温度传感器数据。 1227 1228**系统能力**:SystemCapability.Sensors.Sensor 1229 1230**参数**: 1231 1232| 参数名 | 类型 | 必填 | 说明 | 1233| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1234| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 1235| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 回调函数,异步上报的传感器数据固定为AmbientTemperatureResponse。 | 1236 1237**错误码**: 1238 1239以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1240 1241| 错误码ID | 错误信息 | 1242| -------- | ------------------------------------------------------------ | 1243| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1244| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1245 1246**示例**: 1247 1248```ts 1249import { sensor } from '@kit.SensorServiceKit'; 1250import { BusinessError } from '@kit.BasicServicesKit'; 1251 1252// 使用try catch对可能出现的异常进行捕获 1253try { 1254 sensor.once(sensor.SensorId.AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 1255 console.info('Succeeded in invoking once. Temperature: ' + data.temperature); 1256 }); 1257} catch (error) { 1258 let e: BusinessError = error as BusinessError; 1259 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1260} 1261``` 1262 1263### BAROMETER<sup>9+</sup> 1264 1265once(type: SensorId.BAROMETER, callback: Callback<BarometerResponse>): void 1266 1267获取一次气压计传感器数据。 1268 1269**系统能力**:SystemCapability.Sensors.Sensor 1270 1271**参数**: 1272 1273| 参数名 | 类型 | 必填 | 说明 | 1274| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1275| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 1276| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为BarometerResponse。 | 1277 1278**错误码**: 1279 1280以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1281 1282| 错误码ID | 错误信息 | 1283| -------- | ------------------------------------------------------------ | 1284| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1285| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1286 1287**示例**: 1288 1289```ts 1290import { sensor } from '@kit.SensorServiceKit'; 1291import { BusinessError } from '@kit.BasicServicesKit'; 1292 1293// 使用try catch对可能出现的异常进行捕获 1294try { 1295 sensor.once(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => { 1296 console.info('Succeeded in invoking once. Atmospheric pressure: ' + data.pressure); 1297 }); 1298} catch (error) { 1299 let e: BusinessError = error as BusinessError; 1300 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1301} 1302``` 1303 1304### GRAVITY<sup>9+</sup> 1305 1306once(type: SensorId.GRAVITY, callback: Callback<GravityResponse>): void 1307 1308获取一次重力传感器数据。 1309 1310**系统能力**:SystemCapability.Sensors.Sensor 1311 1312**参数**: 1313 1314| 参数名 | 类型 | 必填 | 说明 | 1315| -------- | --------------------------------------------------- | ---- | ----------------------------------------------------- | 1316| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 1317| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GravityResponse。 | 1318 1319**错误码**: 1320 1321以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1322 1323| 错误码ID | 错误信息 | 1324| -------- | ------------------------------------------------------------ | 1325| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1326| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1327 1328**示例**: 1329 1330```ts 1331import { sensor } from '@kit.SensorServiceKit'; 1332import { BusinessError } from '@kit.BasicServicesKit'; 1333 1334// 使用try catch对可能出现的异常进行捕获 1335try { 1336 sensor.once(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => { 1337 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1338 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1339 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1340 }); 1341} catch (error) { 1342 let e: BusinessError = error as BusinessError; 1343 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1344} 1345``` 1346 1347### GYROSCOPE<sup>9+</sup> 1348 1349once(type: SensorId.GYROSCOPE, callback: Callback<GyroscopeResponse>): void 1350 1351获取一次陀螺仪传感器数据。 1352 1353**需要权限**:ohos.permission.GYROSCOPE 1354 1355**系统能力**:SystemCapability.Sensors.Sensor 1356 1357**参数**: 1358 1359| 参数名 | 类型 | 必填 | 说明 | 1360| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1361| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 1362| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeResponse。 | 1363 1364**错误码**: 1365 1366以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1367 1368| 错误码ID | 错误信息 | 1369| -------- | ------------------------------------------------------------ | 1370| 201 | Permission denied. | 1371| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1372| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1373 1374**示例**: 1375 1376```ts 1377import { sensor } from '@kit.SensorServiceKit'; 1378import { BusinessError } from '@kit.BasicServicesKit'; 1379 1380// 使用try catch对可能出现的异常进行捕获 1381try { 1382 sensor.once(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => { 1383 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1384 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1385 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1386 }); 1387} catch (error) { 1388 let e: BusinessError = error as BusinessError; 1389 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1390} 1391``` 1392 1393### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 1394 1395once(type: SensorId.GYROSCOPE_UNCALIBRATED, callback: Callback<GyroscopeUncalibratedResponse>): void 1396 1397获取一次未校准陀螺仪传感器数据。 1398 1399**需要权限**:ohos.permission.GYROSCOPE 1400 1401**系统能力**:SystemCapability.Sensors.Sensor 1402 1403**参数**: 1404 1405| 参数名 | 类型 | 必填 | 说明 | 1406| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1407| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 1408| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为GyroscopeUncalibratedResponse。 | 1409 1410**错误码**: 1411 1412以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1413 1414| 错误码ID | 错误信息 | 1415| -------- | ------------------------------------------------------------ | 1416| 201 | Permission denied. | 1417| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1418| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1419 1420**示例**: 1421 1422```ts 1423import { sensor } from '@kit.SensorServiceKit'; 1424import { BusinessError } from '@kit.BasicServicesKit'; 1425 1426// 使用try catch对可能出现的异常进行捕获 1427try { 1428 sensor.once(sensor.SensorId.GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 1429 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1430 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1431 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1432 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1433 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1434 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1435 }); 1436} catch (error) { 1437 let e: BusinessError = error as BusinessError; 1438 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1439} 1440``` 1441 1442### HALL<sup>9+</sup> 1443 1444once(type: SensorId.HALL, callback: Callback<HallResponse>): void 1445 1446获取一次霍尔传感器数据。 1447 1448**系统能力**:SystemCapability.Sensors.Sensor 1449 1450**参数**: 1451 1452| 参数名 | 类型 | 必填 | 说明 | 1453| -------- | --------------------------------------------- | ---- | -------------------------------------------------- | 1454| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 1455| callback | Callback<[HallResponse](#hallresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HallResponse。 | 1456 1457**错误码**: 1458 1459以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1460 1461| 错误码ID | 错误信息 | 1462| -------- | ------------------------------------------------------------ | 1463| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1464| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1465 1466**示例**: 1467 1468```ts 1469import { sensor } from '@kit.SensorServiceKit'; 1470import { BusinessError } from '@kit.BasicServicesKit'; 1471 1472// 使用try catch对可能出现的异常进行捕获 1473try { 1474 sensor.once(sensor.SensorId.HALL, (data: sensor.HallResponse) => { 1475 console.info('Succeeded in invoking once. Status: ' + data.status); 1476 }); 1477} catch (error) { 1478 let e: BusinessError = error as BusinessError; 1479 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1480} 1481``` 1482 1483### HEART_RATE<sup>9+</sup> 1484 1485once(type: SensorId.HEART_RATE, callback: Callback<HeartRateResponse>): void 1486 1487获取一次心率传感器数据。 1488 1489**需要权限**:ohos.permission.READ_HEALTH_DATA 1490 1491**系统能力**:SystemCapability.Sensors.Sensor 1492 1493**参数**: 1494 1495| 参数名 | 类型 | 必填 | 说明 | 1496| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1497| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 1498| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HeartRateResponse。 | 1499 1500**错误码**: 1501 1502以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1503 1504| 错误码ID | 错误信息 | 1505| -------- | ------------------------------------------------------------ | 1506| 201 | Permission denied. | 1507| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1508| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1509 1510**示例**: 1511 1512```ts 1513import { sensor } from '@kit.SensorServiceKit'; 1514import { BusinessError } from '@kit.BasicServicesKit'; 1515 1516// 使用try catch对可能出现的异常进行捕获 1517try { 1518 sensor.once(sensor.SensorId.HEART_RATE, (data: sensor.HeartRateResponse) => { 1519 console.info('Succeeded in invoking once. Heart rate: ' + data.heartRate); 1520 }); 1521} catch (error) { 1522 let e: BusinessError = error as BusinessError; 1523 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1524} 1525``` 1526 1527### HUMIDITY<sup>9+</sup> 1528 1529once(type: SensorId.HUMIDITY, callback: Callback<HumidityResponse>): void 1530 1531获取一次湿度传感器数据。 1532 1533**系统能力**:SystemCapability.Sensors.Sensor 1534 1535**参数**: 1536 1537| 参数名 | 类型 | 必填 | 说明 | 1538| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------ | 1539| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 1540| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为HumidityResponse。 | 1541 1542**错误码**: 1543 1544以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1545 1546| 错误码ID | 错误信息 | 1547| -------- | ------------------------------------------------------------ | 1548| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1549| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1550 1551**示例**: 1552 1553```ts 1554import { sensor } from '@kit.SensorServiceKit'; 1555import { BusinessError } from '@kit.BasicServicesKit'; 1556 1557// 使用try catch对可能出现的异常进行捕获 1558try { 1559 sensor.once(sensor.SensorId.HUMIDITY, (data: sensor.HumidityResponse) => { 1560 console.info('Succeeded in invoking once. Humidity: ' + data.humidity); 1561 }); 1562} catch (error) { 1563 let e: BusinessError = error as BusinessError; 1564 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1565} 1566``` 1567 1568### LINEAR_ACCELEROMETER<sup>9+</sup> 1569 1570once(type: SensorId.LINEAR_ACCELEROMETER, callback: Callback<LinearAccelerometerResponse>): void 1571 1572获取一次线性加速度传感器数据。 1573 1574**需要权限**:ohos.permission.ACCELEROMETER 1575 1576**系统能力**:SystemCapability.Sensors.Sensor 1577 1578**参数**: 1579 1580| 参数名 | 类型 | 必填 | 说明 | 1581| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1582| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELEROMETER。 | 1583| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为LinearAccelerometerResponse。 | 1584 1585**错误码**: 1586 1587以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1588 1589| 错误码ID | 错误信息 | 1590| -------- | ------------------------------------------------------------ | 1591| 201 | Permission denied. | 1592| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1593| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1594 1595**示例**: 1596 1597```ts 1598import { sensor } from '@kit.SensorServiceKit'; 1599import { BusinessError } from '@kit.BasicServicesKit'; 1600 1601// 使用try catch对可能出现的异常进行捕获 1602try { 1603 sensor.once(sensor.SensorId.LINEAR_ACCELEROMETER, (data: sensor.LinearAccelerometerResponse) => { 1604 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1605 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1606 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1607 }); 1608} catch (error) { 1609 let e: BusinessError = error as BusinessError; 1610 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1611} 1612``` 1613 1614### MAGNETIC_FIELD<sup>9+</sup> 1615 1616once(type: SensorId.MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>): void 1617 1618获取一次磁场传感器数据。 1619 1620**系统能力**:SystemCapability.Sensors.Sensor 1621 1622**参数**: 1623 1624| 参数名 | 类型 | 必填 | 说明 | 1625| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1626| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 1627| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldResponse。 | 1628 1629**错误码**: 1630 1631以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1632 1633| 错误码ID | 错误信息 | 1634| -------- | ------------------------------------------------------------ | 1635| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1636| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1637 1638**示例**: 1639 1640```ts 1641import { sensor } from '@kit.SensorServiceKit'; 1642import { BusinessError } from '@kit.BasicServicesKit'; 1643 1644// 使用try catch对可能出现的异常进行捕获 1645try { 1646 sensor.once(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 1647 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1648 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1649 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1650 }); 1651} catch (error) { 1652 let e: BusinessError = error as BusinessError; 1653 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1654} 1655``` 1656 1657### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 1658 1659once(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback: Callback<MagneticFieldUncalibratedResponse>): void 1660 1661获取一次未经校准的磁场传感器数据。 1662 1663**系统能力**:SystemCapability.Sensors.Sensor 1664 1665**参数**: 1666 1667| 参数名 | 类型 | 必填 | 说明 | 1668| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1669| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 1670| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 回调函数,异步上报的传感器数据固定为MagneticFieldUncalibratedResponse。 | 1671 1672**错误码**: 1673 1674以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1675 1676| 错误码ID | 错误信息 | 1677| -------- | ------------------------------------------------------------ | 1678| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1679| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1680 1681**示例**: 1682 1683```ts 1684import { sensor } from '@kit.SensorServiceKit'; 1685import { BusinessError } from '@kit.BasicServicesKit'; 1686 1687// 使用try catch对可能出现的异常进行捕获 1688try { 1689 sensor.once(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 1690 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1691 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1692 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1693 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 1694 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 1695 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 1696 }); 1697} catch (error) { 1698 let e: BusinessError = error as BusinessError; 1699 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1700} 1701``` 1702 1703### ORIENTATION<sup>9+</sup> 1704 1705once(type: SensorId.ORIENTATION, callback: Callback<OrientationResponse>): void 1706 1707获取一次方向传感器数据。 1708 1709**系统能力**:SystemCapability.Sensors.Sensor 1710 1711**参数**: 1712 1713| 参数名 | 类型 | 必填 | 说明 | 1714| -------- | ----------------------------------------------------------- | ---- | --------------------------------------------------------- | 1715| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 1716| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 回调函数,异步上报的传感器数据固定为OrientationResponse。 | 1717 1718**错误码**: 1719 1720以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1721 1722| 错误码ID | 错误信息 | 1723| -------- | ------------------------------------------------------------ | 1724| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1725| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1726 1727**示例**: 1728 1729```ts 1730import { sensor } from '@kit.SensorServiceKit'; 1731import { BusinessError } from '@kit.BasicServicesKit'; 1732 1733// 使用try catch对可能出现的异常进行捕获 1734try { 1735 sensor.once(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => { 1736 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 1737 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 1738 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 1739 }); 1740} catch (error) { 1741 let e: BusinessError = error as BusinessError; 1742 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1743} 1744``` 1745 1746### PEDOMETER<sup>9+</sup> 1747 1748once(type: SensorId.PEDOMETER, callback: Callback<PedometerResponse>): void 1749 1750获取一次计步器传感器数据。计步传感器数据上报有一定延迟,延迟时间由具体的实现产品决定。 1751 1752**需要权限**:ohos.permission.ACTIVITY_MOTION 1753 1754**系统能力**:SystemCapability.Sensors.Sensor 1755 1756**参数**: 1757 1758| 参数名 | 类型 | 必填 | 说明 | 1759| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1760| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 1761| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerResponse。 | 1762 1763**错误码**: 1764 1765以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1766 1767| 错误码ID | 错误信息 | 1768| -------- | ------------------------------------------------------------ | 1769| 201 | Permission denied. | 1770| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1771| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1772 1773**示例**: 1774 1775```ts 1776import { sensor } from '@kit.SensorServiceKit'; 1777import { BusinessError } from '@kit.BasicServicesKit'; 1778 1779// 使用try catch对可能出现的异常进行捕获 1780try { 1781 sensor.once(sensor.SensorId.PEDOMETER, (data: sensor.PedometerResponse) => { 1782 console.info('Succeeded in invoking once. Step count: ' + data.steps); 1783 }); 1784} catch (error) { 1785 let e: BusinessError = error as BusinessError; 1786 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1787} 1788``` 1789 1790### PEDOMETER_DETECTION<sup>9+</sup> 1791 1792once(type: SensorId.PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>): void 1793 1794获取一次计步检测器传感器数据。 1795 1796**系需要权限**:ohos.permission.ACTIVITY_MOTION 1797 1798**系统能力**:SystemCapability.Sensors.Sensor 1799 1800**参数**: 1801 1802| 参数名 | 类型 | 必填 | 说明 | 1803| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1804| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 1805| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为PedometerDetectionResponse。 | 1806 1807**错误码**: 1808 1809以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1810 1811| 错误码ID | 错误信息 | 1812| -------- | ------------------------------------------------------------ | 1813| 201 | Permission denied. | 1814| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1815| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1816 1817**示例**: 1818 1819```ts 1820import { sensor } from '@kit.SensorServiceKit'; 1821import { BusinessError } from '@kit.BasicServicesKit'; 1822 1823// 使用try catch对可能出现的异常进行捕获 1824try { 1825 sensor.once(sensor.SensorId.PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 1826 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 1827 }); 1828} catch (error) { 1829 let e: BusinessError = error as BusinessError; 1830 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1831} 1832``` 1833 1834### PROXIMITY<sup>9+</sup> 1835 1836once(type: SensorId.PROXIMITY, callback: Callback<ProximityResponse>): void 1837 1838获取一次接近光传感器数据。 1839 1840**系统能力**:SystemCapability.Sensors.Sensor 1841 1842**参数**: 1843 1844| 参数名 | 类型 | 必填 | 说明 | 1845| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------- | 1846| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 1847| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 回调函数,异步上报的传感器数据固定为ProximityResponse。 | 1848 1849**错误码**: 1850 1851以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1852 1853| 错误码ID | 错误信息 | 1854| -------- | ------------------------------------------------------------ | 1855| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1856| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1857 1858**示例**: 1859 1860```ts 1861import { sensor } from '@kit.SensorServiceKit'; 1862import { BusinessError } from '@kit.BasicServicesKit'; 1863 1864// 使用try catch对可能出现的异常进行捕获 1865try { 1866 sensor.once(sensor.SensorId.PROXIMITY, (data: sensor.ProximityResponse) => { 1867 console.info('Succeeded in invoking once. Distance: ' + data.distance); 1868 }); 1869} catch (error) { 1870 let e: BusinessError = error as BusinessError; 1871 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1872} 1873``` 1874 1875### ROTATION_VECTOR<sup>9+</sup> 1876 1877once(type: SensorId.ROTATION_VECTOR, callback: Callback<RotationVectorResponse>): void 1878 1879获取一次旋转矢量传感器数据。 1880 1881**系统能力**:SystemCapability.Sensors.Sensor 1882 1883**参数**: 1884 1885| 参数名 | 类型 | 必填 | 说明 | 1886| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1887| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 1888| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 回调函数,异步上报的传感器数据固定为RotationVectorResponse。 | 1889 1890**错误码**: 1891 1892以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1893 1894| 错误码ID | 错误信息 | 1895| -------- | ------------------------------------------------------------ | 1896| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1897| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1898 1899**示例**: 1900 1901```ts 1902import { sensor } from '@kit.SensorServiceKit'; 1903import { BusinessError } from '@kit.BasicServicesKit'; 1904 1905// 使用try catch对可能出现的异常进行捕获 1906try { 1907 sensor.once(sensor.SensorId.ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 1908 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 1909 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 1910 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 1911 console.info('Succeeded in invoking once. Scalar quantity: ' + data.w); 1912 }); 1913} catch (error) { 1914 let e: BusinessError = error as BusinessError; 1915 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1916} 1917``` 1918 1919### SIGNIFICANT_MOTION<sup>9+</sup> 1920 1921once(type: SensorId.SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>): void 1922 1923获取一次有效运动传感器数据。 1924 1925**系统能力**:SystemCapability.Sensors.Sensor 1926 1927**参数**: 1928 1929| 参数名 | 类型 | 必填 | 说明 | 1930| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1931| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 1932| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为SignificantMotionResponse。 | 1933 1934**错误码**: 1935 1936以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1937 1938| 错误码ID | 错误信息 | 1939| -------- | ------------------------------------------------------------ | 1940| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1941| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1942 1943**示例**: 1944 1945```ts 1946import { sensor } from '@kit.SensorServiceKit'; 1947import { BusinessError } from '@kit.BasicServicesKit'; 1948 1949// 使用try catch对可能出现的异常进行捕获 1950try { 1951 sensor.once(sensor.SensorId.SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 1952 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 1953 }); 1954} catch (error) { 1955 let e: BusinessError = error as BusinessError; 1956 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1957} 1958``` 1959 1960### WEAR_DETECTION<sup>9+</sup> 1961 1962once(type: SensorId.WEAR_DETECTION, callback: Callback<WearDetectionResponse>): void 1963 1964获取一次佩戴检测传感器数据。 1965 1966**系统能力**:SystemCapability.Sensors.Sensor 1967 1968**参数**: 1969 1970| 参数名 | 类型 | 必填 | 说明 | 1971| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1972| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 1973| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 回调函数,异步上报的传感器数据固定为WearDetectionResponse。 | 1974 1975**错误码**: 1976 1977以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1978 1979| 错误码ID | 错误信息 | 1980| -------- | ------------------------------------------------------------ | 1981| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1982| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 1983 1984**示例**: 1985 1986```ts 1987import { sensor } from '@kit.SensorServiceKit'; 1988import { BusinessError } from '@kit.BasicServicesKit'; 1989 1990// 使用try catch对可能出现的异常进行捕获 1991try { 1992 sensor.once(sensor.SensorId.WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 1993 console.info('Succeeded in invoking once. Wear status: ' + data.value); 1994 }); 1995} catch (error) { 1996 let e: BusinessError = error as BusinessError; 1997 console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`); 1998} 1999``` 2000 2001## sensor.off 2002 2003### ACCELEROMETER<sup>9+</sup> 2004 2005off(type: SensorId.ACCELEROMETER, callback?: Callback<AccelerometerResponse>): void 2006 2007取消订阅加速度传感器数据。 2008 2009**需要权限**:ohos.permission.ACCELEROMETER 2010 2011**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 2012 2013**系统能力**:SystemCapability.Sensors.Sensor 2014 2015**参数**: 2016 2017| 参数名 | 类型 | 必填 | 说明 | 2018| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2019| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 2020| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2021 2022**错误码**: 2023 2024以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2025 2026| 错误码ID | 错误信息 | 2027| -------- | ------------------------------------------------------------ | 2028| 201 | Permission denied. | 2029| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2030 2031**示例**: 2032 2033```ts 2034import { sensor } from '@kit.SensorServiceKit'; 2035import { BusinessError } from '@kit.BasicServicesKit'; 2036 2037function callback1(data: object) { 2038 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2039} 2040 2041function callback2(data: object) { 2042 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2043} 2044 2045// 使用try catch对可能出现的异常进行捕获 2046try { 2047 sensor.on(sensor.SensorId.ACCELEROMETER, callback1); 2048 sensor.on(sensor.SensorId.ACCELEROMETER, callback2); 2049 // 仅取消callback1的注册 2050 sensor.off(sensor.SensorId.ACCELEROMETER, callback1); 2051 // 取消SensorId.ACCELEROMETER类型的所有回调 2052 sensor.off(sensor.SensorId.ACCELEROMETER); 2053} catch (error) { 2054 let e: BusinessError = error as BusinessError; 2055 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2056} 2057``` 2058 2059### ACCELEROMETER<sup>19+</sup> 2060 2061off(type: SensorId.ACCELEROMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<AccelerometerResponse>): void 2062 2063取消订阅加速度传感器数据。 2064 2065**需要权限**:ohos.permission.ACCELEROMETER 2066 2067**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 2068 2069**系统能力**:SystemCapability.Sensors.Sensor 2070 2071**参数**: 2072 2073| 参数名 | 类型 | 必填 | 说明 | 2074|--------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2075| type | [SensorId](#sensorid9).ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER。 | 2076| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2077| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2078 2079**错误码**: 2080 2081以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2082 2083| 错误码ID | 错误信息 | 2084| -------- | ------------------------------------------------------------ | 2085| 201 | Permission denied. | 2086| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2087 2088**示例**: 2089 2090```ts 2091import { sensor } from '@kit.SensorServiceKit'; 2092import { BusinessError } from '@kit.BasicServicesKit'; 2093 2094enum Ret { OK, Failed = -1 } 2095 2096// 传感器回调 2097const sensorCallback = (response: sensor.AccelerometerResponse) => { 2098 console.info(`callback response: ${JSON.stringify(response)}`); 2099} 2100// 传感器监听类别 2101const sensorType = sensor.SensorId.ACCELEROMETER; 2102const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2103 2104function sensorSubscribe(): Ret { 2105 let ret: Ret = Ret.OK; 2106 // 使用try catch对可能出现的异常进行捕获 2107 try { 2108 // 查询所有的传感器 2109 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2110 if (!sensorList.length) { 2111 return Ret.Failed; 2112 } 2113 // 根据实际业务逻辑获取目标传感器。 2114 const targetSensor = sensorList 2115 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2116 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2117 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2118 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2119 if (!targetSensor) { 2120 return Ret.Failed; 2121 } 2122 sensorInfoParam.deviceId = targetSensor.deviceId; 2123 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2124 // 订阅传感器事件 2125 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2126 } catch (error) { 2127 let e: BusinessError = error as BusinessError; 2128 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2129 ret = Ret.Failed; 2130 } 2131 return ret; 2132} 2133 2134function sensorUnsubscribe(): Ret { 2135 let ret: Ret = Ret.OK; 2136 // 使用try catch对可能出现的异常进行捕获 2137 try { 2138 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2139 } catch (error) { 2140 let e: BusinessError = error as BusinessError; 2141 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2142 ret = Ret.Failed; 2143 } 2144 return ret; 2145} 2146``` 2147 2148### ACCELEROMETER_UNCALIBRATED<sup>9+</sup> 2149 2150off(type: SensorId.ACCELEROMETER_UNCALIBRATED, callback?: Callback<AccelerometerUncalibratedResponse>): void 2151 2152取消订阅未校准加速度传感器数据。 2153 2154**需要权限**:ohos.permission.ACCELEROMETER 2155 2156**系统能力**:SystemCapability.Sensors.Sensor 2157 2158**参数**: 2159 2160| 参数名 | 类型 | 必填 | 说明 | 2161| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2162| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 2163| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2164 2165**错误码**: 2166 2167以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2168 2169| 错误码ID | 错误信息 | 2170| -------- | ------------------------------------------------------------ | 2171| 201 | Permission denied. | 2172| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2173 2174**示例**: 2175 2176```ts 2177import { sensor } from '@kit.SensorServiceKit'; 2178import { BusinessError } from '@kit.BasicServicesKit'; 2179 2180function callback1(data: object) { 2181 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2182} 2183 2184function callback2(data: object) { 2185 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2186} 2187 2188// 使用try catch对可能出现的异常进行捕获 2189try { 2190 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback1); 2191 sensor.on(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback2); 2192 // 仅取消callback1的注册 2193 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED, callback1); 2194 // 取消注册SensorId.ACCELEROMETER_UNCALIBRATED类型的所有回调 2195 sensor.off(sensor.SensorId.ACCELEROMETER_UNCALIBRATED); 2196} catch (error) { 2197 let e: BusinessError = error as BusinessError; 2198 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2199} 2200``` 2201 2202### ACCELEROMETER_UNCALIBRATED<sup>19+</sup> 2203 2204off(type: SensorId.ACCELEROMETER_UNCALIBRATED, sensorInfoParam?: SensorInfoParam, callback?: Callback<AccelerometerUncalibratedResponse>): void 2205 2206取消订阅未校准加速度传感器数据。 2207 2208**需要权限**:ohos.permission.ACCELEROMETER 2209 2210**系统能力**:SystemCapability.Sensors.Sensor 2211 2212**参数**: 2213 2214| 参数名 | 类型 | 必填 | 说明 | 2215|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2216| type | [SensorId](#sensorid9).ACCELEROMETER_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.ACCELEROMETER_UNCALIBRATED。 | 2217| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2218| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2219 2220**错误码**: 2221 2222以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2223 2224| 错误码ID | 错误信息 | 2225| -------- | ------------------------------------------------------------ | 2226| 201 | Permission denied. | 2227| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2228 2229**示例**: 2230 2231```ts 2232import { sensor } from '@kit.SensorServiceKit'; 2233import { BusinessError } from '@kit.BasicServicesKit'; 2234 2235enum Ret { OK, Failed = -1 } 2236 2237// 传感器回调 2238const sensorCallback = (response: sensor.AccelerometerUncalibratedResponse) => { 2239 console.info(`callback response: ${JSON.stringify(response)}`); 2240} 2241// 传感器监听类型 2242const sensorType = sensor.SensorId.ACCELEROMETER_UNCALIBRATED; 2243const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2244 2245function sensorSubscribe(): Ret { 2246 let ret: Ret = Ret.OK; 2247 // 使用try catch对可能出现的异常进行捕获 2248 try { 2249 // 查询所有的传感器 2250 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2251 if (!sensorList.length) { 2252 return Ret.Failed; 2253 } 2254 // 根据实际业务逻辑获取目标传感器。 2255 const targetSensor = sensorList 2256 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2257 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2258 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2259 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2260 if (!targetSensor) { 2261 return Ret.Failed; 2262 } 2263 sensorInfoParam.deviceId = targetSensor.deviceId; 2264 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2265 // 订阅传感器事件 2266 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2267 } catch (error) { 2268 let e: BusinessError = error as BusinessError; 2269 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2270 ret = Ret.Failed; 2271 } 2272 return ret; 2273} 2274 2275function sensorUnsubscribe(): Ret { 2276 let ret: Ret = Ret.OK; 2277 // 使用try catch对可能出现的异常进行捕获 2278 try { 2279 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2280 } catch (error) { 2281 let e: BusinessError = error as BusinessError; 2282 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2283 ret = Ret.Failed; 2284 } 2285 return ret; 2286} 2287``` 2288 2289### AMBIENT_LIGHT<sup>9+</sup> 2290 2291off(type: SensorId.AMBIENT_LIGHT, callback?: Callback<LightResponse>): void 2292 2293取消订阅环境光传感器数据。 2294 2295**系统能力**:SystemCapability.Sensors.Sensor 2296 2297**参数**: 2298 2299| 参数名 | 类型 | 必填 | 说明 | 2300| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 2301| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 2302| callback | Callback<[LightResponse](#lightresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2303 2304**错误码**: 2305 2306以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2307 2308| 错误码ID | 错误信息 | 2309| -------- | ------------------------------------------------------------ | 2310| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2311 2312**示例**: 2313 2314```ts 2315import { sensor } from '@kit.SensorServiceKit'; 2316import { BusinessError } from '@kit.BasicServicesKit'; 2317 2318function callback1(data: object) { 2319 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2320} 2321 2322function callback2(data: object) { 2323 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2324} 2325 2326// 使用try catch对可能出现的异常进行捕获 2327try { 2328 sensor.on(sensor.SensorId.AMBIENT_LIGHT, callback1); 2329 sensor.on(sensor.SensorId.AMBIENT_LIGHT, callback2); 2330 // 仅取消callback1的注册 2331 sensor.off(sensor.SensorId.AMBIENT_LIGHT, callback1); 2332 // 取消注册SensorId.AMBIENT_LIGHT 2333 sensor.off(sensor.SensorId.AMBIENT_LIGHT); 2334} catch (error) { 2335 let e: BusinessError = error as BusinessError; 2336 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2337} 2338``` 2339 2340### AMBIENT_LIGHT<sup>19+</sup> 2341 2342off(type: SensorId.AMBIENT_LIGHT, sensorInfoParam?: SensorInfoParam, callback?: Callback<LightResponse>): void 2343 2344取消订阅环境光传感器数据。 2345 2346**系统能力**:SystemCapability.Sensors.Sensor 2347 2348**参数**: 2349 2350| 参数名 | 类型 | 必填 | 说明 | 2351|------------------| ----------------------------------------------- | ---- | ------------------------------------------------------------ | 2352| type | [SensorId](#sensorid9).AMBIENT_LIGHT | 是 | 传感器类型,该值固定为SensorId.AMBIENT_LIGHT。 | 2353| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2354| callback | Callback<[LightResponse](#lightresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2355 2356**错误码**: 2357 2358以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2359 2360| 错误码ID | 错误信息 | 2361| -------- | ------------------------------------------------------------ | 2362| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2363 2364**示例**: 2365 2366```ts 2367import { sensor } from '@kit.SensorServiceKit'; 2368import { BusinessError } from '@kit.BasicServicesKit'; 2369 2370enum Ret { OK, Failed = -1 } 2371 2372// 传感器回调 2373const sensorCallback = (response: sensor.LightResponse) => { 2374 console.info(`callback response: ${JSON.stringify(response)}`); 2375} 2376// 传感器监听类型 2377const sensorType = sensor.SensorId.AMBIENT_LIGHT; 2378const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2379 2380function sensorSubscribe(): Ret { 2381 let ret: Ret = Ret.OK; 2382 // 使用try catch对可能出现的异常进行捕获 2383 try { 2384 // 查询所有的传感器 2385 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2386 if (!sensorList.length) { 2387 return Ret.Failed; 2388 } 2389 // 根据实际业务逻辑获取目标传感器。 2390 const targetSensor = sensorList 2391 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2392 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2393 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2394 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2395 if (!targetSensor) { 2396 return Ret.Failed; 2397 } 2398 sensorInfoParam.deviceId = targetSensor.deviceId; 2399 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2400 // 订阅传感器事件 2401 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2402 } catch (error) { 2403 let e: BusinessError = error as BusinessError; 2404 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2405 ret = Ret.Failed; 2406 } 2407 return ret; 2408} 2409 2410function sensorUnsubscribe(): Ret { 2411 let ret: Ret = Ret.OK; 2412 // 使用try catch对可能出现的异常进行捕获 2413 try { 2414 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2415 } catch (error) { 2416 let e: BusinessError = error as BusinessError; 2417 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2418 ret = Ret.Failed; 2419 } 2420 return ret; 2421} 2422``` 2423 2424### AMBIENT_TEMPERATURE<sup>9+</sup> 2425 2426off(type: SensorId.AMBIENT_TEMPERATURE, callback?: Callback<AmbientTemperatureResponse>): void 2427 2428取消订阅温度传感器数据。 2429 2430**系统能力**:SystemCapability.Sensors.Sensor 2431 2432**参数**: 2433 2434| 参数名 | 类型 | 必填 | 说明 | 2435| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2436| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 2437| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2438 2439**错误码**: 2440 2441以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2442 2443| 错误码ID | 错误信息 | 2444| -------- | ------------------------------------------------------------ | 2445| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2446 2447**示例**: 2448 2449```ts 2450import { sensor } from '@kit.SensorServiceKit'; 2451import { BusinessError } from '@kit.BasicServicesKit'; 2452 2453function callback1(data: object) { 2454 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2455} 2456 2457function callback2(data: object) { 2458 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2459} 2460 2461// 使用try catch对可能出现的异常进行捕获 2462try { 2463 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, callback1); 2464 sensor.on(sensor.SensorId.AMBIENT_TEMPERATURE, callback2); 2465 // 仅取消callback1的注册 2466 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE, callback1); 2467 // 取消注册SensorId.AMBIENT_TEMPERATURE的所有回调 2468 sensor.off(sensor.SensorId.AMBIENT_TEMPERATURE); 2469} catch (error) { 2470 let e: BusinessError = error as BusinessError; 2471 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2472} 2473``` 2474 2475### AMBIENT_TEMPERATURE<sup>19+</sup> 2476 2477off(type: SensorId.AMBIENT_TEMPERATURE, sensorInfoParam?: SensorInfoParam, callback?: Callback<AmbientTemperatureResponse>): void 2478 2479取消订阅温度传感器数据。 2480 2481**系统能力**:SystemCapability.Sensors.Sensor 2482 2483**参数**: 2484 2485| 参数名 | 类型 | 必填 | 说明 | 2486|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2487| type | [SensorId](#sensorid9).AMBIENT_TEMPERATURE | 是 | 传感器类型,该值固定为SensorId.AMBIENT_TEMPERATURE。 | 2488| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2489| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2490 2491**错误码**: 2492 2493以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2494 2495| 错误码ID | 错误信息 | 2496| -------- | ------------------------------------------------------------ | 2497| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2498 2499**示例**: 2500 2501```ts 2502import { sensor } from '@kit.SensorServiceKit'; 2503import { BusinessError } from '@kit.BasicServicesKit'; 2504 2505enum Ret { OK, Failed = -1 } 2506 2507// 传感器回调 2508const sensorCallback = (response: sensor.AmbientTemperatureResponse) => { 2509 console.info(`callback response: ${JSON.stringify(response)}`); 2510} 2511// 传感器监听类型 2512const sensorType = sensor.SensorId.AMBIENT_TEMPERATURE; 2513const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2514 2515function sensorSubscribe(): Ret { 2516 let ret: Ret = Ret.OK; 2517 // 使用try catch对可能出现的异常进行捕获 2518 try { 2519 // 查询所有的传感器 2520 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2521 if (!sensorList.length) { 2522 return Ret.Failed; 2523 } 2524 // 根据实际业务逻辑获取目标传感器。 2525 const targetSensor = sensorList 2526 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2527 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2528 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2529 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2530 if (!targetSensor) { 2531 return Ret.Failed; 2532 } 2533 sensorInfoParam.deviceId = targetSensor.deviceId; 2534 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2535 // 订阅传感器事件 2536 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2537 } catch (error) { 2538 let e: BusinessError = error as BusinessError; 2539 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2540 ret = Ret.Failed; 2541 } 2542 return ret; 2543} 2544 2545function sensorUnsubscribe(): Ret { 2546 let ret: Ret = Ret.OK; 2547 // 使用try catch对可能出现的异常进行捕获 2548 try { 2549 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2550 } catch (error) { 2551 let e: BusinessError = error as BusinessError; 2552 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2553 ret = Ret.Failed; 2554 } 2555 return ret; 2556} 2557``` 2558 2559 2560### BAROMETER<sup>9+</sup> 2561 2562off(type: SensorId.BAROMETER, callback?: Callback<BarometerResponse>): void 2563 2564取消订阅气压计传感器数据。 2565 2566**系统能力**:SystemCapability.Sensors.Sensor 2567 2568**参数**: 2569 2570| 参数名 | 类型 | 必填 | 说明 | 2571| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2572| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 2573| callback | Callback<[BarometerResponse](#barometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2574 2575**错误码**: 2576 2577以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2578 2579| 错误码ID | 错误信息 | 2580| -------- | ------------------------------------------------------------ | 2581| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2582 2583**示例**: 2584 2585```ts 2586import { sensor } from '@kit.SensorServiceKit'; 2587import { BusinessError } from '@kit.BasicServicesKit'; 2588 2589function callback1(data: object) { 2590 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2591} 2592 2593function callback2(data: object) { 2594 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2595} 2596 2597// 使用try catch对可能出现的异常进行捕获 2598try { 2599 sensor.on(sensor.SensorId.BAROMETER, callback1); 2600 sensor.on(sensor.SensorId.BAROMETER, callback2); 2601 // 仅取消callback1的注册 2602 sensor.off(sensor.SensorId.BAROMETER, callback1); 2603 // 取消注册SensorId.BAROMETER的所有回调 2604 sensor.off(sensor.SensorId.BAROMETER); 2605} catch (error) { 2606 let e: BusinessError = error as BusinessError; 2607 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2608} 2609``` 2610 2611### BAROMETER<sup>19+</sup> 2612 2613off(type: SensorId.BAROMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<BarometerResponse>): void 2614 2615取消订阅气压计传感器数据。 2616 2617**系统能力**:SystemCapability.Sensors.Sensor 2618 2619**参数**: 2620 2621| 参数名 | 类型 | 必填 | 说明 | 2622|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2623| type | [SensorId](#sensorid9).BAROMETER | 是 | 传感器类型,该值固定为SensorId.BAROMETER。 | 2624| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2625| callback | Callback<[BarometerResponse](#barometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2626 2627**错误码**: 2628 2629以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2630 2631| 错误码ID | 错误信息 | 2632| -------- | ------------------------------------------------------------ | 2633| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2634 2635**示例**: 2636 2637```ts 2638import { sensor } from '@kit.SensorServiceKit'; 2639import { BusinessError } from '@kit.BasicServicesKit'; 2640 2641enum Ret { OK, Failed = -1 } 2642 2643// 传感器回调 2644const sensorCallback = (response: sensor.BarometerResponse) => { 2645 console.info(`callback response: ${JSON.stringify(response)}`); 2646} 2647// 传感器监听类型 2648const sensorType = sensor.SensorId.BAROMETER; 2649const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2650 2651function sensorSubscribe(): Ret { 2652 let ret: Ret = Ret.OK; 2653 // 使用try catch对可能出现的异常进行捕获 2654 try { 2655 // 查询所有的传感器 2656 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2657 if (!sensorList.length) { 2658 return Ret.Failed; 2659 } 2660 // 根据实际业务逻辑获取目标传感器。 2661 const targetSensor = sensorList 2662 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2663 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2664 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2665 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2666 if (!targetSensor) { 2667 return Ret.Failed; 2668 } 2669 sensorInfoParam.deviceId = targetSensor.deviceId; 2670 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2671 // 订阅传感器事件 2672 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2673 } catch (error) { 2674 let e: BusinessError = error as BusinessError; 2675 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2676 ret = Ret.Failed; 2677 } 2678 return ret; 2679} 2680 2681function sensorUnsubscribe(): Ret { 2682 let ret: Ret = Ret.OK; 2683 // 使用try catch对可能出现的异常进行捕获 2684 try { 2685 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2686 } catch (error) { 2687 let e: BusinessError = error as BusinessError; 2688 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2689 ret = Ret.Failed; 2690 } 2691 return ret; 2692} 2693``` 2694 2695### GRAVITY<sup>9+</sup> 2696 2697off(type: SensorId.GRAVITY, callback?: Callback<GravityResponse>): void 2698 2699取消订阅重力传感器数据。 2700 2701**系统能力**:SystemCapability.Sensors.Sensor 2702 2703**参数**: 2704 2705| 参数名 | 类型 | 必填 | 说明 | 2706| -------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 2707| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 2708| callback | Callback<[GravityResponse](#gravityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2709 2710**错误码**: 2711 2712以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2713 2714| 错误码ID | 错误信息 | 2715| -------- | ------------------------------------------------------------ | 2716| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2717 2718**示例**: 2719 2720```ts 2721import { sensor } from '@kit.SensorServiceKit'; 2722import { BusinessError } from '@kit.BasicServicesKit'; 2723 2724function callback1(data: object) { 2725 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2726} 2727 2728function callback2(data: object) { 2729 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2730} 2731 2732// 使用try catch对可能出现的异常进行捕获 2733try { 2734 sensor.on(sensor.SensorId.GRAVITY, callback1); 2735 sensor.on(sensor.SensorId.GRAVITY, callback2); 2736 // 仅取消callback1的注册 2737 sensor.off(sensor.SensorId.GRAVITY, callback1); 2738 // 取消注册SensorId.GRAVITY的所有回调 2739 sensor.off(sensor.SensorId.GRAVITY); 2740} catch (error) { 2741 let e: BusinessError = error as BusinessError; 2742 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2743} 2744 2745``` 2746 2747### GRAVITY<sup>19+</sup> 2748 2749off(type: SensorId.GRAVITY, sensorInfoParam?: SensorInfoParam, callback?: Callback<GravityResponse>): void 2750 2751取消订阅重力传感器数据。 2752 2753**系统能力**:SystemCapability.Sensors.Sensor 2754 2755**参数**: 2756 2757| 参数名 | 类型 | 必填 | 说明 | 2758|------------------| --------------------------------------------------- | ---- | ------------------------------------------------------------ | 2759| type | [SensorId](#sensorid9).GRAVITY | 是 | 传感器类型,该值固定为SensorId.GRAVITY。 | 2760| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2761| callback | Callback<[GravityResponse](#gravityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2762 2763**错误码**: 2764 2765以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2766 2767| 错误码ID | 错误信息 | 2768| -------- | ------------------------------------------------------------ | 2769| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2770 2771**示例**: 2772 2773```ts 2774import { sensor } from '@kit.SensorServiceKit'; 2775import { BusinessError } from '@kit.BasicServicesKit'; 2776 2777enum Ret { OK, Failed = -1 } 2778 2779// 传感器回调 2780const sensorCallback = (response: sensor.GravityResponse) => { 2781 console.info(`callback response: ${JSON.stringify(response)}`); 2782} 2783// 传感器监听类型 2784const sensorType = sensor.SensorId.GRAVITY; 2785const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2786 2787function sensorSubscribe(): Ret { 2788 let ret: Ret = Ret.OK; 2789 // 使用try catch对可能出现的异常进行捕获 2790 try { 2791 // 查询所有的传感器 2792 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2793 if (!sensorList.length) { 2794 return Ret.Failed; 2795 } 2796 // 根据实际业务逻辑获取目标传感器。 2797 const targetSensor = sensorList 2798 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2799 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2800 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2801 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2802 if (!targetSensor) { 2803 return Ret.Failed; 2804 } 2805 sensorInfoParam.deviceId = targetSensor.deviceId; 2806 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2807 // 订阅传感器事件 2808 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2809 } catch (error) { 2810 let e: BusinessError = error as BusinessError; 2811 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2812 ret = Ret.Failed; 2813 } 2814 return ret; 2815} 2816 2817function sensorUnsubscribe(): Ret { 2818 let ret: Ret = Ret.OK; 2819 // 使用try catch对可能出现的异常进行捕获 2820 try { 2821 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2822 } catch (error) { 2823 let e: BusinessError = error as BusinessError; 2824 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2825 ret = Ret.Failed; 2826 } 2827 return ret; 2828} 2829``` 2830 2831### GYROSCOPE<sup>9+</sup> 2832 2833off(type: SensorId.GYROSCOPE, callback?: Callback<GyroscopeResponse>): void 2834 2835取消订阅陀螺仪传感器数据。 2836 2837**需要权限**:ohos.permission.GYROSCOPE 2838 2839**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 2840 2841**系统能力**:SystemCapability.Sensors.Sensor 2842 2843**参数**: 2844 2845| 参数名 | 类型 | 必填 | 说明 | 2846| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2847| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 2848| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2849 2850**错误码**: 2851 2852以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2853 2854| 错误码ID | 错误信息 | 2855| -------- | ------------------------------------------------------------ | 2856| 201 | Permission denied. | 2857| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 2858 2859**示例**: 2860 2861```ts 2862import { sensor } from '@kit.SensorServiceKit'; 2863import { BusinessError } from '@kit.BasicServicesKit'; 2864 2865function callback1(data: object) { 2866 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 2867} 2868 2869function callback2(data: object) { 2870 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 2871} 2872 2873// 使用try catch对可能出现的异常进行捕获 2874try { 2875 sensor.on(sensor.SensorId.GYROSCOPE, callback1); 2876 sensor.on(sensor.SensorId.GYROSCOPE, callback2); 2877 // 仅取消callback1的注册 2878 sensor.off(sensor.SensorId.GYROSCOPE, callback1); 2879 // 取消注册SensorId.GYROSCOPE的所有回调 2880 sensor.off(sensor.SensorId.GYROSCOPE); 2881} catch (error) { 2882 let e: BusinessError = error as BusinessError; 2883 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 2884} 2885``` 2886 2887### GYROSCOPE<sup>19+</sup> 2888 2889off(type: SensorId.GYROSCOPE, sensorInfoParam?: SensorInfoParam, callback?: Callback<GyroscopeResponse>): void 2890 2891取消订阅陀螺仪传感器数据。 2892 2893**需要权限**:ohos.permission.GYROSCOPE 2894 2895**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 2896 2897**系统能力**:SystemCapability.Sensors.Sensor 2898 2899**参数**: 2900 2901| 参数名 | 类型 | 必填 | 说明 | 2902|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2903| type | [SensorId](#sensorid9).GYROSCOPE | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE。 | 2904| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 2905| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2906 2907**错误码**: 2908 2909以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。。 2910 2911| 错误码ID | 错误信息 | 2912| -------- | ------------------------------------------------------------ | 2913| 201 | Permission denied. | 2914| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 2915 2916**示例**: 2917 2918```ts 2919import { sensor } from '@kit.SensorServiceKit'; 2920import { BusinessError } from '@kit.BasicServicesKit'; 2921 2922enum Ret { OK, Failed = -1 } 2923 2924// 传感器回调 2925const sensorCallback = (response: sensor.GyroscopeResponse) => { 2926 console.info(`callback response: ${JSON.stringify(response)}`); 2927} 2928// 传感器监听类型 2929const sensorType = sensor.SensorId.GYROSCOPE; 2930const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 2931 2932function sensorSubscribe(): Ret { 2933 let ret: Ret = Ret.OK; 2934 // 使用try catch对可能出现的异常进行捕获 2935 try { 2936 // 查询所有的传感器 2937 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 2938 if (!sensorList.length) { 2939 return Ret.Failed; 2940 } 2941 // 根据实际业务逻辑获取目标传感器。 2942 const targetSensor = sensorList 2943 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 2944 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 2945 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 2946 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 2947 if (!targetSensor) { 2948 return Ret.Failed; 2949 } 2950 sensorInfoParam.deviceId = targetSensor.deviceId; 2951 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 2952 // 订阅传感器事件 2953 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 2954 } catch (error) { 2955 let e: BusinessError = error as BusinessError; 2956 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 2957 ret = Ret.Failed; 2958 } 2959 return ret; 2960} 2961 2962function sensorUnsubscribe(): Ret { 2963 let ret: Ret = Ret.OK; 2964 // 使用try catch对可能出现的异常进行捕获 2965 try { 2966 sensor.off(sensorType, sensorInfoParam, sensorCallback); 2967 } catch (error) { 2968 let e: BusinessError = error as BusinessError; 2969 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 2970 ret = Ret.Failed; 2971 } 2972 return ret; 2973} 2974``` 2975 2976### GYROSCOPE_UNCALIBRATED<sup>9+</sup> 2977 2978off(type: SensorId.GYROSCOPE_UNCALIBRATED, callback?: Callback<GyroscopeUncalibratedResponse>): void 2979 2980 取消订阅未校准陀螺仪传感器数据。 2981 2982**需要权限**:ohos.permission.GYROSCOPE 2983 2984**系统能力**:SystemCapability.Sensors.Sensor 2985 2986**参数**: 2987 2988| 参数名 | 类型 | 必填 | 说明 | 2989| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2990| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 2991| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 2992 2993**错误码**: 2994 2995以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 2996 2997| 错误码ID | 错误信息 | 2998| -------- | ------------------------------------------------------------ | 2999| 201 | Permission denied. | 3000| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3001 3002**示例**: 3003 3004```ts 3005import { sensor } from '@kit.SensorServiceKit'; 3006import { BusinessError } from '@kit.BasicServicesKit'; 3007 3008function callback1(data: object) { 3009 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3010} 3011 3012function callback2(data: object) { 3013 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3014} 3015 3016// 使用try catch对可能出现的异常进行捕获 3017try { 3018 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback1); 3019 sensor.on(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback2); 3020 // 仅取消callback1的注册 3021 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED, callback1); 3022 // 取消注册SensorId.GYROSCOPE_UNCALIBRATED的所有回调 3023 sensor.off(sensor.SensorId.GYROSCOPE_UNCALIBRATED); 3024} catch (error) { 3025 let e: BusinessError = error as BusinessError; 3026 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3027} 3028``` 3029 3030### GYROSCOPE_UNCALIBRATED<sup>19+</sup> 3031 3032off(type: SensorId.GYROSCOPE_UNCALIBRATED, sensorInfoParam?: SensorInfoParam, callback?: Callback<GyroscopeUncalibratedResponse>): void 3033 3034取消订阅未校准陀螺仪传感器数据。 3035 3036**需要权限**:ohos.permission.GYROSCOPE 3037 3038**系统能力**:SystemCapability.Sensors.Sensor 3039 3040**参数**: 3041 3042| 参数名 | 类型 | 必填 | 说明 | 3043|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3044| type | [SensorId](#sensorid9).GYROSCOPE_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.GYROSCOPE_UNCALIBRATED。 | 3045| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3046| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3047 3048**错误码**: 3049 3050以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。。 3051 3052| 错误码ID | 错误信息 | 3053| -------- | ------------------------------------------------------------ | 3054| 201 | Permission denied. | 3055| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3056 3057**示例**: 3058 3059```ts 3060import { sensor } from '@kit.SensorServiceKit'; 3061import { BusinessError } from '@kit.BasicServicesKit'; 3062 3063enum Ret { OK, Failed = -1 } 3064 3065// 传感器回调 3066const sensorCallback = (response: sensor.GyroscopeUncalibratedResponse) => { 3067 console.info(`callback response: ${JSON.stringify(response)}`); 3068} 3069// 传感器监听类型 3070const sensorType = sensor.SensorId.GYROSCOPE_UNCALIBRATED; 3071const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3072 3073function sensorSubscribe(): Ret { 3074 let ret: Ret = Ret.OK; 3075 // 使用try catch对可能出现的异常进行捕获 3076 try { 3077 // 查询所有的传感器 3078 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3079 if (!sensorList.length) { 3080 return Ret.Failed; 3081 } 3082 // 根据实际业务逻辑获取目标传感器。 3083 const targetSensor = sensorList 3084 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3085 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3086 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3087 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3088 if (!targetSensor) { 3089 return Ret.Failed; 3090 } 3091 sensorInfoParam.deviceId = targetSensor.deviceId; 3092 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3093 // 订阅传感器事件 3094 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3095 } catch (error) { 3096 let e: BusinessError = error as BusinessError; 3097 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3098 ret = Ret.Failed; 3099 } 3100 return ret; 3101} 3102 3103function sensorUnsubscribe(): Ret { 3104 let ret: Ret = Ret.OK; 3105 // 使用try catch对可能出现的异常进行捕获 3106 try { 3107 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3108 } catch (error) { 3109 let e: BusinessError = error as BusinessError; 3110 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3111 ret = Ret.Failed; 3112 } 3113 return ret; 3114} 3115``` 3116 3117### HALL<sup>9+</sup> 3118 3119off(type: SensorId.HALL, callback?: Callback<HallResponse>): void 3120 3121取消订阅霍尔传感器数据。 3122 3123**系统能力**:SystemCapability.Sensors.Sensor 3124 3125**参数**: 3126 3127| 参数名 | 类型 | 必填 | 说明 | 3128| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 3129| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 3130| callback | Callback<[HallResponse](#hallresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3131 3132**错误码**: 3133 3134以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3135 3136| 错误码ID | 错误信息 | 3137| -------- | ------------------------------------------------------------ | 3138| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3139 3140**示例**: 3141 3142```ts 3143import { sensor } from '@kit.SensorServiceKit'; 3144import { BusinessError } from '@kit.BasicServicesKit'; 3145 3146function callback1(data: object) { 3147 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3148} 3149 3150function callback2(data: object) { 3151 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3152} 3153 3154// 使用try catch对可能出现的异常进行捕获 3155try { 3156 sensor.on(sensor.SensorId.HALL, callback1); 3157 sensor.on(sensor.SensorId.HALL, callback2); 3158 // 仅取消callback1的注册 3159 sensor.off(sensor.SensorId.HALL, callback1); 3160 // 取消注册SensorId.HALL的所有回调 3161 sensor.off(sensor.SensorId.HALL); 3162} catch (error) { 3163 let e: BusinessError = error as BusinessError; 3164 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3165} 3166``` 3167 3168### HALL<sup>19+</sup> 3169 3170off(type: SensorId.HALL, sensorInfoParam?: SensorInfoParam, callback?: Callback<HallResponse>): void 3171 3172取消订阅霍尔传感器数据。 3173 3174**系统能力**:SystemCapability.Sensors.Sensor 3175 3176**参数**: 3177 3178| 参数名 | 类型 | 必填 | 说明 | 3179|------------------| --------------------------------------------- | ---- | ------------------------------------------------------------ | 3180| type | [SensorId](#sensorid9).HALL | 是 | 传感器类型,该值固定为SensorId.HALL。 | 3181| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3182| callback | Callback<[HallResponse](#hallresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3183 3184**错误码**: 3185 3186以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3187 3188| 错误码ID | 错误信息 | 3189| -------- | ------------------------------------------------------------ | 3190| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3191 3192**示例**: 3193 3194```ts 3195import { sensor } from '@kit.SensorServiceKit'; 3196import { BusinessError } from '@kit.BasicServicesKit'; 3197 3198enum Ret { OK, Failed = -1 } 3199 3200// 传感器回调 3201const sensorCallback = (response: sensor.HallResponse) => { 3202 console.info(`callback response: ${JSON.stringify(response)}`); 3203} 3204// 传感器监听类型 3205const sensorType = sensor.SensorId.HALL; 3206const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3207 3208function sensorSubscribe(): Ret { 3209 let ret: Ret = Ret.OK; 3210 // 使用try catch对可能出现的异常进行捕获 3211 try { 3212 // 查询所有的传感器 3213 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3214 if (!sensorList.length) { 3215 return Ret.Failed; 3216 } 3217 // 根据实际业务逻辑获取目标传感器。 3218 const targetSensor = sensorList 3219 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3220 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3221 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3222 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3223 if (!targetSensor) { 3224 return Ret.Failed; 3225 } 3226 sensorInfoParam.deviceId = targetSensor.deviceId; 3227 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3228 // 订阅传感器事件 3229 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3230 } catch (error) { 3231 let e: BusinessError = error as BusinessError; 3232 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3233 ret = Ret.Failed; 3234 } 3235 return ret; 3236} 3237 3238function sensorUnsubscribe(): Ret { 3239 let ret: Ret = Ret.OK; 3240 // 使用try catch对可能出现的异常进行捕获 3241 try { 3242 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3243 } catch (error) { 3244 let e: BusinessError = error as BusinessError; 3245 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3246 ret = Ret.Failed; 3247 } 3248 return ret; 3249} 3250``` 3251 3252### HEART_RATE<sup>9+</sup> 3253 3254off(type: SensorId.HEART_RATE, callback?: Callback<HeartRateResponse>): void 3255 3256取消订阅心率传感器数据。 3257 3258**需要权限**:ohos.permission.READ_HEALTH_DATA 3259 3260**系统能力**:SystemCapability.Sensors.Sensor 3261 3262**参数**: 3263 3264| 参数名 | 类型 | 必填 | 说明 | 3265| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3266| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 3267| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3268 3269**错误码**: 3270 3271以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3272 3273| 错误码ID | 错误信息 | 3274| -------- | ------------------------------------------------------------ | 3275| 201 | Permission denied. | 3276| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3277 3278**示例**: 3279 3280```ts 3281import { sensor } from '@kit.SensorServiceKit'; 3282import { BusinessError } from '@kit.BasicServicesKit'; 3283 3284function callback1(data: object) { 3285 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3286} 3287 3288function callback2(data: object) { 3289 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3290} 3291 3292// 使用try catch对可能出现的异常进行捕获 3293try { 3294 sensor.on(sensor.SensorId.HEART_RATE, callback1); 3295 sensor.on(sensor.SensorId.HEART_RATE, callback2); 3296 // 仅取消callback1的注册 3297 sensor.off(sensor.SensorId.HEART_RATE, callback1); 3298 // 取消注册SensorId.HEART_RATE的所有回调 3299 sensor.off(sensor.SensorId.HEART_RATE); 3300} catch (error) { 3301 let e: BusinessError = error as BusinessError; 3302 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3303} 3304``` 3305 3306### HEART_RATE<sup>19+</sup> 3307 3308off(type: SensorId.HEART_RATE, sensorInfoParam?: SensorInfoParam, callback?: Callback<HeartRateResponse>): void 3309 3310取消订阅心率传感器数据。 3311 3312**需要权限**:ohos.permission.READ_HEALTH_DATA 3313 3314**系统能力**:SystemCapability.Sensors.Sensor 3315 3316**参数**: 3317 3318| 参数名 | 类型 | 必填 | 说明 | 3319|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3320| type | [SensorId](#sensorid9).HEART_RATE | 是 | 传感器类型,该值固定为SensorId.HEART_RATE。 | 3321| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3322| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3323 3324**错误码**: 3325 3326以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3327 3328| 错误码ID | 错误信息 | 3329| -------- | ------------------------------------------------------------ | 3330| 201 | Permission denied. | 3331| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3332 3333**示例**: 3334 3335```ts 3336import { sensor } from '@kit.SensorServiceKit'; 3337import { BusinessError } from '@kit.BasicServicesKit'; 3338 3339enum Ret { OK, Failed = -1 } 3340 3341// 传感器回调 3342const sensorCallback = (response: sensor.HeartRateResponse) => { 3343 console.info(`callback response: ${JSON.stringify(response)}`); 3344} 3345// 传感器监听类型 3346const sensorType = sensor.SensorId.HEART_RATE; 3347const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3348 3349function sensorSubscribe(): Ret { 3350 let ret: Ret = Ret.OK; 3351 // 使用try catch对可能出现的异常进行捕获 3352 try { 3353 // 查询所有的传感器 3354 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3355 if (!sensorList.length) { 3356 return Ret.Failed; 3357 } 3358 // 根据实际业务逻辑获取目标传感器。 3359 const targetSensor = sensorList 3360 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3361 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3362 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3363 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3364 if (!targetSensor) { 3365 return Ret.Failed; 3366 } 3367 sensorInfoParam.deviceId = targetSensor.deviceId; 3368 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3369 // 订阅传感器事件 3370 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3371 } catch (error) { 3372 let e: BusinessError = error as BusinessError; 3373 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3374 ret = Ret.Failed; 3375 } 3376 return ret; 3377} 3378 3379function sensorUnsubscribe(): Ret { 3380 let ret: Ret = Ret.OK; 3381 // 使用try catch对可能出现的异常进行捕获 3382 try { 3383 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3384 } catch (error) { 3385 let e: BusinessError = error as BusinessError; 3386 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3387 ret = Ret.Failed; 3388 } 3389 return ret; 3390} 3391``` 3392 3393### HUMIDITY<sup>9+</sup> 3394 3395off(type: SensorId.HUMIDITY, callback?: Callback<HumidityResponse>): void 3396 3397取消订阅湿度传感器数据。 3398 3399**系统能力**:SystemCapability.Sensors.Sensor 3400 3401**参数**: 3402 3403| 参数名 | 类型 | 必填 | 说明 | 3404| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 3405| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 3406| callback | Callback<[HumidityResponse](#humidityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3407 3408**错误码**: 3409 3410以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3411 3412| 错误码ID | 错误信息 | 3413| -------- | ------------------------------------------------------------ | 3414| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3415 3416**示例**: 3417 3418```ts 3419import { sensor } from '@kit.SensorServiceKit'; 3420import { BusinessError } from '@kit.BasicServicesKit'; 3421 3422function callback1(data: object) { 3423 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3424} 3425 3426function callback2(data: object) { 3427 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3428} 3429 3430// 使用try catch对可能出现的异常进行捕获 3431try { 3432 sensor.on(sensor.SensorId.HUMIDITY, callback1); 3433 sensor.on(sensor.SensorId.HUMIDITY, callback2); 3434 // 仅取消callback1的注册 3435 sensor.off(sensor.SensorId.HUMIDITY, callback1); 3436 // 取消注册SensorId.HUMIDITY的所有回调 3437 sensor.off(sensor.SensorId.HUMIDITY); 3438} catch (error) { 3439 let e: BusinessError = error as BusinessError; 3440 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3441} 3442``` 3443 3444### HUMIDITY<sup>19+</sup> 3445 3446off(type: SensorId.HUMIDITY, sensorInfoParam?: SensorInfoParam, callback?: Callback<HumidityResponse>): void 3447 3448取消订阅湿度传感器数据。 3449 3450**系统能力**:SystemCapability.Sensors.Sensor 3451 3452**参数**: 3453 3454| 参数名 | 类型 | 必填 | 说明 | 3455|------------------| ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 3456| type | [SensorId](#sensorid9).HUMIDITY | 是 | 传感器类型,该值固定为SensorId.HUMIDITY。 | 3457| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3458| callback | Callback<[HumidityResponse](#humidityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3459 3460**错误码**: 3461 3462以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3463 3464| 错误码ID | 错误信息 | 3465| -------- | ------------------------------------------------------------ | 3466| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3467 3468**示例**: 3469 3470```ts 3471import { sensor } from '@kit.SensorServiceKit'; 3472import { BusinessError } from '@kit.BasicServicesKit'; 3473 3474enum Ret { OK, Failed = -1 } 3475 3476// 传感器回调 3477const sensorCallback = (response: sensor.HumidityResponse) => { 3478 console.info(`callback response: ${JSON.stringify(response)}`); 3479} 3480// 传感器监听类型 3481const sensorType = sensor.SensorId.HUMIDITY; 3482const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3483 3484function sensorSubscribe(): Ret { 3485 let ret: Ret = Ret.OK; 3486 // 使用try catch对可能出现的异常进行捕获 3487 try { 3488 // 查询所有的传感器 3489 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3490 if (!sensorList.length) { 3491 return Ret.Failed; 3492 } 3493 // 根据实际业务逻辑获取目标传感器。 3494 const targetSensor = sensorList 3495 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3496 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3497 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3498 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3499 if (!targetSensor) { 3500 return Ret.Failed; 3501 } 3502 sensorInfoParam.deviceId = targetSensor.deviceId; 3503 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3504 // 订阅传感器事件 3505 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3506 } catch (error) { 3507 let e: BusinessError = error as BusinessError; 3508 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3509 ret = Ret.Failed; 3510 } 3511 return ret; 3512} 3513 3514function sensorUnsubscribe(): Ret { 3515 let ret: Ret = Ret.OK; 3516 // 使用try catch对可能出现的异常进行捕获 3517 try { 3518 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3519 } catch (error) { 3520 let e: BusinessError = error as BusinessError; 3521 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3522 ret = Ret.Failed; 3523 } 3524 return ret; 3525} 3526``` 3527 3528### LINEAR_ACCELEROMETER<sup>9+</sup> 3529 3530off(type: SensorId.LINEAR_ACCELEROMETER, callback?: Callback<LinearAccelerometerResponse>): void 3531 3532取消订阅线性加速度传感器数据。 3533 3534**需要权限**:ohos.permission.ACCELEROMETER 3535 3536**系统能力**:SystemCapability.Sensors.Sensor 3537 3538**参数**: 3539 3540| 参数名 | 类型 | 必填 | 说明 | 3541| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3542| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELERATION。 | 3543| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3544 3545**错误码**: 3546 3547以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3548 3549| 错误码ID | 错误信息 | 3550| -------- | ------------------------------------------------------------ | 3551| 201 | Permission denied. | 3552| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3553 3554**示例**: 3555 3556```ts 3557import { sensor } from '@kit.SensorServiceKit'; 3558import { BusinessError } from '@kit.BasicServicesKit'; 3559 3560function callback1(data: object) { 3561 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3562} 3563 3564function callback2(data: object) { 3565 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3566} 3567 3568// 使用try catch对可能出现的异常进行捕获 3569try { 3570 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, callback1); 3571 sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, callback2); 3572 // 仅取消callback1的注册 3573 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER, callback1); 3574 // 取消注册SensorId.LINEAR_ACCELEROMETER的所有回调 3575 sensor.off(sensor.SensorId.LINEAR_ACCELEROMETER); 3576} catch (error) { 3577 let e: BusinessError = error as BusinessError; 3578 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3579} 3580``` 3581 3582### LINEAR_ACCELEROMETER<sup>19+</sup> 3583 3584off(type: SensorId.LINEAR_ACCELEROMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<LinearAccelerometerResponse>): void 3585 3586取消订阅线性加速度传感器数据。 3587 3588**需要权限**:ohos.permission.ACCELEROMETER 3589 3590**系统能力**:SystemCapability.Sensors.Sensor 3591 3592**参数**: 3593 3594| 参数名 | 类型 | 必填 | 说明 | 3595|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3596| type | [SensorId](#sensorid9).LINEAR_ACCELEROMETER | 是 | 传感器类型,该值固定为SensorId.LINEAR_ACCELERATION。 | 3597| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3598| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3599 3600**错误码**: 3601 3602以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3603 3604| 错误码ID | 错误信息 | 3605| -------- | ------------------------------------------------------------ | 3606| 201 | Permission denied. | 3607| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3608 3609**示例**: 3610 3611```ts 3612import { sensor } from '@kit.SensorServiceKit'; 3613import { BusinessError } from '@kit.BasicServicesKit'; 3614 3615enum Ret { OK, Failed = -1 } 3616 3617// 传感器回调 3618const sensorCallback = (response: sensor.LinearAccelerometerResponse) => { 3619 console.info(`callback response: ${JSON.stringify(response)}`); 3620} 3621// 传感器监听类型 3622const sensorType = sensor.SensorId.LINEAR_ACCELEROMETER; 3623const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3624 3625function sensorSubscribe(): Ret { 3626 let ret: Ret = Ret.OK; 3627 // 使用try catch对可能出现的异常进行捕获 3628 try { 3629 // 查询所有的传感器 3630 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3631 if (!sensorList.length) { 3632 return Ret.Failed; 3633 } 3634 // 根据实际业务逻辑获取目标传感器。 3635 const targetSensor = sensorList 3636 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3637 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3638 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3639 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3640 if (!targetSensor) { 3641 return Ret.Failed; 3642 } 3643 sensorInfoParam.deviceId = targetSensor.deviceId; 3644 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3645 // 订阅传感器事件 3646 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3647 } catch (error) { 3648 let e: BusinessError = error as BusinessError; 3649 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3650 ret = Ret.Failed; 3651 } 3652 return ret; 3653} 3654 3655function sensorUnsubscribe(): Ret { 3656 let ret: Ret = Ret.OK; 3657 // 使用try catch对可能出现的异常进行捕获 3658 try { 3659 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3660 } catch (error) { 3661 let e: BusinessError = error as BusinessError; 3662 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3663 ret = Ret.Failed; 3664 } 3665 return ret; 3666} 3667``` 3668 3669### MAGNETIC_FIELD<sup>9+</sup> 3670 3671off(type: SensorId.MAGNETIC_FIELD, callback?: Callback<MagneticFieldResponse>): void 3672 3673取消订阅磁场传感器数据。 3674 3675**系统能力**:SystemCapability.Sensors.Sensor 3676 3677**参数**: 3678 3679| 参数名 | 类型 | 必填 | 说明 | 3680| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3681| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 3682| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3683 3684**错误码**: 3685 3686以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3687 3688| 错误码ID | 错误信息 | 3689| -------- | ------------------------------------------------------------ | 3690| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3691 3692**示例**: 3693 3694```ts 3695import { sensor } from '@kit.SensorServiceKit'; 3696import { BusinessError } from '@kit.BasicServicesKit'; 3697 3698function callback1(data: object) { 3699 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3700} 3701 3702function callback2(data: object) { 3703 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3704} 3705 3706// 使用try catch对可能出现的异常进行捕获 3707try { 3708 sensor.on(sensor.SensorId.MAGNETIC_FIELD, callback1); 3709 sensor.on(sensor.SensorId.MAGNETIC_FIELD, callback2); 3710 // 仅取消callback1的注册 3711 sensor.off(sensor.SensorId.MAGNETIC_FIELD, callback1); 3712 // 取消注册SensorId.MAGNETIC_FIELD的所有回调 3713 sensor.off(sensor.SensorId.MAGNETIC_FIELD); 3714} catch (error) { 3715 let e: BusinessError = error as BusinessError; 3716 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3717} 3718``` 3719 3720### MAGNETIC_FIELD<sup>19+</sup> 3721 3722off(type: SensorId.MAGNETIC_FIELD, sensorInfoParam?: SensorInfoParam, callback?: Callback<MagneticFieldResponse>): void 3723 3724取消订阅磁场传感器数据。 3725 3726**系统能力**:SystemCapability.Sensors.Sensor 3727 3728**参数**: 3729 3730| 参数名 | 类型 | 必填 | 说明 | 3731|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3732| type | [SensorId](#sensorid9).MAGNETIC_FIELD | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD。 | 3733| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3734| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3735 3736**错误码**: 3737 3738以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3739 3740| 错误码ID | 错误信息 | 3741| -------- | ------------------------------------------------------------ | 3742| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3743 3744**示例**: 3745 3746```ts 3747import { sensor } from '@kit.SensorServiceKit'; 3748import { BusinessError } from '@kit.BasicServicesKit'; 3749 3750enum Ret { OK, Failed = -1 } 3751 3752// 传感器回调 3753const sensorCallback = (response: sensor.MagneticFieldResponse) => { 3754 console.info(`callback response: ${JSON.stringify(response)}`); 3755} 3756// 传感器监听类型 3757const sensorType = sensor.SensorId.MAGNETIC_FIELD; 3758const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3759 3760function sensorSubscribe(): Ret { 3761 let ret: Ret = Ret.OK; 3762 // 使用try catch对可能出现的异常进行捕获 3763 try { 3764 // 查询所有的传感器 3765 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3766 if (!sensorList.length) { 3767 return Ret.Failed; 3768 } 3769 // 根据实际业务逻辑获取目标传感器。 3770 const targetSensor = sensorList 3771 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3772 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3773 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3774 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3775 if (!targetSensor) { 3776 return Ret.Failed; 3777 } 3778 sensorInfoParam.deviceId = targetSensor.deviceId; 3779 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3780 // 订阅传感器事件 3781 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3782 } catch (error) { 3783 let e: BusinessError = error as BusinessError; 3784 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3785 ret = Ret.Failed; 3786 } 3787 return ret; 3788} 3789 3790function sensorUnsubscribe(): Ret { 3791 let ret: Ret = Ret.OK; 3792 // 使用try catch对可能出现的异常进行捕获 3793 try { 3794 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3795 } catch (error) { 3796 let e: BusinessError = error as BusinessError; 3797 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3798 ret = Ret.Failed; 3799 } 3800 return ret; 3801} 3802``` 3803 3804### MAGNETIC_FIELD_UNCALIBRATED<sup>9+</sup> 3805 3806off(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback?: Callback<MagneticFieldUncalibratedResponse>): void 3807 3808取消订阅未校准的磁场传感器数据。 3809 3810**系统能力**:SystemCapability.Sensors.Sensor 3811 3812**参数**: 3813 3814| 参数名 | 类型 | 必填 | 说明 | 3815| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3816| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 3817| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3818 3819**错误码**: 3820 3821以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3822 3823| 错误码ID | 错误信息 | 3824| -------- | ------------------------------------------------------------ | 3825| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3826 3827**示例**: 3828 3829```ts 3830import { sensor } from '@kit.SensorServiceKit'; 3831import { BusinessError } from '@kit.BasicServicesKit'; 3832 3833function callback1(data: object) { 3834 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3835} 3836 3837function callback2(data: object) { 3838 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3839} 3840 3841// 使用try catch对可能出现的异常进行捕获 3842try { 3843 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback1); 3844 sensor.on(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback2); 3845 // 仅取消callback1的注册 3846 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED, callback1); 3847 // 取消注册SensorId.MAGNETIC_FIELD_UNCALIBRATED的所有回调 3848 sensor.off(sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED); 3849} catch (error) { 3850 let e: BusinessError = error as BusinessError; 3851 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3852} 3853``` 3854 3855### MAGNETIC_FIELD_UNCALIBRATED<sup>19+</sup> 3856 3857off(type: SensorId.MAGNETIC_FIELD_UNCALIBRATED, sensorInfoParam?: SensorInfoParam, callback?: Callback<MagneticFieldUncalibratedResponse>): void 3858 3859取消订阅未校准的磁场传感器数据。 3860 3861**系统能力**:SystemCapability.Sensors.Sensor 3862 3863**参数**: 3864 3865| 参数名 | 类型 | 必填 | 说明 | 3866|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3867| type | [SensorId](#sensorid9).MAGNETIC_FIELD_UNCALIBRATED | 是 | 传感器类型,该值固定为SensorId.MAGNETIC_FIELD_UNCALIBRATED。 | 3868| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 3869| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3870 3871**错误码**: 3872 3873以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3874 3875| 错误码ID | 错误信息 | 3876| -------- | ------------------------------------------------------------ | 3877| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 3878 3879**示例**: 3880 3881```ts 3882import { sensor } from '@kit.SensorServiceKit'; 3883import { BusinessError } from '@kit.BasicServicesKit'; 3884 3885enum Ret { OK, Failed = -1 } 3886 3887// 传感器回调 3888const sensorCallback = (response: sensor.MagneticFieldUncalibratedResponse) => { 3889 console.info(`callback response: ${JSON.stringify(response)}`); 3890} 3891// 传感器监听类型 3892const sensorType = sensor.SensorId.MAGNETIC_FIELD_UNCALIBRATED; 3893const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 3894 3895function sensorSubscribe(): Ret { 3896 let ret: Ret = Ret.OK; 3897 // 使用try catch对可能出现的异常进行捕获 3898 try { 3899 // 查询所有的传感器 3900 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 3901 if (!sensorList.length) { 3902 return Ret.Failed; 3903 } 3904 // 根据实际业务逻辑获取目标传感器。 3905 const targetSensor = sensorList 3906 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 3907 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 3908 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 3909 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 3910 if (!targetSensor) { 3911 return Ret.Failed; 3912 } 3913 sensorInfoParam.deviceId = targetSensor.deviceId; 3914 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 3915 // 订阅传感器事件 3916 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 3917 } catch (error) { 3918 let e: BusinessError = error as BusinessError; 3919 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 3920 ret = Ret.Failed; 3921 } 3922 return ret; 3923} 3924 3925function sensorUnsubscribe(): Ret { 3926 let ret: Ret = Ret.OK; 3927 // 使用try catch对可能出现的异常进行捕获 3928 try { 3929 sensor.off(sensorType, sensorInfoParam, sensorCallback); 3930 } catch (error) { 3931 let e: BusinessError = error as BusinessError; 3932 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 3933 ret = Ret.Failed; 3934 } 3935 return ret; 3936} 3937``` 3938 3939### ORIENTATION<sup>9+</sup> 3940 3941off(type: SensorId.ORIENTATION, callback?: Callback<OrientationResponse>): void 3942 3943取消订阅方向传感器数据。 3944 3945**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 3946 3947**系统能力**:SystemCapability.Sensors.Sensor 3948 3949**参数**: 3950 3951| 参数名 | 类型 | 必填 | 说明 | 3952| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3953| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 3954| callback | Callback<[OrientationResponse](#orientationresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 3955 3956**错误码**: 3957 3958以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 3959 3960| 错误码ID | 错误信息 | 3961| -------- | ------------------------------------------------------------ | 3962| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 3963 3964**示例**: 3965 3966```ts 3967import { sensor } from '@kit.SensorServiceKit'; 3968import { BusinessError } from '@kit.BasicServicesKit'; 3969 3970function callback1(data: object) { 3971 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 3972} 3973 3974function callback2(data: object) { 3975 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 3976} 3977 3978// 使用try catch对可能出现的异常进行捕获 3979try { 3980 sensor.on(sensor.SensorId.ORIENTATION, callback1); 3981 sensor.on(sensor.SensorId.ORIENTATION, callback2); 3982 // 仅取消callback1的注册 3983 sensor.off(sensor.SensorId.ORIENTATION, callback1); 3984 // 取消注册SensorId.ORIENTATION的所有回调 3985 sensor.off(sensor.SensorId.ORIENTATION); 3986} catch (error) { 3987 let e: BusinessError = error as BusinessError; 3988 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 3989} 3990``` 3991 3992### ORIENTATION<sup>19+</sup> 3993 3994off(type: SensorId.ORIENTATION, sensorInfoParam?: SensorInfoParam, callback?: Callback<OrientationResponse>): void 3995 3996取消订阅方向传感器数据。 3997 3998**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 3999 4000**系统能力**:SystemCapability.Sensors.Sensor 4001 4002**参数**: 4003 4004| 参数名 | 类型 | 必填 | 说明 | 4005| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4006| type | [SensorId](#sensorid9).ORIENTATION | 是 | 传感器类型,该值固定为SensorId.ORIENTATION。 | 4007| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4008| callback | Callback<[OrientationResponse](#orientationresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4009 4010**错误码**: 4011 4012以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4013 4014| 错误码ID | 错误信息 | 4015| -------- | ------------------------------------------------------------ | 4016| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4017 4018**示例**: 4019 4020```ts 4021import { sensor } from '@kit.SensorServiceKit'; 4022import { BusinessError } from '@kit.BasicServicesKit'; 4023 4024enum Ret { OK, Failed = -1 } 4025 4026// 传感器回调 4027const sensorCallback = (response: sensor.OrientationResponse) => { 4028 console.info(`callback response: ${JSON.stringify(response)}`); 4029} 4030// 传感器监听类型 4031const sensorType = sensor.SensorId.ORIENTATION; 4032const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4033 4034function sensorSubscribe(): Ret { 4035 let ret: Ret = Ret.OK; 4036 // 使用try catch对可能出现的异常进行捕获 4037 try { 4038 // 查询所有的传感器 4039 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4040 if (!sensorList.length) { 4041 return Ret.Failed; 4042 } 4043 // 根据实际业务逻辑获取目标传感器。 4044 const targetSensor = sensorList 4045 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4046 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4047 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4048 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4049 if (!targetSensor) { 4050 return Ret.Failed; 4051 } 4052 sensorInfoParam.deviceId = targetSensor.deviceId; 4053 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4054 // 订阅传感器事件 4055 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4056 } catch (error) { 4057 let e: BusinessError = error as BusinessError; 4058 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4059 ret = Ret.Failed; 4060 } 4061 return ret; 4062} 4063 4064function sensorUnsubscribe(): Ret { 4065 let ret: Ret = Ret.OK; 4066 // 使用try catch对可能出现的异常进行捕获 4067 try { 4068 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4069 } catch (error) { 4070 let e: BusinessError = error as BusinessError; 4071 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4072 ret = Ret.Failed; 4073 } 4074 return ret; 4075} 4076``` 4077 4078### PEDOMETER<sup>9+</sup> 4079 4080off(type: SensorId.PEDOMETER, callback?: Callback<PedometerResponse>): void 4081 4082取消订阅计步器传感器数据。 4083 4084**需要权限**:ohos.permission.ACTIVITY_MOTION 4085 4086**系统能力**:SystemCapability.Sensors.Sensor 4087 4088**参数**: 4089 4090| 参数名 | 类型 | 必填 | 说明 | 4091| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4092| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 4093| callback | Callback<[PedometerResponse](#pedometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4094 4095**错误码**: 4096 4097以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4098 4099| 错误码ID | 错误信息 | 4100| -------- | ------------------------------------------------------------ | 4101| 201 | Permission denied. | 4102| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4103 4104**示例**: 4105 4106```ts 4107import { sensor } from '@kit.SensorServiceKit'; 4108import { BusinessError } from '@kit.BasicServicesKit'; 4109 4110function callback1(data: object) { 4111 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4112} 4113 4114function callback2(data: object) { 4115 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4116} 4117 4118// 使用try catch对可能出现的异常进行捕获 4119try { 4120 sensor.on(sensor.SensorId.PEDOMETER, callback1); 4121 sensor.on(sensor.SensorId.PEDOMETER, callback2); 4122 // 仅取消callback1的注册 4123 sensor.off(sensor.SensorId.PEDOMETER, callback1); 4124 // 取消注册SensorId.PEDOMETER的所有回调 4125 sensor.off(sensor.SensorId.PEDOMETER); 4126} catch (error) { 4127 let e: BusinessError = error as BusinessError; 4128 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4129} 4130``` 4131 4132### PEDOMETER<sup>19+</sup> 4133 4134off(type: SensorId.PEDOMETER, sensorInfoParam?: SensorInfoParam, callback?: Callback<PedometerResponse>): void 4135 4136取消订阅计步器传感器数据。 4137 4138**需要权限**:ohos.permission.ACTIVITY_MOTION 4139 4140**系统能力**:SystemCapability.Sensors.Sensor 4141 4142**参数**: 4143 4144| 参数名 | 类型 | 必填 | 说明 | 4145|------------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4146| type | [SensorId](#sensorid9).PEDOMETER | 是 | 传感器类型,该值固定为SensorId.PEDOMETER。 | 4147| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4148| callback | Callback<[PedometerResponse](#pedometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4149 4150**错误码**: 4151 4152以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。。 4153 4154| 错误码ID | 错误信息 | 4155| -------- | ------------------------------------------------------------ | 4156| 201 | Permission denied. | 4157| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4158 4159**示例**: 4160 4161```ts 4162import { sensor } from '@kit.SensorServiceKit'; 4163import { BusinessError } from '@kit.BasicServicesKit'; 4164 4165enum Ret { OK, Failed = -1 } 4166 4167// 传感器回调 4168const sensorCallback = (response: sensor.PedometerResponse) => { 4169 console.info(`callback response: ${JSON.stringify(response)}`); 4170} 4171// 传感器监听类型 4172const sensorType = sensor.SensorId.PEDOMETER; 4173const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4174 4175function sensorSubscribe(): Ret { 4176 let ret: Ret = Ret.OK; 4177 // 使用try catch对可能出现的异常进行捕获 4178 try { 4179 // 查询所有的传感器 4180 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4181 if (!sensorList.length) { 4182 return Ret.Failed; 4183 } 4184 // 根据实际业务逻辑获取目标传感器。 4185 const targetSensor = sensorList 4186 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4187 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4188 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4189 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4190 if (!targetSensor) { 4191 return Ret.Failed; 4192 } 4193 sensorInfoParam.deviceId = targetSensor.deviceId; 4194 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4195 // 订阅传感器事件 4196 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4197 } catch (error) { 4198 let e: BusinessError = error as BusinessError; 4199 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4200 ret = Ret.Failed; 4201 } 4202 return ret; 4203} 4204 4205function sensorUnsubscribe(): Ret { 4206 let ret: Ret = Ret.OK; 4207 // 使用try catch对可能出现的异常进行捕获 4208 try { 4209 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4210 } catch (error) { 4211 let e: BusinessError = error as BusinessError; 4212 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4213 ret = Ret.Failed; 4214 } 4215 return ret; 4216} 4217``` 4218 4219### PEDOMETER_DETECTION<sup>9+</sup> 4220 4221off(type: SensorId.PEDOMETER_DETECTION, callback?: Callback<PedometerDetectionResponse>): void 4222 4223取消订阅计步检测器传感器数据。 4224 4225**需要权限**:ohos.permission.ACTIVITY_MOTION 4226 4227**系统能力**:SystemCapability.Sensors.Sensor 4228 4229**参数**: 4230 4231| 参数名 | 类型 | 必填 | 说明 | 4232| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4233| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 4234| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4235 4236**错误码**: 4237 4238以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4239 4240| 错误码ID | 错误信息 | 4241| -------- | ------------------------------------------------------------ | 4242| 201 | Permission denied. | 4243| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4244 4245**示例**: 4246 4247```ts 4248import { sensor } from '@kit.SensorServiceKit'; 4249import { BusinessError } from '@kit.BasicServicesKit'; 4250 4251function callback1(data: object) { 4252 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4253} 4254 4255function callback2(data: object) { 4256 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4257} 4258 4259// 使用try catch对可能出现的异常进行捕获 4260try { 4261 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, callback1); 4262 sensor.on(sensor.SensorId.PEDOMETER_DETECTION, callback2); 4263 // 仅取消callback1的注册 4264 sensor.off(sensor.SensorId.PEDOMETER_DETECTION, callback1); 4265 // 取消注册SensorId.PEDOMETER_DETECTION的所有回调 4266 sensor.off(sensor.SensorId.PEDOMETER_DETECTION); 4267} catch (error) { 4268 let e: BusinessError = error as BusinessError; 4269 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4270} 4271``` 4272 4273### PEDOMETER_DETECTION<sup>19+</sup> 4274 4275off(type: SensorId.PEDOMETER_DETECTION, sensorInfoParam?: SensorInfoParam, callback?: Callback<PedometerDetectionResponse>): void 4276 4277取消订阅计步检测器传感器数据。 4278 4279**需要权限**:ohos.permission.ACTIVITY_MOTION 4280 4281**系统能力**:SystemCapability.Sensors.Sensor 4282 4283**参数**: 4284 4285| 参数名 | 类型 | 必填 | 说明 | 4286|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4287| type | [SensorId](#sensorid9).PEDOMETER_DETECTION | 是 | 传感器类型,该值固定为SensorId.PEDOMETER_DETECTION。 | 4288| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4289| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4290 4291**错误码**: 4292 4293以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4294 4295| 错误码ID | 错误信息 | 4296| -------- | ------------------------------------------------------------ | 4297| 201 | Permission denied. | 4298| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4299 4300**示例**: 4301 4302```ts 4303import { sensor } from '@kit.SensorServiceKit'; 4304import { BusinessError } from '@kit.BasicServicesKit'; 4305 4306enum Ret { OK, Failed = -1 } 4307 4308// 传感器回调 4309const sensorCallback = (response: sensor.PedometerDetectionResponse) => { 4310 console.info(`callback response: ${JSON.stringify(response)}`); 4311} 4312// 传感器监听类型 4313const sensorType = sensor.SensorId.PEDOMETER_DETECTION; 4314const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4315 4316function sensorSubscribe(): Ret { 4317 let ret: Ret = Ret.OK; 4318 // 使用try catch对可能出现的异常进行捕获 4319 try { 4320 // 查询所有的传感器 4321 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4322 if (!sensorList.length) { 4323 return Ret.Failed; 4324 } 4325 // 根据实际业务逻辑获取目标传感器。 4326 const targetSensor = sensorList 4327 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4328 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4329 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4330 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4331 if (!targetSensor) { 4332 return Ret.Failed; 4333 } 4334 sensorInfoParam.deviceId = targetSensor.deviceId; 4335 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4336 // 订阅传感器事件 4337 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4338 } catch (error) { 4339 let e: BusinessError = error as BusinessError; 4340 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4341 ret = Ret.Failed; 4342 } 4343 return ret; 4344} 4345 4346function sensorUnsubscribe(): Ret { 4347 let ret: Ret = Ret.OK; 4348 // 使用try catch对可能出现的异常进行捕获 4349 try { 4350 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4351 } catch (error) { 4352 let e: BusinessError = error as BusinessError; 4353 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4354 ret = Ret.Failed; 4355 } 4356 return ret; 4357} 4358``` 4359 4360### PROXIMITY<sup>9+</sup> 4361 4362off(type: SensorId.PROXIMITY, callback?: Callback<ProximityResponse>): void 4363 4364取消订阅接近光传感器数据。 4365 4366**系统能力**:SystemCapability.Sensors.Sensor 4367 4368**参数**: 4369 4370| 参数名 | 类型 | 必填 | 说明 | 4371| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4372| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 4373| callback | Callback<[ProximityResponse](#proximityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4374 4375**错误码**: 4376 4377以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4378 4379| 错误码ID | 错误信息 | 4380| -------- | ------------------------------------------------------------ | 4381| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4382 4383**示例**: 4384 4385```ts 4386import { sensor } from '@kit.SensorServiceKit'; 4387import { BusinessError } from '@kit.BasicServicesKit'; 4388 4389function callback1(data: object) { 4390 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4391} 4392 4393function callback2(data: object) { 4394 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4395} 4396 4397// 使用try catch对可能出现的异常进行捕获 4398try { 4399 sensor.on(sensor.SensorId.PROXIMITY, callback1); 4400 sensor.on(sensor.SensorId.PROXIMITY, callback2); 4401 // 仅取消callback1的注册 4402 sensor.off(sensor.SensorId.PROXIMITY, callback1); 4403 // 取消注册SensorId.PROXIMITY的所有回调 4404 sensor.off(sensor.SensorId.PROXIMITY); 4405} catch (error) { 4406 let e: BusinessError = error as BusinessError; 4407 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4408} 4409``` 4410 4411### PROXIMITY<sup>19+</sup> 4412 4413off(type: SensorId.PROXIMITY, sensorInfoParam?: SensorInfoParam, callback?: Callback<ProximityResponse>): void 4414 4415取消订阅接近光传感器数据。 4416 4417**系统能力**:SystemCapability.Sensors.Sensor 4418 4419**参数**: 4420 4421| 参数名 | 类型 | 必填 | 说明 | 4422|-----------------| ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4423| type | [SensorId](#sensorid9).PROXIMITY | 是 | 传感器类型,该值固定为SensorId.PROXIMITY。 | 4424| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4425| callback | Callback<[ProximityResponse](#proximityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4426 4427**错误码**: 4428 4429以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4430 4431| 错误码ID | 错误信息 | 4432| -------- | ------------------------------------------------------------ | 4433| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4434 4435**示例**: 4436 4437```ts 4438import { sensor } from '@kit.SensorServiceKit'; 4439import { BusinessError } from '@kit.BasicServicesKit'; 4440 4441enum Ret { OK, Failed = -1 } 4442 4443// 传感器回调 4444const sensorCallback = (response: sensor.ProximityResponse) => { 4445 console.info(`callback response: ${JSON.stringify(response)}`); 4446} 4447// 传感器监听类型 4448const sensorType = sensor.SensorId.PROXIMITY; 4449const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4450 4451function sensorSubscribe(): Ret { 4452 let ret: Ret = Ret.OK; 4453 // 使用try catch对可能出现的异常进行捕获 4454 try { 4455 // 查询所有的传感器 4456 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4457 if (!sensorList.length) { 4458 return Ret.Failed; 4459 } 4460 // 根据实际业务逻辑获取目标传感器。 4461 const targetSensor = sensorList 4462 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4463 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4464 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4465 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4466 if (!targetSensor) { 4467 return Ret.Failed; 4468 } 4469 sensorInfoParam.deviceId = targetSensor.deviceId; 4470 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4471 // 订阅传感器事件 4472 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4473 } catch (error) { 4474 let e: BusinessError = error as BusinessError; 4475 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4476 ret = Ret.Failed; 4477 } 4478 return ret; 4479} 4480 4481function sensorUnsubscribe(): Ret { 4482 let ret: Ret = Ret.OK; 4483 // 使用try catch对可能出现的异常进行捕获 4484 try { 4485 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4486 } catch (error) { 4487 let e: BusinessError = error as BusinessError; 4488 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4489 ret = Ret.Failed; 4490 } 4491 return ret; 4492} 4493``` 4494 4495### ROTATION_VECTOR<sup>9+</sup> 4496 4497off(type: SensorId.ROTATION_VECTOR, callback?: Callback<RotationVectorResponse>): void 4498 4499取消订阅旋转矢量传感器数据。 4500 4501**系统能力**:SystemCapability.Sensors.Sensor 4502 4503**参数**: 4504 4505| 参数名 | 类型 | 必填 | 说明 | 4506| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4507| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 4508| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4509 4510**错误码**: 4511 4512以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4513 4514| 错误码ID | 错误信息 | 4515| -------- | ------------------------------------------------------------ | 4516| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4517 4518**示例**: 4519 4520```ts 4521import { sensor } from '@kit.SensorServiceKit'; 4522import { BusinessError } from '@kit.BasicServicesKit'; 4523 4524function callback1(data: object) { 4525 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4526} 4527 4528function callback2(data: object) { 4529 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4530} 4531 4532// 使用try catch对可能出现的异常进行捕获 4533try { 4534 sensor.on(sensor.SensorId.ROTATION_VECTOR, callback1); 4535 sensor.on(sensor.SensorId.ROTATION_VECTOR, callback2); 4536 // 仅取消callback1的注册 4537 sensor.off(sensor.SensorId.ROTATION_VECTOR, callback1); 4538 // 取消注册SensorId.ROTATION_VECTOR的所有回调 4539 sensor.off(sensor.SensorId.ROTATION_VECTOR); 4540} catch (error) { 4541 let e: BusinessError = error as BusinessError; 4542 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4543} 4544``` 4545 4546### ROTATION_VECTOR<sup>19+</sup> 4547 4548off(type: SensorId.ROTATION_VECTOR, sensorInfoParam?: SensorInfoParam, callback?: Callback<RotationVectorResponse>): void 4549 4550取消订阅旋转矢量传感器数据。 4551 4552**系统能力**:SystemCapability.Sensors.Sensor 4553 4554**参数**: 4555 4556| 参数名 | 类型 | 必填 | 说明 | 4557|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4558| type | [SensorId](#sensorid9).ROTATION_VECTOR | 是 | 传感器类型,该值固定为SensorId.ROTATION_VECTOR。 | 4559| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4560| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4561 4562**错误码**: 4563 4564以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4565 4566| 错误码ID | 错误信息 | 4567| -------- | ------------------------------------------------------------ | 4568| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4569 4570**示例**: 4571 4572```ts 4573import { sensor } from '@kit.SensorServiceKit'; 4574import { BusinessError } from '@kit.BasicServicesKit'; 4575 4576enum Ret { OK, Failed = -1 } 4577 4578// 传感器回调 4579const sensorCallback = (response: sensor.RotationVectorResponse) => { 4580 console.info(`callback response: ${JSON.stringify(response)}`); 4581} 4582// 传感器监听类型 4583const sensorType = sensor.SensorId.ROTATION_VECTOR; 4584const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4585 4586function sensorSubscribe(): Ret { 4587 let ret: Ret = Ret.OK; 4588 // 使用try catch对可能出现的异常进行捕获 4589 try { 4590 // 查询所有的传感器 4591 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4592 if (!sensorList.length) { 4593 return Ret.Failed; 4594 } 4595 // 根据实际业务逻辑获取目标传感器。 4596 const targetSensor = sensorList 4597 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4598 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4599 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4600 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4601 if (!targetSensor) { 4602 return Ret.Failed; 4603 } 4604 sensorInfoParam.deviceId = targetSensor.deviceId; 4605 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4606 // 订阅传感器事件 4607 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4608 } catch (error) { 4609 let e: BusinessError = error as BusinessError; 4610 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4611 ret = Ret.Failed; 4612 } 4613 return ret; 4614} 4615 4616function sensorUnsubscribe(): Ret { 4617 let ret: Ret = Ret.OK; 4618 // 使用try catch对可能出现的异常进行捕获 4619 try { 4620 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4621 } catch (error) { 4622 let e: BusinessError = error as BusinessError; 4623 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4624 ret = Ret.Failed; 4625 } 4626 return ret; 4627} 4628``` 4629 4630### SIGNIFICANT_MOTION<sup>9+</sup> 4631 4632off(type: SensorId.SIGNIFICANT_MOTION, callback?: Callback<SignificantMotionResponse>): void 4633 4634取消订阅有效运动传感器数据。 4635 4636**系统能力**:SystemCapability.Sensors.Sensor 4637 4638**参数**: 4639 4640| 参数名 | 类型 | 必填 | 说明 | 4641| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4642| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 4643| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4644 4645**错误码**: 4646 4647以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4648 4649| 错误码ID | 错误信息 | 4650| -------- | ------------------------------------------------------------ | 4651| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4652 4653**示例**: 4654 4655```ts 4656import { sensor } from '@kit.SensorServiceKit'; 4657import { BusinessError } from '@kit.BasicServicesKit'; 4658 4659function callback1(data: object) { 4660 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4661} 4662 4663function callback2(data: object) { 4664 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4665} 4666 4667// 使用try catch对可能出现的异常进行捕获 4668try { 4669 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, callback1); 4670 sensor.on(sensor.SensorId.SIGNIFICANT_MOTION, callback2); 4671 // 仅取消callback1的注册 4672 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION, callback1); 4673 // 取消注册SensorId.SIGNIFICANT_MOTION的所有回调 4674 sensor.off(sensor.SensorId.SIGNIFICANT_MOTION); 4675} catch (error) { 4676 let e: BusinessError = error as BusinessError; 4677 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4678} 4679``` 4680 4681### SIGNIFICANT_MOTION<sup>19+</sup> 4682 4683off(type: SensorId.SIGNIFICANT_MOTION, sensorInfoParam?: SensorInfoParam, callback?: Callback<SignificantMotionResponse>): void 4684 4685取消订阅有效运动传感器数据。 4686 4687**系统能力**:SystemCapability.Sensors.Sensor 4688 4689**参数**: 4690 4691| 参数名 | 类型 | 必填 | 说明 | 4692|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4693| type | [SensorId](#sensorid9).SIGNIFICANT_MOTION | 是 | 传感器类型,该值固定为SensorId.SIGNIFICANT_MOTION。 | 4694| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4695| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4696 4697**错误码**: 4698 4699以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4700 4701| 错误码ID | 错误信息 | 4702| -------- | ------------------------------------------------------------ | 4703| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4704 4705**示例**: 4706 4707```ts 4708import { sensor } from '@kit.SensorServiceKit'; 4709import { BusinessError } from '@kit.BasicServicesKit'; 4710 4711enum Ret { OK, Failed = -1 } 4712 4713// 传感器回调 4714const sensorCallback = (response: sensor.SignificantMotionResponse) => { 4715 console.info(`callback response: ${JSON.stringify(response)}`); 4716} 4717// 传感器监听类型 4718const sensorType = sensor.SensorId.SIGNIFICANT_MOTION; 4719const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4720 4721function sensorSubscribe(): Ret { 4722 let ret: Ret = Ret.OK; 4723 // 使用try catch对可能出现的异常进行捕获 4724 try { 4725 // 查询所有的传感器 4726 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4727 if (!sensorList.length) { 4728 return Ret.Failed; 4729 } 4730 // 根据实际业务逻辑获取目标传感器。 4731 const targetSensor = sensorList 4732 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4733 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4734 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4735 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4736 if (!targetSensor) { 4737 return Ret.Failed; 4738 } 4739 sensorInfoParam.deviceId = targetSensor.deviceId; 4740 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4741 // 订阅传感器事件 4742 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4743 } catch (error) { 4744 let e: BusinessError = error as BusinessError; 4745 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4746 ret = Ret.Failed; 4747 } 4748 return ret; 4749} 4750 4751function sensorUnsubscribe(): Ret { 4752 let ret: Ret = Ret.OK; 4753 // 使用try catch对可能出现的异常进行捕获 4754 try { 4755 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4756 } catch (error) { 4757 let e: BusinessError = error as BusinessError; 4758 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4759 ret = Ret.Failed; 4760 } 4761 return ret; 4762} 4763``` 4764 4765### WEAR_DETECTION<sup>9+</sup> 4766 4767off(type: SensorId.WEAR_DETECTION, callback?: Callback<WearDetectionResponse>): void 4768 4769取消订阅佩戴检测传感器数据。 4770 4771**系统能力**:SystemCapability.Sensors.Sensor 4772 4773**参数**: 4774 4775| 参数名 | 类型 | 必填 | 说明 | 4776| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4777| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 4778| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4779 4780**错误码**: 4781 4782以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4783 4784| 错误码ID | 错误信息 | 4785| -------- | ------------------------------------------------------------ | 4786| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 4787 4788**示例**: 4789 4790```ts 4791import { sensor } from '@kit.SensorServiceKit'; 4792import { BusinessError } from '@kit.BasicServicesKit'; 4793 4794function callback1(data: object) { 4795 console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 4796} 4797 4798function callback2(data: object) { 4799 console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 4800} 4801 4802// 使用try catch对可能出现的异常进行捕获 4803try { 4804 sensor.on(sensor.SensorId.WEAR_DETECTION, callback1); 4805 sensor.on(sensor.SensorId.WEAR_DETECTION, callback2); 4806 // 仅取消callback1的注册 4807 sensor.off(sensor.SensorId.WEAR_DETECTION, callback1); 4808 // 取消注册SensorId.WEAR_DETECTION的所有回调 4809 sensor.off(sensor.SensorId.WEAR_DETECTION); 4810} catch (error) { 4811 let e: BusinessError = error as BusinessError; 4812 console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 4813} 4814``` 4815 4816### WEAR_DETECTION<sup>19+</sup> 4817 4818off(type: SensorId.WEAR_DETECTION, sensorInfoParam?: SensorInfoParam, callback?: Callback<WearDetectionResponse>): void 4819 4820取消订阅佩戴检测传感器数据。 4821 4822**系统能力**:SystemCapability.Sensors.Sensor 4823 4824**参数**: 4825 4826| 参数名 | 类型 | 必填 | 说明 | 4827|------------------| ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4828| type | [SensorId](#sensorid9).WEAR_DETECTION | 是 | 传感器类型,该值固定为SensorId.WEAR_DETECTION。 | 4829| sensorInfoParam | [SensorInfoParam](#sensorinfoparam19) | 否 | 传感器传入设置参数,可指定deviceId、sensorIndex | 4830| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 4831 4832**错误码**: 4833 4834以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4835 4836| 错误码ID | 错误信息 | 4837| -------- | ------------------------------------------------------------ | 4838| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4839 4840**示例**: 4841 4842```ts 4843import { sensor } from '@kit.SensorServiceKit'; 4844import { BusinessError } from '@kit.BasicServicesKit'; 4845 4846enum Ret { OK, Failed = -1 } 4847 4848// 传感器回调 4849const sensorCallback = (response: sensor.WearDetectionResponse) => { 4850 console.info(`callback response: ${JSON.stringify(response)}`); 4851} 4852// 传感器监听类型 4853const sensorType = sensor.SensorId.WEAR_DETECTION; 4854const sensorInfoParam: sensor.SensorInfoParam = { deviceId: -1, sensorIndex: 0 }; 4855 4856function sensorSubscribe(): Ret { 4857 let ret: Ret = Ret.OK; 4858 // 使用try catch对可能出现的异常进行捕获 4859 try { 4860 // 查询所有的传感器 4861 const sensorList: sensor.Sensor[] = sensor.getSensorListSync(); 4862 if (!sensorList.length) { 4863 return Ret.Failed; 4864 } 4865 // 根据实际业务逻辑获取目标传感器。 4866 const targetSensor = sensorList 4867 // 按需过滤deviceId为1、sensorId为2的所有传感器。此处示例仅做展示,开发者需要自行调整筛选逻辑。 4868 .filter((sensor: sensor.Sensor) => sensor.deviceId === 1 && sensor.sensorId === 2) 4869 // 可能存在的多个同类型传感器,选择sensorIndex为0的传感器。 4870 .find((sensor: sensor.Sensor) => sensor.sensorIndex === 0); 4871 if (!targetSensor) { 4872 return Ret.Failed; 4873 } 4874 sensorInfoParam.deviceId = targetSensor.deviceId; 4875 sensorInfoParam.sensorIndex = targetSensor.sensorIndex; 4876 // 订阅传感器事件 4877 sensor.on(sensorType, sensorCallback, { sensorInfoParam }); 4878 } catch (error) { 4879 let e: BusinessError = error as BusinessError; 4880 console.error(`Failed to invoke sensor.on. Code: ${e.code}, message: ${e.message}`); 4881 ret = Ret.Failed; 4882 } 4883 return ret; 4884} 4885 4886function sensorUnsubscribe(): Ret { 4887 let ret: Ret = Ret.OK; 4888 // 使用try catch对可能出现的异常进行捕获 4889 try { 4890 sensor.off(sensorType, sensorInfoParam, sensorCallback); 4891 } catch (error) { 4892 let e: BusinessError = error as BusinessError; 4893 console.error(`Failed to invoke sensor.off. Code: ${e.code}, message: ${e.message}`); 4894 ret = Ret.Failed; 4895 } 4896 return ret; 4897} 4898``` 4899 4900### sensorStatusChange<sup>19+<sup> 4901 4902off(type: 'sensorStatusChange', callback?: Callback<SensorStatusEvent>): void 4903 4904取消监听传感器变化。 4905 4906**系统能力**:SystemCapability.Sensors.Sensor 4907 4908**参数**: 4909 4910| 参数名 | 类型 | 必填 | 说明 | 4911| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 4912| type | 'sensorStatusChange' | 是 | 固定传入'sensorStatusChange',状态监听固定参数。 | 4913| callback | Callback<[SensorStatusEvent](#sensorstatusevent19)> | 否 | sensor.on传入的回调函数,不传则取消所有监听。 | 4914 4915**错误码**: 4916 4917以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 4918 4919| 错误码ID | 错误信息 | 4920| -------- | ------------------------------------------------------------ | 4921| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 4922 4923**示例**: 4924 4925```ts 4926import { sensor } from '@kit.SensorServiceKit'; 4927import { BusinessError } from '@kit.BasicServicesKit'; 4928 4929// 使用try catch对可能出现的异常进行捕获 4930try { 4931 const statusChangeCallback = (data: sensor.SensorStatusEvent) => { 4932 console.info('sensorStatusChange : ' + JSON.stringify(data)); 4933 } 4934 const statusChangeCallback2 = (data: sensor.SensorStatusEvent) => { 4935 console.info('sensorStatusChange2 : ' + JSON.stringify(data)); 4936 } 4937 // 注册两个设备上线消息监听回调 4938 sensor.on('sensorStatusChange', statusChangeCallback); 4939 sensor.on('sensorStatusChange', statusChangeCallback2); 4940 4941 // 3秒后注销第一个监听 4942 setTimeout(() => { 4943 sensor.off('sensorStatusChange', statusChangeCallback); 4944 }, 3000); 4945 // 5秒后注销所有监听 4946 setTimeout(() => { 4947 sensor.off('sensorStatusChange'); 4948 }, 5000); 4949} catch (error) { 4950 let e: BusinessError = error as BusinessError; 4951 console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 4952} 4953``` 4954 4955 4956## sensor.getSensorListByDeviceSync<sup>19+</sup> 4957 4958getSensorListByDeviceSync(deviceId?: number): Array<Sensor> 4959 4960同步获取设备的所有传感器信息。 4961 4962**系统能力**:SystemCapability.Sensors.Sensor 4963 4964**参数**: 4965 4966| 参数名 | 类型 | 必填 | 说明 | 4967| --------------- | ------------------------------------------------------------ | ---- |--------| 4968| deviceId | number | 否 | 设备ID,默认为查询本地设备,默认值为-1,表示本地设备,设备ID需通过[getSensorList](#sensorgetsensorlist9)查询或者监听设备上下线接口[on](#sensorstatuschange19)获取。 | 4969 4970 4971**返回值**: 4972 4973| 类型 | 说明 | 4974| ---------------------------------------------------------- | -------------- | 4975| Array<[Sensor](#sensor9)> | 传感器属性列表。 | 4976 4977 4978**示例**: 4979 4980```ts 4981import { sensor } from '@kit.SensorServiceKit'; 4982import { BusinessError } from '@kit.BasicServicesKit'; 4983 4984try { 4985 const deviceId = 1; 4986 // 第一个参数deviceId 非必填 4987 const sensorList: sensor.Sensor[] = sensor.getSensorListByDeviceSync(deviceId); 4988 console.info(`sensorList length: ${sensorList.length}`); 4989 console.info(`sensorList: ${JSON.stringify(sensorList)}`); 4990} catch (error) { 4991 let e: BusinessError = error as BusinessError; 4992 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 4993} 4994``` 4995 4996 4997## sensor.getSingleSensorByDeviceSync<sup>19+</sup> 4998 4999getSingleSensorByDeviceSync(type: SensorId, deviceId?: number): Array<Sensor> 5000 5001同步获取指定设备和类型的传感器信息。 5002 5003**系统能力**:SystemCapability.Sensors.Sensor 5004 5005**参数**: 5006 5007| 参数名 | 类型 | 必填 | 说明 | 5008| --------------- | ------------------------------------------------------------ | ---- |----------| 5009| type | [SensorId](#sensorid9) | 是 | 指定传感器类型。 | 5010| deviceId | number | 否 | 设备ID,默认为查询本地设备,默认值为-1,表示本地设备,设备ID需通过[getSensorList](#sensorgetsensorlist9)查询或者监听设备上下线接口[on](#sensorstatuschange19)获取。 | 5011 5012 5013**返回值**: 5014 5015| 类型 | 说明 | 5016| ---------------------------------------------------------- | -------------- | 5017| Array<[Sensor](#sensor9)> | 传感器属性列表。 | 5018 5019**示例**: 5020 5021```ts 5022import { sensor } from '@kit.SensorServiceKit'; 5023import { BusinessError } from '@kit.BasicServicesKit'; 5024 5025try { 5026 const deviceId = 1; 5027 // 第二个参数deviceId 非必填 5028 const sensorList: sensor.Sensor[] = sensor.getSingleSensorByDeviceSync(sensor.SensorId.ACCELEROMETER, deviceId); 5029 console.info(`sensorList length: ${sensorList.length}`); 5030 console.info(`sensorList Json: ${JSON.stringify(sensorList)}`); 5031} catch (error) { 5032 let e: BusinessError = error as BusinessError; 5033 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 5034} 5035``` 5036 5037 5038## sensor.getGeomagneticInfo<sup>9+</sup> 5039 5040getGeomagneticInfo(locationOptions: LocationOptions, timeMillis: number, callback: AsyncCallback<GeomagneticResponse>): void 5041 5042获取某时刻地球上特定位置的地磁场信息,使用Callback异步方式返回结果。 5043 5044**系统能力**:SystemCapability.Sensors.Sensor 5045 5046**参数**: 5047 5048| 参数名 | 类型 | 必填 | 说明 | 5049| --------------- | ------------------------------------------------------------ | ---- | ---------------------------------- | 5050| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置,包括经度、纬度和海拔高度。 | 5051| timeMillis | number | 是 | 获取磁偏角的时间,unix时间戳,单位毫秒。 | 5052| callback | AsyncCallback<[GeomagneticResponse](#geomagneticresponse)> | 是 | 回调函数,异步返回地磁场信息。 | 5053 5054**错误码**: 5055 5056以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5057 5058| 错误码ID | 错误信息 | 5059| -------- | ------------------------------------------------------------ | 5060| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5061| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5062 5063**示例**: 5064 5065```ts 5066import { sensor } from '@kit.SensorServiceKit'; 5067import { BusinessError } from '@kit.BasicServicesKit'; 5068 5069// 使用try catch对可能出现的异常进行捕获 5070try { 5071 sensor.getGeomagneticInfo({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000, 5072 (err: BusinessError, data: sensor.GeomagneticResponse) => { 5073 if (err) { 5074 console.error(`Failed to get geomagneticInfo. Code: ${err.code}, message: ${err.message}`); 5075 return; 5076 } 5077 console.info("Succeeded in getting geomagneticInfo x" + data.x); 5078 console.info("Succeeded in getting geomagneticInfo y" + data.y); 5079 console.info("Succeeded in getting geomagneticInfo z" + data.z); 5080 console.info("Succeeded in getting geomagneticInfo geomagneticDip" + data.geomagneticDip); 5081 console.info("Succeeded in getting geomagneticInfo deflectionAngle" + data.deflectionAngle); 5082 console.info("Succeeded in getting geomagneticInfo levelIntensity" + data.levelIntensity); 5083 console.info("Succeeded in getting geomagneticInfo totalIntensity" + data.totalIntensity); 5084 }); 5085} catch (error) { 5086 let e: BusinessError = error as BusinessError; 5087 console.error(`Failed to get geomagneticInfo. Code: ${e.code}, message: ${e.message}`); 5088} 5089``` 5090 5091## sensor.getGeomagneticInfo<sup>9+</sup> 5092 5093getGeomagneticInfo(locationOptions: LocationOptions, timeMillis: number): Promise<GeomagneticResponse> 5094 5095获取某时刻地球上特定位置的地磁场信息,使用Promise异步方式返回结果。 5096 5097**系统能力**:SystemCapability.Sensors.Sensor 5098 5099**参数**: 5100 5101| 参数名 | 类型 | 必填 | 说明 | 5102| --------------- | ----------------------------------- | ---- | ---------------------------------- | 5103| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置,包括经度、纬度和海拔高度。 | 5104| timeMillis | number | 是 | 获取磁偏角的时间,unix时间戳,单位毫秒。 | 5105 5106**返回值**: 5107 5108| 类型 | 说明 | 5109| ---------------------------------------------------------- | -------------- | 5110| Promise<[GeomagneticResponse](#geomagneticresponse)> | Promise对象,使用异步方式返回地磁场信息。 | 5111 5112**错误码**: 5113 5114以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5115 5116| 错误码ID | 错误信息 | 5117| -------- | ------------------------------------------------------------ | 5118| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5119| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5120 5121**示例**: 5122 5123```ts 5124import { sensor } from '@kit.SensorServiceKit'; 5125import { BusinessError } from '@kit.BasicServicesKit'; 5126 5127// 使用try catch对可能出现的异常进行捕获 5128try { 5129 const promise = sensor.getGeomagneticInfo({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000); 5130 promise.then((data: sensor.GeomagneticResponse) => { 5131 console.info("Succeeded in getting geomagneticInfo x" + data.x); 5132 console.info("Succeeded in getting geomagneticInfo y" + data.y); 5133 console.info("Succeeded in getting geomagneticInfo z" + data.z); 5134 console.info("Succeeded in getting geomagneticInfo geomagneticDip" + data.geomagneticDip); 5135 console.info("Succeeded in getting geomagneticInfo deflectionAngle" + data.deflectionAngle); 5136 console.info("Succeeded in getting geomagneticInfo levelIntensity" + data.levelIntensity); 5137 console.info("Succeeded in getting geomagneticInfo totalIntensity" + data.totalIntensity); 5138 }, (err: BusinessError) => { 5139 console.error(`Failed to get geomagneticInfo. Code: ${err.code}, message: ${err.message}`); 5140 }); 5141} catch (error) { 5142 let e: BusinessError = error as BusinessError; 5143 console.error(`Failed to get geomagneticInfo. Code: ${e.code}, message: ${e.message}`); 5144} 5145``` 5146 5147## sensor.getDeviceAltitude<sup>9+</sup> 5148 5149getDeviceAltitude(seaPressure: number, currentPressure: number, callback: AsyncCallback<number>): void 5150 5151根据气压值获取海拔高度,使用Callback异步方式返回结果。 5152 5153**系统能力**:SystemCapability.Sensors.Sensor 5154 5155**参数**: 5156 5157| 参数名 | 类型 | 必填 | 说明 | 5158| --------------- | --------------------------- | ---- | ------------------------------------- | 5159| seaPressure | number | 是 | 海平面气压值,单位为hPa。 | 5160| currentPressure | number | 是 | 指定的气压值,单位为hPa。 | 5161| callback | AsyncCallback<number> | 是 | 回调函数,异步返回指定的气压值对应的海拔高度,单位为米。 | 5162 5163**错误码**: 5164 5165以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5166 5167| 错误码ID | 错误信息 | 5168| -------- | ------------------------------------------------------------ | 5169| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5170| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5171 5172**示例**: 5173 5174```ts 5175import { sensor } from '@kit.SensorServiceKit'; 5176import { BusinessError } from '@kit.BasicServicesKit'; 5177 5178// 使用try catch对可能出现的异常进行捕获 5179try { 5180 let seaPressure = 1013.2; 5181 let currentPressure = 1500.0; 5182 sensor.getDeviceAltitude(seaPressure, currentPressure, (err: BusinessError, data: number) => { 5183 if (err) { 5184 console.error(`Failed to get altitude. Code: ${err.code}, message: ${err.message}`); 5185 return; 5186 } 5187 console.info('Succeeded in getting altitude: ' + data); 5188 }); 5189} catch (error) { 5190 let e: BusinessError = error as BusinessError; 5191 console.error(`Failed to get altitude. Code: ${e.code}, message: ${e.message}`); 5192} 5193``` 5194 5195## sensor.getDeviceAltitude<sup>9+</sup> 5196 5197getDeviceAltitude(seaPressure: number, currentPressure: number): Promise<number> 5198 5199根据气压值获取海拔高度,使用Promise异步方式返回结果。 5200 5201**系统能力**:SystemCapability.Sensors.Sensor 5202 5203**参数**: 5204 5205| 参数名 | 类型 | 必填 | 说明 | 5206| --------------- | ------ | ---- | ------------------------------------- | 5207| seaPressure | number | 是 | 海平面气压值,单位为hPa。 | 5208| currentPressure | number | 是 | 指定的气压值,单位为hPa。 | 5209 5210**返回值**: 5211 5212| 类型 | 说明 | 5213| --------------------- | ------------------------------------ | 5214| Promise<number> | Promise对象,使用异步方式返回指定的气压值对应的海拔高度,单位为米。 | 5215 5216**错误码**: 5217 5218以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5219 5220| 错误码ID | 错误信息 | 5221| -------- | ------------------------------------------------------------ | 5222| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5223| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5224 5225**示例**: 5226 5227```ts 5228import { sensor } from '@kit.SensorServiceKit'; 5229import { BusinessError } from '@kit.BasicServicesKit'; 5230 5231// 使用try catch对可能出现的异常进行捕获 5232try { 5233 let seaPressure = 1013.2; 5234 let currentPressure = 1500.0; 5235 const promise = sensor.getDeviceAltitude(seaPressure, currentPressure); 5236 promise.then((data: number) => { 5237 console.info('Succeeded in getting sensor_getDeviceAltitude_Promise', data); 5238 }, (err: BusinessError) => { 5239 console.error(`Failed to get altitude. Code: ${err.code}, message: ${err.message}`); 5240 }); 5241} catch (error) { 5242 let e: BusinessError = error as BusinessError; 5243 console.error(`Failed to get altitude. Code: ${e.code}, message: ${e.message}`); 5244} 5245``` 5246 5247## sensor.getInclination<sup>9+</sup> 5248 5249getInclination(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void 5250 5251根据倾斜矩阵计算地磁倾角,使用Callback异步方式返回结果。 5252 5253**系统能力**:SystemCapability.Sensors.Sensor 5254 5255**参数**: 5256 5257| 参数名 | 类型 | 必填 | 说明 | 5258| ----------------- | --------------------------- | ---- | ---------------------------- | 5259| inclinationMatrix | Array<number> | 是 | 倾斜矩阵。 | 5260| callback | AsyncCallback<number> | 是 | 回调函数,异步返回地磁倾角,单位为弧度。 | 5261 5262**错误码**: 5263 5264以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5265 5266| 错误码ID | 错误信息 | 5267| -------- | ------------------------------------------------------------ | 5268| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5269| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5270 5271**示例**: 5272 5273```ts 5274import { sensor } from '@kit.SensorServiceKit'; 5275import { BusinessError } from '@kit.BasicServicesKit'; 5276 5277// 使用try catch对可能出现的异常进行捕获 5278try { 5279 // inclinationMatrix可以为3*3,或者4*4 5280 let inclinationMatrix = [ 5281 1, 0, 0, 5282 0, 1, 0, 5283 0, 0, 1 5284 ] 5285 sensor.getInclination(inclinationMatrix, (err: BusinessError, data: number) => { 5286 if (err) { 5287 console.error(`Failed to get inclination. Code: ${err.code}, message: ${err.message}`); 5288 return; 5289 } 5290 console.info('Succeeded in getting inclination: ' + data); 5291 }) 5292} catch (error) { 5293 let e: BusinessError = error as BusinessError; 5294 console.error(`Failed to get inclination. Code: ${e.code}, message: ${e.message}`); 5295} 5296``` 5297 5298## sensor.getInclination<sup>9+</sup> 5299 5300 getInclination(inclinationMatrix: Array<number>): Promise<number> 5301 5302根据倾斜矩阵计算地磁倾角,使用Promise异步方式返回结果。 5303 5304**系统能力**:SystemCapability.Sensors.Sensor 5305 5306**参数**: 5307 5308| 参数名 | 类型 | 必填 | 说明 | 5309| ----------------- | ------------------- | ---- | -------------- | 5310| inclinationMatrix | Array<number> | 是 | 倾斜矩阵。 | 5311 5312**返回值**: 5313 5314| 类型 | 说明 | 5315| --------------------- | ---------------------------- | 5316| Promise<number> | Promise对象,使用异步方式返回地磁倾斜角,单位为弧度。 | 5317 5318**错误码**: 5319 5320以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5321 5322| 错误码ID | 错误信息 | 5323| -------- | ------------------------------------------------------------ | 5324| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5325| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5326 5327**示例**: 5328 5329```ts 5330import { sensor } from '@kit.SensorServiceKit'; 5331import { BusinessError } from '@kit.BasicServicesKit'; 5332 5333// 使用try catch对可能出现的异常进行捕获 5334try { 5335 // inclinationMatrix可以为3*3,或者4*4 5336 let inclinationMatrix = [ 5337 1, 0, 0, 5338 0, 1, 0, 5339 0, 0, 1 5340 ] 5341 const promise = sensor.getInclination(inclinationMatrix); 5342 promise.then((data: number) => { 5343 console.info('Succeeded in getting inclination: ' + data); 5344 }, (err: BusinessError) => { 5345 console.error(`Failed to get inclination. Code: ${err.code}, message: ${err.message}`); 5346 }); 5347} catch (error) { 5348 let e: BusinessError = error as BusinessError; 5349 console.error(`Failed to get inclination. Code: ${e.code}, message: ${e.message}`); 5350} 5351``` 5352 5353## sensor.getAngleVariation<sup>9+</sup> 5354 5355 getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>, 5356 callback: AsyncCallback<Array<number>>): void 5357 5358计算两个旋转矩阵之间的角度变化,使用Callback异步方式返回结果。 5359 5360**系统能力**:SystemCapability.Sensors.Sensor 5361 5362**参数**: 5363 5364| 参数名 | 类型 | 必填 | 说明 | 5365| --------------------- | ---------------------------------------- | ---- | --------------------------------- | 5366| currentRotationMatrix | Array<number> | 是 | 当前旋转矩阵。 | 5367| preRotationMatrix | Array<number> | 是 | 相对旋转矩阵。 | 5368| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回绕z、x、y轴方向的旋转角度。 | 5369 5370**错误码**: 5371 5372以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5373 5374| 错误码ID | 错误信息 | 5375| -------- | ------------------------------------------------------------ | 5376| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5377| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5378 5379**示例**: 5380 5381```ts 5382import { sensor } from '@kit.SensorServiceKit'; 5383import { BusinessError } from '@kit.BasicServicesKit'; 5384 5385// 使用try catch对可能出现的异常进行捕获 5386try { 5387 // 旋转矩阵可以为3*3,或者4*4 5388 let currentRotationMatrix = [ 5389 1, 0, 0, 5390 0, 1, 0, 5391 0, 0, 1 5392 ]; 5393 let preRotationMatrix = [ 5394 1, 0, 0, 5395 0, 0.87, -0.50, 5396 0, 0.50, 0.87 5397 ]; 5398 sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix, (err: BusinessError, data: Array<number>) => { 5399 if (err) { 5400 console.error(`Failed to get angle variation. Code: ${err.code}, message: ${err.message}`); 5401 return; 5402 } 5403 if (data.length < 3) { 5404 console.error("Failed to get angle variation, length" + data.length); 5405 return; 5406 } 5407 console.info("Z: " + data[0]); 5408 console.info("X: " + data[1]); 5409 console.info("Y: " + data[2]); 5410 }) 5411} catch (error) { 5412 let e: BusinessError = error as BusinessError; 5413 console.error(`Failed to get angle variation. Code: ${e.code}, message: ${e.message}`); 5414} 5415``` 5416 5417## sensor.getAngleVariation<sup>9+</sup> 5418 5419getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>> 5420 5421得到两个旋转矩阵之间的角度变化,使用Promise异步方式返回结果。 5422 5423**系统能力**:SystemCapability.Sensors.Sensor 5424 5425**参数**: 5426 5427| 参数名 | 类型 | 必填 | 说明 | 5428| --------------------- | ------------------- | ---- | ------------------ | 5429| currentRotationMatrix | Array<number> | 是 | 当前旋转矩阵。 | 5430| preRotationMatrix | Array<number> | 是 | 相对旋转矩阵。 | 5431 5432**返回值**: 5433 5434| 类型 | 说明 | 5435| ---------------------------------- | --------------------------------- | 5436| Promise<Array<number>> | Promise对象,使用异步方式返回绕z、x、y轴方向的旋转角度。 | 5437 5438**错误码**: 5439 5440以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5441 5442| 错误码ID | 错误信息 | 5443| -------- | ------------------------------------------------------------ | 5444| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5445| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5446 5447**示例**: 5448 5449```ts 5450import { sensor } from '@kit.SensorServiceKit'; 5451import { BusinessError } from '@kit.BasicServicesKit'; 5452 5453// 使用try catch对可能出现的异常进行捕获 5454try { 5455 // 旋转矩阵可以为3*3,或者4*4 5456 let currentRotationMatrix = [ 5457 1, 0, 0, 5458 0, 1, 0, 5459 0, 0, 1 5460 ]; 5461 let preRotationMatrix = [ 5462 1, 0, 0, 5463 0, 0.87, -0.50, 5464 0, 0.50, 0.87 5465 ]; 5466 const promise = sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix); 5467 promise.then((data: Array<number>) => { 5468 if (data.length < 3) { 5469 console.error("Failed to get angle variation, length" + data.length); 5470 return; 5471 } 5472 console.info("Z: " + data[0]); 5473 console.info("X: " + data[1]); 5474 console.info("Y: " + data[2]); 5475 }, (err: BusinessError) => { 5476 console.error(`Failed to get angle variation. Code: ${err.code}, message: ${err.message}`); 5477 }); 5478} catch (error) { 5479 let e: BusinessError = error as BusinessError; 5480 console.error(`Failed to get angle variation. Code: ${e.code}, message: ${e.message}`); 5481} 5482``` 5483 5484## sensor.getRotationMatrix<sup>9+</sup> 5485 5486getRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 5487 5488根据旋转矢量获取旋转矩阵,使用Callback异步方式返回结果。 5489 5490**系统能力**:SystemCapability.Sensors.Sensor 5491 5492**参数**: 5493 5494| 参数名 | 类型 | 必填 | 说明 | 5495| -------------- | ---------------------------------------- | ---- | -------------- | 5496| rotationVector | Array<number> | 是 | 旋转矢量。 | 5497| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回3*3旋转矩阵。 | 5498 5499**错误码**: 5500 5501以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5502 5503| 错误码ID | 错误信息 | 5504| -------- | ------------------------------------------------------------ | 5505| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5506| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5507 5508**示例**: 5509 5510```ts 5511import { sensor } from '@kit.SensorServiceKit'; 5512import { BusinessError } from '@kit.BasicServicesKit'; 5513 5514// 使用try catch对可能出现的异常进行捕获 5515try { 5516 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5517 sensor.getRotationMatrix(rotationVector, (err: BusinessError, data: Array<number>) => { 5518 if (err) { 5519 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5520 return; 5521 } 5522 for (let i = 0; i < data.length; i++) { 5523 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5524 } 5525 }) 5526} catch (error) { 5527 let e: BusinessError = error as BusinessError; 5528 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5529} 5530``` 5531 5532## sensor.getRotationMatrix<sup>9+</sup> 5533 5534getRotationMatrix(rotationVector: Array<number>): Promise<Array<number>> 5535 5536根据旋转矢量获取旋转矩阵,使用Promise异步方式返回结果。 5537 5538**系统能力**:SystemCapability.Sensors.Sensor 5539 5540**参数**: 5541 5542| 参数名 | 类型 | 必填 | 说明 | 5543| -------------- | ------------------- | ---- | -------------- | 5544| rotationVector | Array<number> | 是 | 旋转矢量。 | 5545 5546**返回值**: 5547 5548| 类型 | 说明 | 5549| ---------------------------------- | -------------- | 5550| Promise<Array<number>> | Promise对象,使用异步方式返回旋转矩阵。 | 5551 5552**错误码**: 5553 5554以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5555 5556| 错误码ID | 错误信息 | 5557| -------- | ------------------------------------------------------------ | 5558| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5559| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5560 5561**示例**: 5562 5563```ts 5564import { sensor } from '@kit.SensorServiceKit'; 5565import { BusinessError } from '@kit.BasicServicesKit'; 5566 5567// 使用try catch对可能出现的异常进行捕获 5568try { 5569 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5570 const promise = sensor.getRotationMatrix(rotationVector); 5571 promise.then((data: Array<number>) => { 5572 for (let i = 0; i < data.length; i++) { 5573 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5574 } 5575 }, (err: BusinessError) => { 5576 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5577 }); 5578} catch (error) { 5579 let e: BusinessError = error as BusinessError; 5580 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5581} 5582``` 5583 5584## sensor.transformRotationMatrix<sup>9+</sup> 5585 5586transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions, 5587 callback: AsyncCallback<Array<number>>): void 5588 5589根据指定坐标系映射旋转矩阵,使用Callback异步方式返回结果。 5590 5591**系统能力**:SystemCapability.Sensors.Sensor 5592 5593**参数**: 5594 5595| 参数名 | 类型 | 必填 | 说明 | 5596| ---------------- | ----------------------------------------- | ---- | ---------------------- | 5597| inRotationVector | Array<number> | 是 | 旋转矩阵。 | 5598| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 指定坐标系方向。 | 5599| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回映射后的旋转矩阵。 | 5600 5601**错误码**: 5602 5603以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5604 5605| 错误码ID | 错误信息 | 5606| -------- | ------------------------------------------------------------ | 5607| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5608| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5609 5610**示例**: 5611 5612```ts 5613import { sensor } from '@kit.SensorServiceKit'; 5614import { BusinessError } from '@kit.BasicServicesKit'; 5615 5616// 使用try catch对可能出现的异常进行捕获 5617try { 5618 let rotationMatrix = [ 5619 1, 0, 0, 5620 0, 0.87, -0.50, 5621 0, 0.50, 0.87 5622 ]; 5623 sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }, (err: BusinessError, data: Array<number>) => { 5624 if (err) { 5625 console.error(`Failed to transform rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5626 return; 5627 } 5628 for (let i = 0; i < data.length; i++) { 5629 console.info('Succeeded in getting data[' + i + '] = ' + data[i]); 5630 } 5631 }) 5632} catch (error) { 5633 let e: BusinessError = error as BusinessError; 5634 console.error(`Failed to transform rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5635} 5636``` 5637 5638## sensor.transformRotationMatrix<sup>9+</sup> 5639 5640transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>> 5641 5642根据指定坐标系映射旋转矩阵,使用Promise异步方式返回结果。 5643 5644**系统能力**:SystemCapability.Sensors.Sensor 5645 5646**参数**: 5647 5648| 参数名 | 类型 | 必填 | 说明 | 5649| ---------------- | ----------------------------------------- | ---- | ---------------- | 5650| inRotationVector | Array<number> | 是 | 旋转矩阵。 | 5651| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 指定坐标系方向。 | 5652 5653**返回值**: 5654 5655| 类型 | 说明 | 5656| ---------------------------------- | ---------------------- | 5657| Promise<Array<number>> | Promise对象,使用异步方式返回转换后的旋转矩阵。 | 5658 5659**错误码**: 5660 5661以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5662 5663| 错误码ID | 错误信息 | 5664| -------- | ------------------------------------------------------------ | 5665| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5666| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5667 5668**示例** : 5669 5670```ts 5671import { sensor } from '@kit.SensorServiceKit'; 5672import { BusinessError } from '@kit.BasicServicesKit'; 5673 5674// 使用try catch对可能出现的异常进行捕获 5675try { 5676 let rotationMatrix = [ 5677 1, 0, 0, 5678 0, 0.87, -0.50, 5679 0, 0.50, 0.87 5680 ]; 5681 const promise = sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }); 5682 promise.then((data: Array<number>) => { 5683 for (let i = 0; i < data.length; i++) { 5684 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5685 } 5686 }, (err: BusinessError) => { 5687 console.error(`Failed to transform rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5688 }); 5689} catch (error) { 5690 let e: BusinessError = error as BusinessError; 5691 console.error(`Failed to transform rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5692} 5693``` 5694 5695## sensor.getQuaternion<sup>9+</sup> 5696 5697getQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 5698 5699根据旋转向量计算归一化四元数,使用Callback异步方式返回结果。 5700 5701**系统能力**:SystemCapability.Sensors.Sensor 5702 5703**参数**: 5704 5705| 参数名 | 类型 | 必填 | 说明 | 5706| -------------- | ---------------------------------------- | ---- | -------------- | 5707| rotationVector | Array<number> | 是 | 旋转矢量。 | 5708| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回归一化四元数。 | 5709 5710**错误码**: 5711 5712以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5713 5714| 错误码ID | 错误信息 | 5715| -------- | ------------------------------------------------------------ | 5716| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5717| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5718 5719**示例**: 5720 5721```ts 5722import { sensor } from '@kit.SensorServiceKit'; 5723import { BusinessError } from '@kit.BasicServicesKit'; 5724 5725// 使用try catch对可能出现的异常进行捕获 5726try { 5727 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5728 sensor.getQuaternion(rotationVector, (err: BusinessError, data: Array<number>) => { 5729 if (err) { 5730 console.error(`Failed to get quaternion. Code: ${err.code}, message: ${err.message}`); 5731 return; 5732 } 5733 for (let i = 0; i < data.length; i++) { 5734 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5735 } 5736 }) 5737} catch (error) { 5738 let e: BusinessError = error as BusinessError; 5739 console.error(`Failed to get quaternion. Code: ${e.code}, message: ${e.message}`); 5740} 5741``` 5742 5743## sensor.getQuaternion<sup>9+</sup> 5744 5745getQuaternion(rotationVector: Array<number>): Promise<Array<number>> 5746 5747根据旋转向量计算归一化四元数,使用Promise异步方式返回结果。 5748 5749**系统能力**:SystemCapability.Sensors.Sensor 5750 5751**参数**: 5752 5753| 参数名 | 类型 | 必填 | 说明 | 5754| -------------- | ------------------- | ---- | -------------- | 5755| rotationVector | Array<number> | 是 | 旋转矢量。 | 5756 5757**返回值**: 5758 5759| 类型 | 说明 | 5760| ---------------------------------- | ------------ | 5761| Promise<Array<number>> | Promise,使用异步方式对象返归一化回四元数。 | 5762 5763**错误码**: 5764 5765以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5766 5767| 错误码ID | 错误信息 | 5768| -------- | ------------------------------------------------------------ | 5769| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5770| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5771 5772**示例**: 5773 5774```ts 5775import { sensor } from '@kit.SensorServiceKit'; 5776import { BusinessError } from '@kit.BasicServicesKit'; 5777 5778// 使用try catch对可能出现的异常进行捕获 5779try { 5780 let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877]; 5781 const promise = sensor.getQuaternion(rotationVector); 5782 promise.then((data: Array<number>) => { 5783 for (let i = 0; i < data.length; i++) { 5784 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5785 } 5786 }, (err: BusinessError) => { 5787 console.error(`Failed to get quaternion. Code: ${err.code}, message: ${err.message}`); 5788 }); 5789} catch (error) { 5790 let e: BusinessError = error as BusinessError; 5791 console.error(`Failed to get quaternion. Code: ${e.code}, message: ${e.message}`); 5792} 5793``` 5794 5795## sensor.getOrientation<sup>9+</sup> 5796 5797getOrientation(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 5798 5799根据旋转矩阵计算设备方向,使用Callback异步方式返回结果。 5800 5801**系统能力**:SystemCapability.Sensors.Sensor 5802 5803**参数**: 5804 5805| 参数名 | 类型 | 必填 | 说明 | 5806| -------------- | ---------------------------------------- | ---- | --------------------------------- | 5807| rotationMatrix | Array<number> | 是 | 旋转矩阵。 | 5808| callback | AsyncCallback<Array<number>> | 是 | 回调函数,异步返回围绕z、x、y轴方向的旋转角度。 | 5809 5810**错误码**: 5811 5812以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5813 5814| 错误码ID | 错误信息 | 5815| -------- | ------------------------------------------------------------ | 5816| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5817| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5818 5819**示例**: 5820 5821```ts 5822import { sensor } from '@kit.SensorServiceKit'; 5823import { BusinessError } from '@kit.BasicServicesKit'; 5824 5825// 使用try catch对可能出现的异常进行捕获 5826try { 5827 let preRotationMatrix = [ 5828 1, 0, 0, 5829 0, 0.87, -0.50, 5830 0, 0.50, 0.87 5831 ]; 5832 sensor.getOrientation(preRotationMatrix, (err: BusinessError, data: Array<number>) => { 5833 if (err) { 5834 console.error(`Failed to get orientation. Code: ${err.code}, message: ${err.message}`); 5835 return; 5836 } 5837 if (data.length < 3) { 5838 console.error("Failed to get orientation, length" + data.length); 5839 } 5840 console.info("Succeeded in getting data. Z: " + data[0]); 5841 console.info("Succeeded in getting data. X: " + data[1]); 5842 console.info("Succeeded in getting data. Y: " + data[2]); 5843 }) 5844} catch (error) { 5845 let e: BusinessError = error as BusinessError; 5846 console.error(`Failed to get orientation. Code: ${e.code}, message: ${e.message}`); 5847} 5848``` 5849 5850## sensor.getOrientation<sup>9+</sup> 5851 5852getOrientation(rotationMatrix: Array<number>): Promise<Array<number>> 5853 5854根据旋转矩阵计算设备的方向,使用Promise异步方式返回结果。 5855 5856**系统能力**:SystemCapability.Sensors.Sensor 5857 5858**参数**: 5859 5860| 参数名 | 类型 | 必填 | 说明 | 5861| -------------- | ------------------- | ---- | -------------- | 5862| rotationMatrix | Array<number> | 是 | 旋转矩阵。 | 5863 5864**返回值**: 5865 5866| 类型 | 说明 | 5867| ---------------------------------- | --------------------------------- | 5868| Promise<Array<number>> | Promise对象,使用异步方式返回围绕z、x、y轴方向的旋转角度。 | 5869 5870**错误码**: 5871 5872以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5873 5874| 错误码ID | 错误信息 | 5875| -------- | ------------------------------------------------------------ | 5876| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5877| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5878 5879**示例**: 5880 5881```ts 5882import { sensor } from '@kit.SensorServiceKit'; 5883import { BusinessError } from '@kit.BasicServicesKit'; 5884 5885// 使用try catch对可能出现的异常进行捕获 5886try { 5887 let preRotationMatrix = [ 5888 1, 0, 0, 5889 0, 0.87, -0.50, 5890 0, 0.50, 0.87 5891 ]; 5892 const promise = sensor.getOrientation(preRotationMatrix); 5893 promise.then((data: Array<number>) => { 5894 for (let i = 0; i < data.length; i++) { 5895 console.info('Succeeded in getting data[' + i + ']: ' + data[i]); 5896 } 5897 }, (err: BusinessError) => { 5898 console.error(`Failed to getOrientation. Code: ${err.code}, message: ${err.message}`); 5899 }); 5900} catch (error) { 5901 let e: BusinessError = error as BusinessError; 5902 console.error(`Failed to getOrientation Code: ${e.code}, message: ${e.message}`); 5903} 5904``` 5905 5906## sensor.getRotationMatrix<sup>9+</sup> 5907 5908getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void 5909 5910根据重力矢量和地磁矢量计算旋转矩阵,使用Callback异步方式返回结果。 5911 5912**系统能力**:SystemCapability.Sensors.Sensor 5913 5914**参数**: 5915 5916| 参数名 | 类型 | 必填 | 说明 | 5917| ----------- | ------------------------------------------------------------ | ---- | -------------- | 5918| gravity | Array<number> | 是 | 重力矢量。 | 5919| geomagnetic | Array<number> | 是 | 地磁矢量。 | 5920| callback | AsyncCallback<[RotationMatrixResponse](#rotationmatrixresponse)> | 是 | 回调函数,异步返回旋转矩阵。 | 5921 5922**错误码**: 5923 5924以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5925 5926| 错误码ID | 错误信息 | 5927| -------- | ------------------------------------------------------------ | 5928| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5929| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5930 5931**示例**: 5932 5933```ts 5934import { sensor } from '@kit.SensorServiceKit'; 5935import { BusinessError } from '@kit.BasicServicesKit'; 5936 5937// 使用try catch对可能出现的异常进行捕获 5938try { 5939 let gravity = [-0.27775216, 0.5351276, 9.788099]; 5940 let geomagnetic = [210.87253, -78.6096, -111.44444]; 5941 sensor.getRotationMatrix(gravity, geomagnetic, (err: BusinessError, data: sensor.RotationMatrixResponse) => { 5942 if (err) { 5943 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5944 return; 5945 } 5946 console.info('Succeeded in getting rotationMatrix' + JSON.stringify(data)); 5947 }) 5948} catch (error) { 5949 let e: BusinessError = error as BusinessError; 5950 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 5951} 5952``` 5953 5954## sensor.getRotationMatrix<sup>9+</sup> 5955 5956getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>): Promise<RotationMatrixResponse> 5957 5958根据重力矢量和地磁矢量计算旋转矩阵,使用Promise异步方式返回结果。 5959 5960**系统能力**:SystemCapability.Sensors.Sensor 5961 5962**参数**: 5963 5964| 参数名 | 类型 | 必填 | 说明 | 5965| ----------- | ------------------- | ---- | -------------- | 5966| gravity | Array<number> | 是 | 重力向量。 | 5967| geomagnetic | Array<number> | 是 | 地磁矢量。 | 5968 5969**返回值**: 5970 5971| 类型 | 说明 | 5972| ------------------------------------------------------------ | -------------- | 5973| Promise<[RotationMatrixResponse](#rotationmatrixresponse)> | Promise对象,使用异步方式返回旋转矩阵。 | 5974 5975**错误码**: 5976 5977以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 5978 5979| 错误码ID | 错误信息 | 5980| -------- | ------------------------------------------------------------ | 5981| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 5982| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 5983 5984**示例**: 5985 5986```ts 5987import { sensor } from '@kit.SensorServiceKit'; 5988import { BusinessError } from '@kit.BasicServicesKit'; 5989 5990// 使用try catch对可能出现的异常进行捕获 5991try { 5992 let gravity = [-0.27775216, 0.5351276, 9.788099]; 5993 let geomagnetic = [210.87253, -78.6096, -111.44444]; 5994 const promise = sensor.getRotationMatrix(gravity, geomagnetic); 5995 promise.then((data: sensor.RotationMatrixResponse) => { 5996 console.info('Succeeded in getting rotationMatrix' + JSON.stringify(data)); 5997 }, (err: BusinessError) => { 5998 console.error(`Failed to get rotationMatrix. Code: ${err.code}, message: ${err.message}`); 5999 }); 6000} catch (error) { 6001 let e: BusinessError = error as BusinessError; 6002 console.error(`Failed to get rotationMatrix. Code: ${e.code}, message: ${e.message}`); 6003} 6004``` 6005 6006## sensor.getSensorList<sup>9+</sup> 6007 6008getSensorList(callback: AsyncCallback<Array<Sensor>>): void 6009 6010获取设备上的所有传感器信息,使用Callback异步方式返回结果。 6011 6012**系统能力**:SystemCapability.Sensors.Sensor 6013 6014**参数**: 6015 6016| 参数名 | 类型 | 必填 | 说明 | 6017| -------- | ---------------------------------------------- | ---- | ---------------- | 6018| callback | AsyncCallback<Array<[Sensor](#sensor9)>> | 是 | 回调函数,异步返回传感器属性列表。 | 6019 6020**错误码**: 6021 6022以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 6023 6024| 错误码ID | 错误信息 | 6025| -------- | ------------------------------------------------------------ | 6026| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6027| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6028 6029**示例**: 6030 6031```ts 6032import { sensor } from '@kit.SensorServiceKit'; 6033import { BusinessError } from '@kit.BasicServicesKit'; 6034 6035// 使用try catch对可能出现的异常进行捕获 6036try { 6037 sensor.getSensorList((err: BusinessError, data: Array<sensor.Sensor>) => { 6038 if (err) { 6039 console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); 6040 return; 6041 } 6042 for (let i = 0; i < data.length; i++) { 6043 console.info('Succeeded in getting data[' + i + ']: ' + JSON.stringify(data[i])); 6044 } 6045 }); 6046} catch (error) { 6047 let e: BusinessError = error as BusinessError; 6048 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 6049} 6050``` 6051 6052## sensor.getSensorList<sup>9+</sup> 6053 6054 getSensorList(): Promise<Array<Sensor>> 6055 6056获取设备上的所有传感器信息,使用Promise异步方式返回结果。 6057 6058**系统能力**:SystemCapability.Sensors.Sensor 6059 6060**返回值**: 6061 6062| 类型 | 说明 | 6063| ---------------------------------------- | ---------------- | 6064| Promise<Array<[Sensor](#sensor9)>> | Promise对象,使用异步方式返回传感器属性列表。 | 6065 6066**错误码**: 6067 6068以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 6069 6070| 错误码ID | 错误信息 | 6071| -------- | ------------------------------------------------------------ | 6072| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6073| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6074 6075**示例**: 6076 6077```ts 6078import { sensor } from '@kit.SensorServiceKit'; 6079import { BusinessError } from '@kit.BasicServicesKit'; 6080 6081// 使用try catch对可能出现的异常进行捕获 6082try { 6083 sensor.getSensorList().then((data: Array<sensor.Sensor>) => { 6084 for (let i = 0; i < data.length; i++) { 6085 console.info('Succeeded in getting data[' + i + ']: ' + JSON.stringify(data[i])); 6086 } 6087 }, (err: BusinessError) => { 6088 console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`); 6089 }); 6090} catch (error) { 6091 let e: BusinessError = error as BusinessError; 6092 console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`); 6093} 6094``` 6095 6096## sensor.getSensorListSync<sup>12+</sup> 6097 6098getSensorListSync(): Array<Sensor> 6099 6100获取设备上的所有传感器信息,使用同步方式返回结果。 6101 6102**系统能力**:SystemCapability.Sensors.Sensor 6103 6104**返回值**: 6105 6106| 类型 | 说明 | 6107| --------------------------------------- | -------------------------------- | 6108| Array<[Sensor](#sensor9)> | 使用同步方式返回传感器属性列表。 | 6109 6110**错误码**: 6111 6112以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 6113 6114| 错误码ID | 错误信息 | 6115| -------- | ------------------ | 6116| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6117 6118**示例**: 6119 6120```ts 6121import { sensor } from '@kit.SensorServiceKit'; 6122import { BusinessError } from '@kit.BasicServicesKit'; 6123 6124// 使用try catch对可能出现的异常进行捕获 6125try { 6126 let ret = sensor.getSensorListSync() 6127 for (let i = 0; i < ret.length; i++) { 6128 console.info('Succeeded in getting sensor: ' + JSON.stringify(ret[i])); 6129 } 6130} catch(error) { 6131 let e: BusinessError = error as BusinessError; 6132 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 6133} 6134``` 6135 6136## sensor.getSingleSensor<sup>9+</sup> 6137 6138getSingleSensor(type: SensorId, callback: AsyncCallback<Sensor>): void 6139 6140获取指定传感器类型的属性信息,使用Callback异步方式返回结果。 6141 6142**系统能力**:SystemCapability.Sensors.Sensor 6143 6144**参数**: 6145 6146| 参数名 | 类型 | 必填 | 说明 | 6147| -------- | --------------------------------------- | ---- | ---------------- | 6148| type | [SensorId](#sensorid9) | 是 | 指定传感器类型。 | 6149| callback | AsyncCallback<[Sensor](#sensor9)> | 是 | 回调函数,异步返回指定传感器的属性信息。 | 6150 6151**错误码**: 6152 6153以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 6154 6155| 错误码ID | 错误信息 | 6156| -------- | ------------------------------------------------------------ | 6157| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6158| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6159| 14500102 | The sensor is not supported by the device. | 6160 6161**示例**: 6162 6163```ts 6164import { sensor } from '@kit.SensorServiceKit'; 6165import { BusinessError } from '@kit.BasicServicesKit'; 6166 6167// 使用try catch对可能出现的异常进行捕获 6168try { 6169 sensor.getSingleSensor(sensor.SensorId.ACCELEROMETER, (err: BusinessError, data: sensor.Sensor) => { 6170 if (err) { 6171 console.error(`Failed to get singleSensor. Code: ${err.code}, message: ${err.message}`); 6172 return; 6173 } 6174 console.info('Succeeded in getting sensor: ' + JSON.stringify(data)); 6175 }); 6176} catch (error) { 6177 let e: BusinessError = error as BusinessError; 6178 console.error(`Failed to get singleSensor. Code: ${e.code}, message: ${e.message}`); 6179} 6180``` 6181 6182## sensor.getSingleSensor<sup>9+</sup> 6183 6184 getSingleSensor(type: SensorId): Promise<Sensor> 6185 6186获取指定类型的传感器信息,使用Promise异步方式返回结果。 6187 6188**系统能力**:SystemCapability.Sensors.Sensor 6189 6190**参数**: 6191 6192| 参数名 | 类型 | 必填 | 说明 | 6193| ------ | ---------------------- | ---- | ------------ | 6194| type | [SensorId](#sensorid9) | 是 | 传感器类型。 | 6195 6196**返回值**: 6197 6198| 类型 | 说明 | 6199| --------------------------------- | ---------------------------- | 6200| Promise<[Sensor](#sensor9)> | 使用异步方式返回传感器信息。 | 6201 6202**错误码**: 6203 6204以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 6205 6206| 错误码ID | 错误信息 | 6207| -------- | ------------------------------------------------------------ | 6208| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6209| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6210| 14500102 | The sensor is not supported by the device. | 6211 6212**示例**: 6213 6214```ts 6215import { sensor } from '@kit.SensorServiceKit'; 6216import { BusinessError } from '@kit.BasicServicesKit'; 6217 6218// 使用try catch对可能出现的异常进行捕获 6219try { 6220 sensor.getSingleSensor(sensor.SensorId.ACCELEROMETER).then((data: sensor.Sensor) => { 6221 console.info('Succeeded in getting sensor: ' + JSON.stringify(data)); 6222 }, (err: BusinessError) => { 6223 console.error(`Failed to get singleSensor . Code: ${err.code}, message: ${err.message}`); 6224 }); 6225} catch (error) { 6226 let e: BusinessError = error as BusinessError; 6227 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 6228} 6229``` 6230 6231## sensor.getSingleSensorSync<sup>12+</sup> 6232 6233getSingleSensorSync(type: SensorId): Sensor 6234 6235获取指定类型的传感器信息,使用同步方式返回结果。 6236 6237**系统能力**:SystemCapability.Sensors.Sensor 6238 6239**参数**: 6240 6241| 参数名 | 类型 | 必填 | 说明 | 6242| ------ | ---------------------- | ---- | ------------ | 6243| type | [SensorId](#sensorid9) | 是 | 传感器类型。 | 6244 6245**返回值**: 6246 6247| 类型 | 说明 | 6248| ------ | ---------------------------- | 6249| Sensor | 使用同步方式返回传感器信息。 | 6250 6251**错误码**: 6252 6253以下错误码的详细介绍请参见[传感器错误码](errorcode-sensor.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 6254 6255| 错误码ID | 错误信息 | 6256| -------- | ------------------------------------------------------------ | 6257| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 6258| 14500101 | Service exception.Possible causes:1. Sensor hdf service exception;2. Sensor service ipc exception;3.Sensor data channel exception. | 6259| 14500102 | The sensor is not supported by the device. | 6260 6261**示例**: 6262 6263```ts 6264import { sensor } from '@kit.SensorServiceKit'; 6265import { BusinessError } from '@kit.BasicServicesKit'; 6266 6267// 使用try catch对可能出现的异常进行捕获 6268try { 6269 let ret = sensor.getSingleSensorSync(sensor.SensorId.ACCELEROMETER); 6270 console.info('Succeeded in getting sensor: ' + JSON.stringify(ret)); 6271} catch (error) { 6272 let e: BusinessError = error as BusinessError; 6273 console.error(`Failed to get singleSensor . Code: ${e.code}, message: ${e.message}`); 6274} 6275``` 6276 6277## SensorId<sup>9+</sup> 6278 6279表示当前支持订阅或取消订阅的传感器类型。 6280 6281**系统能力**:SystemCapability.Sensors.Sensor 6282 6283| 名称 | 值 | 说明 | 6284| --------------------------- | ---- | ------------------------------------------------------------ | 6285| ACCELEROMETER | 1 | 加速度传感器。<br/>**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 | 6286| GYROSCOPE | 2 | 陀螺仪传感器。<br/>**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 | 6287| AMBIENT_LIGHT | 5 | 环境光传感器。 | 6288| MAGNETIC_FIELD | 6 | 磁场传感器。 | 6289| BAROMETER | 8 | 气压计传感器。 | 6290| HALL | 10 | 霍尔传感器。 | 6291| PROXIMITY | 12 | 接近光传感器。 | 6292| HUMIDITY | 13 | 湿度传感器。 | 6293| ORIENTATION | 256 | 方向传感器。<br/>**原子化服务API**:从API Version 11开始,该接口在支持原子化服务中使用。 | 6294| GRAVITY | 257 | 重力传感器。 | 6295| LINEAR_ACCELEROMETER | 258 | 线性加速度传感器。 | 6296| ROTATION_VECTOR | 259 | 旋转矢量传感器。 | 6297| AMBIENT_TEMPERATURE | 260 | 环境温度传感器。 | 6298| MAGNETIC_FIELD_UNCALIBRATED | 261 | 未校准磁场传感器。 | 6299| GYROSCOPE_UNCALIBRATED | 263 | 未校准陀螺仪传感器。 | 6300| SIGNIFICANT_MOTION | 264 | 有效运动传感器。 | 6301| PEDOMETER_DETECTION | 265 | 计步检测传感器。 | 6302| PEDOMETER | 266 | 计步传感器。 | 6303| HEART_RATE | 278 | 心率传感器。 | 6304| WEAR_DETECTION | 280 | 佩戴检测传感器。 | 6305| ACCELEROMETER_UNCALIBRATED | 281 | 未校准加速度计传感器。 | 6306 6307 6308## SensorInfoParam<sup>19+</sup> 6309 6310传感器传入设置参数,多传感器情况下通过deviceId、sensorIndex控制指定传感器。 6311 6312**系统能力**:SystemCapability.Sensors.Sensor 6313 6314**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 6315 6316 6317| 名称 | 类型 | 只读 | 可选 | 说明 | 6318|--------------|----------|-------|------|----------------- | 6319| deviceId | number | 否 | 是 | 设备ID:默认值为-1,表示本地设备,设备ID需通过[getSensorList](#sensorgetsensorlist9)查询或者监听设备上下线接口[on](#sensorstatuschange19)获取。<br/>**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 | 6320| sensorIndex | number | 否 | 是 | 传感器索引:默认值为0,为设备上的默认传感器,其它传感器ID需通过[getSensorList](#sensorgetsensorlist9)查询或者监听设备上下线接口[on](#sensorstatuschange19)获取。<br/>**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 | 6321 6322 6323## SensorStatusEvent<sup>19+</sup> 6324 6325设备状态变化事件数据。 6326 6327**系统能力**:SystemCapability.Sensors.Sensor 6328 6329| 名称 | 类型 | 只读 | 可选 | 说明 | 6330|----------------|---------|-----|-----|-----------------------------| 6331| timestamp | number | 否 | 否 | 事件发生的时间戳。 | 6332| sensorId | number | 否 | 否 | 传感器ID。 | 6333| sensorIndex | number | 否 | 否 | 传感器索引。 | 6334| isSensorOnline | boolean | 否 | 否 | 传感器上线或者下线,true为上线,false为下线。 | 6335| deviceId | number | 否 | 否 | 设备ID。 | 6336| deviceName | string | 否 | 否 | 设备名称。 | 6337 6338## SensorAccuracy<sup>11+</sup> 6339 6340传感器数据的精度。 6341 6342**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6343 6344**系统能力**:SystemCapability.Sensors.Sensor 6345 6346| 名称 | 值 | 说明 | 6347| --------- | ---- | ------------------------ | 6348| ACCURACY_UNRELIABLE | 0 | 传感器数据不可信。 | 6349| ACCURACY_LOW | 1 | 传感器低挡位精度。 | 6350| ACCURACY_MEDIUM | 2 | 传感器中挡位精度。 | 6351| ACCURACY_HIGH | 3 | 传感器高挡位精度。 | 6352 6353## Response 6354 6355传感器数据的时间戳。 6356 6357**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6358 6359**系统能力**:SystemCapability.Sensors.Sensor 6360 6361| 名称 | 类型 | 只读 | 可选 | 说明 | 6362| --------- | ------ | ---- | ---- | ------------------------ | 6363| timestamp | number | 否 | 是 | 传感器数据上报的时间戳。从设备开机开始计时到上报数据的时间,单位 : ns。 | 6364| accuracy<sup>11+</sup> | [SensorAccuracy](#sensoraccuracy11)<sup>11+</sup> | 否 | 否 | 传感器数据上报的精度挡位值。 | 6365 6366## Sensor<sup>9+</sup> 6367 6368指示传感器信息。 6369 6370**系统能力**:SystemCapability.Sensors.Sensor 6371 6372| 名称 | 类型 | 只读 | 可选 | 说明 | 6373|-----------------------------|---------|----|----|------------------| 6374| sensorName | string | 否 | 否 | 传感器名称。 | 6375| vendorName | string | 否 | 否 | 传感器供应商。 | 6376| firmwareVersion | string | 否 | 否 | 传感器固件版本。 | 6377| hardwareVersion | string | 否 | 否 | 传感器硬件版本。 | 6378| sensorId | number | 否 | 否 | 传感器类型id。 | 6379| maxRange | number | 否 | 否 | 传感器测量范围的最大值。 | 6380| minSamplePeriod | number | 否 | 否 | 允许的最小采样周期。 | 6381| maxSamplePeriod | number | 否 | 否 | 允许的最大采样周期。 | 6382| precision | number | 否 | 否 | 传感器精度。 | 6383| power | number | 否 | 否 | 传感器功率的估计值,单位:mA。 | 6384| sensorIndex<sup>19+</sup> | number | 否 | 是 | 传感器索引。 | 6385| deviceId<sup>19+</sup> | number | 否 | 是 | 设备ID。 | 6386| deviceName<sup>19+</sup> | string | 否 | 是 | 设备名称。 | 6387| isLocalSensor<sup>19+</sup> | boolean | 否 | 是 | 是否本地传感器。 | 6388 6389## AccelerometerResponse 6390 6391加速度传感器数据,继承于[Response](#response)。 6392 6393**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6394 6395**系统能力**:SystemCapability.Sensors.Sensor 6396 6397 6398| 名称 | 类型 | 只读 | 可选 | 说明 | 6399| ---- | ------ | ---- | ---- | ---------------------------------------------------------- | 6400| x | number | 否 | 是 | 施加在设备x轴的加速度,单位 : m/s²;取值为实际上报物理量。 | 6401| y | number | 否 | 是 | 施加在设备y轴的加速度,单位 : m/s²;取值为实际上报物理量。 | 6402| z | number | 否 | 是 | 施加在设备z轴的加速度,单位 : m/s²;取值为实际上报物理量。 | 6403 6404 6405## LinearAccelerometerResponse 6406 6407线性加速度传感器数据,继承于[Response](#response)。 6408 6409**系统能力**:SystemCapability.Sensors.Sensor 6410 6411 6412| 名称 | 类型 | 只读 | 可选 | 说明 | 6413| ---- | ------ | ---- | ---- | ---------------------------------------- | 6414| x | number | 否 | 是 | 施加在设备x轴的线性加速度,单位 : m/s²。 | 6415| y | number | 否 | 是 | 施加在设备y轴的线性加速度,单位 : m/s²。 | 6416| z | number | 否 | 是 | 施加在设备z轴的线性加速度,单位 : m/s²。 | 6417 6418 6419## AccelerometerUncalibratedResponse 6420 6421未校准加速度计传感器数据,继承于[Response](#response)。 6422 6423**系统能力**:SystemCapability.Sensors.Sensor 6424 6425 6426| 名称 | 类型 | 只读 | 可选 | 说明 | 6427| ----- | ------ | ---- | ---- | ---------------------------------------------- | 6428| x | number | 否 | 是 | 施加在设备x轴未校准的加速度,单位 : m/s²。 | 6429| y | number | 否 | 是 | 施加在设备y轴未校准的加速度,单位 : m/s²。 | 6430| z | number | 否 | 是 | 施加在设备z轴未校准的加速度,单位 : m/s²。 | 6431| biasX | number | 否 | 是 | 施加在设备x轴未校准的加速度偏量,单位 : m/s²。 | 6432| biasY | number | 否 | 是 | 施加在设备y轴未校准的加速度偏量,单位 : m/s²。 | 6433| biasZ | number | 否 | 是 | 施加在设备z轴未校准的加速度偏量,单位 : m/s²。 | 6434 6435 6436## GravityResponse 6437 6438重力传感器数据,继承于[Response](#response)。 6439 6440**系统能力**:SystemCapability.Sensors.Sensor 6441 6442 6443| 名称 | 类型 | 只读 | 可选 | 说明 | 6444| ---- | ------ | ---- | ---- | ---------------------------------------- | 6445| x | number | 否 | 是 | 施加在设备x轴的重力加速度,单位 : m/s²。 | 6446| y | number | 否 | 是 | 施加在设备y轴的重力加速度,单位 : m/s²。 | 6447| z | number | 否 | 是 | 施加在设备z轴的重力加速度,单位 : m/s²。 | 6448 6449 6450## OrientationResponse 6451 6452方向传感器数据,继承于[Response](#response)。 6453 6454**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6455 6456**系统能力**:SystemCapability.Sensors.Sensor 6457 6458 6459| 名称 | 类型 | 只读 | 可选 | 说明 | 6460| ----- | ------ | ---- | ---- | ----------------------------------------------------- | 6461| alpha | number | 否 | 是 | 设备围绕Z轴的旋转角度,单位:度;取值范围为0-360度。 | 6462| beta | number | 否 | 是 | 设备围绕X轴的旋转角度,单位:度;取值范围为0-±180度。 | 6463| gamma | number | 否 | 是 | 设备围绕Y轴的旋转角度,单位:度;取值范围为0-±90度。 | 6464 6465 6466## RotationVectorResponse 6467 6468旋转矢量传感器数据,继承于[Response](#response)。 6469 6470**系统能力**:SystemCapability.Sensors.Sensor 6471 6472 6473| 名称 | 类型 | 只读 | 可选 | 说明 | 6474| ---- | ------ | ---- | ---- | ----------------- | 6475| x | number | 否 | 是 | 旋转矢量x轴分量。 | 6476| y | number | 否 | 是 | 旋转矢量y轴分量。 | 6477| z | number | 否 | 是 | 旋转矢量z轴分量。 | 6478| w | number | 否 | 是 | 标量,描述设备相对于某个参考方向的旋转状态,单位:弧度。 | 6479 6480 6481## GyroscopeResponse 6482 6483陀螺仪传感器数据,继承于[Response](#response)。 6484 6485**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6486 6487**系统能力**:SystemCapability.Sensors.Sensor 6488 6489 6490| 名称 | 类型 | 只读 | 可选 | 说明 | 6491| ---- | ------ | ---- | ---- | ------------------------------------------------------ | 6492| x | number | 否 | 是 | 设备x轴的旋转角速度,单位rad/s;取值为实际上报物理量。 | 6493| y | number | 否 | 是 | 设备y轴的旋转角速度,单位rad/s;取值为实际上报物理量。 | 6494| z | number | 否 | 是 | 设备z轴的旋转角速度,单位rad/s;取值为实际上报物理量。 | 6495 6496 6497## GyroscopeUncalibratedResponse 6498 6499未校准陀螺仪传感器数据,继承于[Response](#response)。 6500 6501**系统能力**:SystemCapability.Sensors.Sensor 6502 6503 6504| 名称 | 类型 | 只读 | 可选 | 说明 | 6505| ----- | ------ | ---- | ---- | ------------------------------------------ | 6506| x | number | 否 | 是 | 设备x轴未校准的旋转角速度,单位rad/s。 | 6507| y | number | 否 | 是 | 设备y轴未校准的旋转角速度,单位rad/s。 | 6508| z | number | 否 | 是 | 设备z轴未校准的旋转角速度,单位rad/s。 | 6509| biasX | number | 否 | 是 | 设备x轴未校准的旋转角速度偏量,单位rad/s。 | 6510| biasY | number | 否 | 是 | 设备y轴未校准的旋转角速度偏量,单位rad/s。 | 6511| biasZ | number | 否 | 是 | 设备z轴未校准的旋转角速度偏量,单位rad/s。 | 6512 6513 6514## SignificantMotionResponse 6515 6516有效运动传感器数据,继承于[Response](#response)。 6517 6518**系统能力**:SystemCapability.Sensors.Sensor 6519 6520 6521| 名称 | 类型 | 只读 | 可选 | 说明 | 6522| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 6523| scalar | number | 否 | 是 | 表示剧烈运动程度。测量三个物理轴(x、y 和 z)上,设备是否存在大幅度运动;若存在大幅度运动则数据上报为1。 | 6524 6525 6526## ProximityResponse 6527 6528接近光传感器数据,继承于[Response](#response)。 6529 6530**系统能力**:SystemCapability.Sensors.Sensor 6531 6532 6533| 名称 | 类型 | 只读 | 可选 | 说明 | 6534| -------- | ------ | ---- | ---- | ---------------------------------------------------------- | 6535| distance | number | 否 | 是 | 可见物体与设备显示器的接近程度。0表示接近,大于0表示远离。 | 6536 6537 6538## LightResponse 6539 6540环境光传感器数据,继承于[Response](#response)。 6541 6542**系统能力**:SystemCapability.Sensors.Sensor 6543 6544 6545| 名称 | 类型 | 只读 | 可选 | 说明 | 6546| ------------------------------- | ------ | ---- | ---- | ------------------------------------------------------------ | 6547| intensity | number | 否 | 是 | 光强(单位:勒克斯)。 | 6548| colorTemperature<sup>12+</sup> | number | 否 | 是 | 色温(单位:开尔文),可选参数,如果该参数不支持在js层返回未定义,支持则返回正常数值。 | 6549| infraredLuminance<sup>12+</sup> | number | 否 | 是 | 红外亮度(单位:cd/m²),可选参数,如果该参数不支持在js层返回未定义,支持则返回正常数值。 | 6550 6551 6552## HallResponse 6553 6554霍尔传感器数据,继承于[Response](#response)。 6555 6556**系统能力**:SystemCapability.Sensors.Sensor 6557 6558 6559| 名称 | 类型 | 只读 | 可选 | 说明 | 6560| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 6561| status | number | 否 | 是 | 显示霍尔状态。测量设备周围是否存在磁力吸引,0表示没有,大于0表示有。 | 6562 6563 6564## MagneticFieldResponse 6565 6566磁场传感器数据,继承于[Response](#response)。 6567 6568**系统能力**:SystemCapability.Sensors.Sensor 6569 6570 6571| 名称 | 类型 | 只读 | 可选 | 说明 | 6572| ---- | ------ | ---- | ---- | ---------------------------- | 6573| x | number | 否 | 是 | x轴环境磁场强度,单位 : μT。 | 6574| y | number | 否 | 是 | y轴环境磁场强度,单位 : μT。 | 6575| z | number | 否 | 是 | z轴环境磁场强度,单位 : μT。 | 6576 6577 6578## MagneticFieldUncalibratedResponse 6579 6580未校准磁场传感器数据,继承于[Response](#response)。 6581 6582**系统能力**:SystemCapability.Sensors.Sensor 6583 6584 6585| 名称 | 类型 | 只读 | 可选 | 说明 | 6586| ----- | ------ | ---- | ---- | -------------------------------------- | 6587| x | number | 否 | 是 | x轴未校准环境磁场强度,单位 : μT。 | 6588| y | number | 否 | 是 | y轴未校准环境磁场强度,单位 : μT。 | 6589| z | number | 否 | 是 | z轴未校准环境磁场强度,单位 : μT。 | 6590| biasX | number | 否 | 是 | x轴未校准环境磁场强度偏量,单位 : μT。 | 6591| biasY | number | 否 | 是 | y轴未校准环境磁场强度偏量,单位 : μT。 | 6592| biasZ | number | 否 | 是 | z轴未校准环境磁场强度偏量,单位 : μT。 | 6593 6594 6595## PedometerResponse 6596 6597计步传感器数据,继承于[Response](#response)。 6598 6599**系统能力**:SystemCapability.Sensors.Sensor 6600 6601 6602| 名称 | 类型 | 只读 | 可选 | 说明 | 6603| ----- | ------ | ---- | ---- | ---------------- | 6604| steps | number | 否 | 是 | 用户的行走步数。 | 6605 6606 6607## HumidityResponse 6608 6609湿度传感器数据,继承于[Response](#response)。 6610 6611**系统能力**:SystemCapability.Sensors.Sensor 6612 6613 6614| 名称 | 类型 | 只读 | 可选 | 说明 | 6615| -------- | ------ | ---- | ---- | --------------------------------------------------------- | 6616| humidity | number | 否 | 是 | 湿度值。测量环境的相对湿度,以百分比 (%) 表示。 | 6617 6618 6619## PedometerDetectionResponse 6620 6621计步检测传感器数据,继承于[Response](#response)。 6622 6623**系统能力**:SystemCapability.Sensors.Sensor 6624 6625 6626| 名称 | 类型 | 只读 | 可选 | 说明 | 6627| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 6628| scalar | number | 否 | 是 | 计步器检测。检测用户的计步动作,如果取值为1则代表用户产生了计步行走的动作,取值为0则代表用户没有发生运动。 | 6629 6630 6631## AmbientTemperatureResponse 6632 6633温度传感器数据,继承于[Response](#response)。 6634 6635**系统能力**:SystemCapability.Sensors.Sensor 6636 6637 6638| 名称 | 类型 | 只读 | 可选 | 说明 | 6639| ----------- | ------ | ---- | ---- | -------------------------- | 6640| temperature | number | 否 | 是 | 环境温度(单位:摄氏度)。 | 6641 6642 6643## BarometerResponse 6644 6645气压计传感器数据,继承于[Response](#response)。 6646 6647**系统能力**:SystemCapability.Sensors.Sensor 6648 6649 6650| 名称 | 类型 | 只读 | 可选 | 说明 | 6651| -------- | ------ | ---- | ---- | ---------------------- | 6652| pressure | number | 否 | 是 | 压力值(单位:百帕)。 | 6653 6654 6655## HeartRateResponse 6656 6657心率传感器数据,继承于[Response](#response)。 6658 6659**系统能力**:SystemCapability.Sensors.Sensor 6660 6661 6662| 名称 | 类型 | 只读 | 可选 | 说明 | 6663| --------- | ------ | ---- | ---- | --------------------------------------- | 6664| heartRate | number | 否 | 是 | 心率值。测量用户的心率数值,单位:bpm。 | 6665 6666 6667## WearDetectionResponse 6668 6669佩戴检测传感器数据,继承于[Response](#response)。 6670 6671**系统能力**:SystemCapability.Sensors.Sensor 6672 6673 6674| 名称 | 类型 | 只读 | 可选 | 说明 | 6675| ----- | ------ | ---- | ---- | ------------------------------------------------ | 6676| value | number | 否 | 是 | 表示设备是否被穿戴(1表示已穿戴,0表示未穿戴)。 | 6677 6678 6679## Options 6680 6681设置传感器上报频率。 6682 6683**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6684 6685**系统能力**:SystemCapability.Sensors.Sensor 6686 6687| 名称 | 类型 | 只读 | 可选 | 说明 | 6688| -------- | ----------------------------------------------------------- | ---- | ---- |--------------------------------------------------------------------------------------------| 6689| interval | number\|[SensorFrequency](#sensorfrequency11)<sup>11+</sup> | 否 | 是 | 表示传感器的上报频率,默认值为200000000ns。该属性有最小值和最大值的限制,由硬件支持的上报频率决定,当设置频率大于最大值时以最大值上报数据,小于最小值时以最小值上报数据。 | 6690| sensorInfoParam<sup>19+</sup> | [SensorInfoParam](#sensorinfoparam19) | 否 | 是 | 传感器传入设置参数,可指定deviceId、sensorIndex。<br/>**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 | 6691 6692## SensorFrequency<sup>11+</sup> 6693 6694type SensorFrequency = 'game' | 'ui' | 'normal' 6695 6696传感器上报频率模式。 6697 6698**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 6699 6700**系统能力**:SystemCapability.Sensors.Sensor 6701 6702| 类型 | 说明 | 6703| -------- | ------------------------------------------------------------ | 6704| 'game' | 用于指定传感器上报频率,频率值为20000000ns,该频率被设置在硬件支持的频率范围内时会生效,值固定为'game'字符串。 | 6705| 'ui' | 用于指定传感器上报频率,频率值为60000000ns,该频率被设置在硬件支持的频率范围内时会生效,值固定为'ui'字符串。 | 6706| 'normal' | 用于指定传感器上报频率,频率值为200000000ns,该频率被设置在硬件支持的频率范围内时会生效,值固定为'normal'字符串。 | 6707 6708## RotationMatrixResponse 6709 6710设置旋转矩阵响应对象。 6711 6712**系统能力**:SystemCapability.Sensors.Sensor 6713 6714| 名称 | 类型 | 只读 | 可选 | 说明 | 6715| ----------- | ------------------- | ---- | ---- | ---------- | 6716| rotation | Array<number> | 否 | 是 | 旋转矩阵。 | 6717| inclination | Array<number> | 否 | 是 | 倾斜矩阵。 | 6718 6719 6720## CoordinatesOptions 6721 6722设置坐标选项对象。 6723 6724**系统能力**:SystemCapability.Sensors.Sensor 6725 6726| 名称 | 类型 | 只读 | 可选 | 说明 | 6727| ---- | ------ | ---- | ---- | ----------- | 6728| x | number | 否 | 是 | x坐标方向。 | 6729| y | number | 否 | 是 | y坐标方向。 | 6730 6731 6732## GeomagneticResponse 6733 6734设置地磁响应对象。 6735 6736**系统能力**:SystemCapability.Sensors.Sensor 6737 6738| 名称 | 类型 | 只读 | 可选 | 说明 | 6739| --------------- | ------ | ---- | ---- | -------------------------------------------------- | 6740| x | number | 否 | 是 | 地磁场的北分量。 | 6741| y | number | 否 | 是 | 地磁场的东分量。 | 6742| z | number | 否 | 是 | 地磁场的垂直分量。 | 6743| geomagneticDip | number | 否 | 是 | 地磁倾角,即地球磁场线与水平面的夹角。 | 6744| deflectionAngle | number | 否 | 是 | 地磁偏角,即地磁北方向与正北方向在水平面上的角度。 | 6745| levelIntensity | number | 否 | 是 | 地磁场的水平强度。 | 6746| totalIntensity | number | 否 | 是 | 地磁场的总强度。 | 6747 6748## LocationOptions 6749 6750指示地理位置。 6751 6752**系统能力**:SystemCapability.Sensors.Sensor 6753 6754| 名称 | 类型 | 只读 | 可选 | 说明 | 6755| --------- | ------ | ---- | ---- | ---------- | 6756| latitude | number | 否 | 是 | 纬度。 | 6757| longitude | number | 否 | 是 | 经度。 | 6758| altitude | number | 否 | 是 | 海拔高度。 | 6759 6760## sensor.on<sup>(deprecated)</sup> 6761 6762### ACCELEROMETER<sup>(deprecated)</sup> 6763 6764on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>,options?: Options): void 6765 6766监听加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 6767 6768> **说明**: 6769> 6770> 从API version 9 开始不再维护,建议使用[sensor.on.ACCELEROMETER](#accelerometer9)<sup>9+</sup>代替。 6771 6772**需要权限**:ohos.permission.ACCELEROMETER 6773 6774**系统能力**:SystemCapability.Sensors.Sensor 6775 6776**参数**: 6777 6778| 参数名 | 类型 | 必填 | 说明 | 6779| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6780| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER | 是 | 要订阅的加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。 | 6781| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 注册加速度传感器的回调函数,上报的数据类型为AccelerometerResponse。 | 6782| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6783 6784**示例**: 6785 6786```ts 6787import { sensor } from '@kit.SensorServiceKit'; 6788 6789sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 6790 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6791 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6792 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6793}, 6794 { interval: 100000000 } 6795); 6796``` 6797 6798### LINEAR_ACCELERATION<sup>(deprecated)</sup> 6799 6800on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>, options?: Options): void 6801 6802监听线性加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 6803 6804> **说明**: 6805> 6806> 从API version 9 开始不再维护,建议使用[sensor.on.LINEAR_ACCELEROMETER](#linear_accelerometer9)<sup>9+</sup>代替。 6807 6808**需要权限**:ohos.permission.ACCELEROMETER 6809 6810**系统能力**:SystemCapability.Sensors.Sensor 6811 6812**参数**: 6813 6814| 参数名 | 类型 | 必填 | 说明 | 6815| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6816| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_LINEAR_ACCELERATION | 是 | 要订阅的线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。 | 6817| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 注册线性加速度传感器的回调函数,上报的数据类型为LinearAccelerometerResponse。 | 6818| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6819 6820### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 6821 6822on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void 6823 6824监听未校准加速度计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 6825 6826> **说明**: 6827> 6828> 从API version 9 开始不再维护,建议使用[sensor.on.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9)<sup>9+</sup>代替。 6829 6830**需要权限**:ohos.permission.ACCELEROMETER 6831 6832**系统能力**:SystemCapability.Sensors.Sensor 6833 6834**参数**: 6835 6836| 参数名 | 类型 | 必填 | 说明 | 6837| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6838| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 是 | 要订阅的未校准加速度计传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。 | 6839| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 注册未校准加速度计传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。 | 6840| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6841 6842**示例**: 6843 6844```ts 6845import { sensor } from '@kit.SensorServiceKit'; 6846 6847sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 6848 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6849 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6850 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6851 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 6852 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 6853 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 6854}, 6855 { interval: 100000000 } 6856); 6857 6858``` 6859 6860### GRAVITY<sup>(deprecated)</sup> 6861 6862on(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>,options?: Options): void 6863 6864监听重力传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 6865 6866> **说明**: 6867> 6868> 从API version 9 开始不再维护,建议使用[sensor.on.GRAVITY](#gravity9)<sup>9+</sup>代替。 6869 6870**系统能力**:SystemCapability.Sensors.Sensor 6871 6872**参数**: 6873 6874| 参数名 | 类型 | 必填 | 说明 | 6875| -------- | ---------------------------------------------------------- | ---- | ----------------------------------------------------------- | 6876| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GRAVITY | 是 | 要订阅的重力传感器类型为SENSOR_TYPE_ID_GRAVITY。 | 6877| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 注册重力传感器的回调函数,上报的数据类型为GravityResponse。 | 6878| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6879 6880**示例**: 6881 6882```ts 6883import { sensor } from '@kit.SensorServiceKit'; 6884 6885sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, (data: sensor.GravityResponse) => { 6886 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6887 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6888 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6889}, 6890 { interval: 100000000 } 6891); 6892``` 6893 6894### GYROSCOPE<sup>(deprecated)</sup> 6895 6896on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void 6897 6898监听陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 6899 6900> **说明**: 6901> 6902> 从API version 9 开始不再维护,建议使用[sensor.on.GYROSCOPE](#gyroscope9)<sup>9+</sup>代替。 6903 6904**需要权限**:ohos.permission.GYROSCOPE 6905 6906**系统能力**:SystemCapability.Sensors.Sensor 6907 6908**参数**: 6909 6910| 参数名 | 类型 | 必填 | 说明 | 6911| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6912| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE | 是 | 要订阅的陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。 | 6913| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 注册陀螺仪传感器的回调函数,上报的数据类型为GyroscopeResponse。 | 6914| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6915 6916**示例**: 6917 6918```ts 6919import { sensor } from '@kit.SensorServiceKit'; 6920 6921sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, (data: sensor.GyroscopeResponse) => { 6922 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6923 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6924 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6925}, 6926 { interval: 100000000 } 6927); 6928``` 6929 6930### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 6931 6932on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback:Callback<GyroscopeUncalibratedResponse>, options?: Options): void 6933 6934监听未校准陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 6935 6936> **说明**: 6937> 6938> 从API version 9 开始不再维护,建议使用[sensor.on.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9)<sup>9+</sup>代替。 6939 6940**需要权限**:ohos.permission.GYROSCOPE 6941 6942**系统能力**:SystemCapability.Sensors.Sensor 6943 6944**参数**: 6945 6946| 参数名 | 类型 | 必填 | 说明 | 6947| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6948| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 是 | 要订阅的未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。 | 6949| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 注册未校准陀螺仪传感器的回调函数,上报的数据类型为GyroscopeUncalibratedResponse。 | 6950| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6951 6952**示例**: 6953 6954```ts 6955import { sensor } from '@kit.SensorServiceKit'; 6956 6957sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 6958 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 6959 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 6960 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 6961 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 6962 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 6963 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 6964}, 6965 { interval: 100000000 } 6966); 6967``` 6968 6969### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 6970 6971on(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, options?: Options): void 6972 6973监听有效运动传感器数据变化。如果多次调用该接口,仅最后一次调用生效。 6974 6975> **说明**: 6976> 6977> 从API version 9 开始不再维护,建议使用[sensor.on.SIGNIFICANT_MOTION](#significant_motion9)<sup>9+</sup>代替。 6978 6979**系统能力**:SystemCapability.Sensors.Sensor 6980 6981**参数**: 6982 6983| 参数名 | 类型 | 必填 | 说明 | 6984| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6985| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 是 | 要订阅的有效运动传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。 | 6986| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 注册有效运动传感器的回调函数,上报的数据类型为SignificantMotionResponse。 | 6987| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 6988 6989**示例**: 6990 6991```ts 6992import { sensor } from '@kit.SensorServiceKit'; 6993 6994sensor.on(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 6995 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 6996}, 6997 { interval: 100000000 } 6998); 6999``` 7000 7001### PEDOMETER_DETECTION<sup>(deprecated)</sup> 7002 7003on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, options?: Options): void 7004 7005监听计步检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7006 7007> **说明**: 7008> 7009> 从API version 9 开始不再维护,建议使用[sensor.on.PEDOMETER_DETECTION](#pedometer_detection9)<sup>9+</sup>代替。 7010 7011**需要权限**:ohos.permission.ACTIVITY_MOTION 7012 7013**系统能力**:SystemCapability.Sensors.Sensor 7014 7015**参数**: 7016 7017| 参数名 | 类型 | 必填 | 说明 | 7018| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7019| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER_DETECTION | 是 | 要订阅的计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。 | 7020| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 注册计步检测传感器的回调函数,上报的数据类型为PedometerDetectionResponse。 | 7021| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7022 7023**示例**: 7024 7025```ts 7026import { sensor } from '@kit.SensorServiceKit'; 7027 7028sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 7029 console.info('Succeeded in invoking on. Scalar data: ' + data.scalar); 7030}, 7031 { interval: 100000000 } 7032); 7033``` 7034 7035### PEDOMETER<sup>(deprecated)</sup> 7036 7037on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void 7038 7039监听计步传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7040 7041> **说明**: 7042> 7043> 从API version 9 开始不再维护,建议使用[sensor.on.PEDOMETER](#pedometer9)<sup>9+</sup>代替。 7044 7045**需要权限**:ohos.permission.ACTIVITY_MOTION 7046 7047**系统能力**:SystemCapability.Sensors.Sensor 7048 7049**参数**: 7050 7051| 参数名 | 类型 | 必填 | 说明 | 7052| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7053| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER | 是 | 要订阅的计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。 | 7054| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 注册计步传感器的回调函数,上报的数据类型为PedometerResponse。 | 7055| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7056 7057**示例**: 7058 7059```ts 7060import { sensor } from '@kit.SensorServiceKit'; 7061 7062sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, (data: sensor.PedometerResponse) => { 7063 console.info('Succeeded in invoking on. Steps: ' + data.steps); 7064}, 7065 { interval: 100000000 } 7066); 7067``` 7068 7069### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 7070 7071on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback:Callback<AmbientTemperatureResponse>, options?: Options): void 7072 7073监听环境温度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7074 7075> **说明**: 7076> 7077> 从API version 9 开始不再维护,建议使用[sensor.on.AMBIENT_TEMPERATURE](#ambient_temperature9)<sup>9+</sup>代替。 7078 7079**系统能力**:SystemCapability.Sensors.Sensor 7080 7081**参数**: 7082 7083| 参数名 | 类型 | 必填 | 说明 | 7084| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7085| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 是 | 要订阅的环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。 | 7086| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 注册环境温度传感器的回调函数,上报的数据类型为AmbientTemperatureResponse。 | 7087| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7088 7089**示例**: 7090 7091```ts 7092import { sensor } from '@kit.SensorServiceKit'; 7093 7094sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 7095 console.info('Succeeded in invoking on. Temperature: ' + data.temperature); 7096}, 7097 { interval: 100000000 } 7098); 7099``` 7100 7101### MAGNETIC_FIELD<sup>(deprecated)</sup> 7102 7103on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>,options?: Options): void 7104 7105监听磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7106 7107> **说明**: 7108> 7109> 从API version 9 开始不再维护,建议使用[sensor.on.MAGNETIC_FIELD](#magnetic_field9)<sup>9+</sup>代替。 7110 7111**系统能力**:SystemCapability.Sensors.Sensor 7112 7113**参数**: 7114 7115| 参数名 | 类型 | 必填 | 说明 | 7116| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7117| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD | 是 | 要订阅的磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。 | 7118| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 注册磁场传感器的回调函数,上报的数据类型为MagneticFieldResponse。 | 7119| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7120 7121**示例**: 7122 7123```ts 7124import { sensor } from '@kit.SensorServiceKit'; 7125 7126sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 7127 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 7128 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 7129 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 7130}, 7131 { interval: 100000000 } 7132); 7133``` 7134 7135### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 7136 7137on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void 7138 7139监听未校准磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7140 7141> **说明**: 7142> 7143> 从API version 9 开始不再维护,建议使用[sensor.on.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9)<sup>9+</sup>代替。 7144 7145**系统能力**:SystemCapability.Sensors.Sensor 7146 7147**参数**: 7148 7149| 参数名 | 类型 | 必填 | 说明 | 7150| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7151| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 是 | 要订阅的未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。 | 7152| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 注册未校准磁场传感器的回调函数,上报的数据类型为MagneticFieldUncalibratedResponse。 | 7153| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7154 7155**示例**: 7156 7157```ts 7158import { sensor } from '@kit.SensorServiceKit'; 7159 7160sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 7161 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 7162 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 7163 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 7164 console.info('Succeeded in invoking on. X-coordinate bias: ' + data.biasX); 7165 console.info('Succeeded in invoking on. Y-coordinate bias: ' + data.biasY); 7166 console.info('Succeeded in invoking on. Z-coordinate bias: ' + data.biasZ); 7167}, 7168 { interval: 100000000 } 7169); 7170``` 7171 7172### PROXIMITY<sup>(deprecated)</sup> 7173 7174on(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: Callback<ProximityResponse>,options?: Options): void 7175 7176监听接近光传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7177 7178> **说明**: 7179> 7180> 从API version 9 开始不再维护,建议使用[sensor.on.PROXIMITY](#proximity9)<sup>9+</sup>代替。 7181 7182**系统能力**:SystemCapability.Sensors.Sensor 7183 7184**参数**: 7185 7186| 参数名 | 类型 | 必填 | 说明 | 7187| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7188| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PROXIMITY | 是 | 要订阅的接近光传感器类型为SENSOR_TYPE_ID_PROXIMITY。 | 7189| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 注册接近光传感器的回调函数,上报的数据类型为ProximityResponse。 | 7190| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当接近光事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 7191 7192**示例**: 7193 7194```ts 7195import { sensor } from '@kit.SensorServiceKit'; 7196 7197sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, (data: sensor.ProximityResponse) => { 7198 console.info('Succeeded in invoking on. Distance: ' + data.distance); 7199}, 7200 { interval: 100000000 } 7201); 7202``` 7203 7204### HUMIDITY<sup>(deprecated)</sup> 7205 7206on(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: Callback<HumidityResponse>,options?: Options): void 7207 7208监听湿度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7209 7210> **说明**: 7211> 7212> 从API version 9 开始不再维护,建议使用[sensor.on.HUMIDITY](#humidity9)<sup>9+</sup>代替。 7213 7214**系统能力**:SystemCapability.Sensors.Sensor 7215 7216**参数**: 7217 7218| 参数名 | 类型 | 必填 | 说明 | 7219| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7220| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HUMIDITY | 是 | 要订阅的湿度传感器类型为SENSOR_TYPE_ID_HUMIDITY。 | 7221| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 注册湿度传感器的回调函数,上报的数据类型为HumidityResponse。 | 7222| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7223 7224**示例**: 7225 7226```ts 7227import { sensor } from '@kit.SensorServiceKit'; 7228 7229sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, (data: sensor.HumidityResponse) => { 7230 console.info('Succeeded in invoking on. Humidity: ' + data.humidity); 7231}, 7232 { interval: 100000000 } 7233); 7234``` 7235 7236### BAROMETER<sup>(deprecated)</sup> 7237 7238on(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: Callback<BarometerResponse>,options?: Options): void 7239 7240监听气压计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7241 7242> **说明**: 7243> 7244> 从API version 9 开始不再维护,建议使用[sensor.on.BAROMETER](#barometer9)<sup>9+</sup>代替。 7245 7246**系统能力**:SystemCapability.Sensors.Sensor 7247 7248**参数**: 7249 7250| 参数名 | 类型 | 必填 | 说明 | 7251| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7252| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_BAROMETER | 是 | 要订阅的气压计传感器类型为SENSOR_TYPE_ID_BAROMETER。 | 7253| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 注册气压计传感器的回调函数,上报的数据类型为BarometerResponse。 | 7254| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7255 7256**示例**: 7257 7258```ts 7259import { sensor } from '@kit.SensorServiceKit'; 7260 7261sensor.on(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, (data: sensor.BarometerResponse) => { 7262 console.info('Succeeded in invoking on. Atmospheric pressure: ' + data.pressure); 7263}, 7264 { interval: 100000000 } 7265); 7266``` 7267 7268### HALL<sup>(deprecated)</sup> 7269 7270on(type: SensorType.SENSOR_TYPE_ID_HALL, callback: Callback<HallResponse>, options?: Options): void 7271 7272监听霍尔传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7273 7274> **说明**: 7275> 7276> 从API version 9 开始不再维护,建议使用[sensor.on.HALL](#hall9)<sup>9+</sup>代替。 7277 7278**系统能力**:SystemCapability.Sensors.Sensor 7279 7280**参数**: 7281 7282| 参数名 | 类型 | 必填 | 说明 | 7283| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7284| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HALL | 是 | 要订阅的霍尔传感器类型为SENSOR_TYPE_ID_HALL。 | 7285| callback | Callback<[HallResponse](#hallresponse)> | 是 | 注册霍尔传感器的回调函数,上报的数据类型为 HallResponse。 | 7286| options | [Options](#options) | 否 | 可选参数列表,默认值为200000000ns。当霍尔事件被触发的很频繁时,该参数用于限定事件上报的频率。 | 7287 7288**示例**: 7289 7290```ts 7291import { sensor } from '@kit.SensorServiceKit'; 7292 7293sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HALL, (data: sensor.HallResponse) => { 7294 console.info('Succeeded in invoking on. Status: ' + data.status); 7295}, 7296 { interval: 100000000 } 7297); 7298``` 7299 7300### AMBIENT_LIGHT<sup>(deprecated)</sup> 7301 7302on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: Callback<LightResponse>, options?: Options): void 7303 7304监听环境光传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7305 7306> **说明**: 7307> 7308> 从API version 9 开始不再维护,建议使用[sensor.on.AMBIENT_LIGHT](#ambient_light9)<sup>9+</sup>代替。 7309 7310**系统能力**:SystemCapability.Sensors.Sensor 7311 7312**参数**: 7313 7314| 参数名 | 类型 | 必填 | 说明 | 7315| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 7316| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_LIGHT | 是 | 要订阅的环境光传感器类型为SENSOR_TYPE_ID_AMBIENT_LIGHT。 | 7317| callback | Callback<[LightResponse](#lightresponse)> | 是 | 注册环境光传感器的回调函数,上报的数据类型为LightResponse。 | 7318| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7319 7320**示例**: 7321 7322```ts 7323import { sensor } from '@kit.SensorServiceKit'; 7324 7325sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, (data: sensor.LightResponse) => { 7326 console.info('Succeeded in invoking on. Illumination: ' + data.intensity); 7327}, 7328 { interval: 100000000 } 7329); 7330``` 7331 7332### ORIENTATION<sup>(deprecated)</sup> 7333 7334on(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: Callback<OrientationResponse>, options?: Options): void 7335 7336监听方向传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7337 7338> **说明**: 7339> 7340> 从API version 9 开始不再维护,建议使用[sensor.on.ORIENTATION](#orientation9)<sup>9+</sup>代替。 7341 7342**系统能力**:SystemCapability.Sensors.Sensor 7343 7344**参数**: 7345 7346| 参数名 | 类型 | 必填 | 说明 | 7347| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7348| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ORIENTATION | 是 | 要订阅的方向传感器类型为SENSOR_TYPE_ID_ORIENTATION。 | 7349| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 注册方向传感器的回调函数,上报的数据类型为OrientationResponse。 | 7350| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7351 7352**示例**: 7353 7354```ts 7355import { sensor } from '@kit.SensorServiceKit'; 7356 7357sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, (data: sensor.OrientationResponse) => { 7358 console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta); 7359 console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma); 7360 console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha); 7361}, 7362 { interval: 100000000 } 7363); 7364``` 7365 7366### HEART_RATE<sup>(deprecated)</sup> 7367 7368on(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback<HeartRateResponse>, options?: Options): void 7369 7370监听心率传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7371 7372> **说明**: 7373> 7374> 从API version 9 开始不再维护,建议使用[sensor.on.HEART_RATE](#heart_rate9)<sup>9+</sup>代替。 7375 7376**需要权限**:ohos.permission.HEALTH_DATA 7377 7378**系统能力**:SystemCapability.Sensors.Sensor 7379 7380**参数**: 7381 7382| 参数名 | 类型 | 必填 | 说明 | 7383| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7384| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HEART_RATE | 是 | 要订阅的心率传感器类型为SENSOR_TYPE_ID_HEART_RATE。 | 7385| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 注册心率传感器的回调函数,上报的数据类型为HeartRateResponse。 | 7386| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7387 7388### ROTATION_VECTOR<sup>(deprecated)</sup> 7389 7390on(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR,callback: Callback<RotationVectorResponse>,options?: Options): void 7391 7392监听旋转矢量传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7393 7394> **说明**: 7395> 7396> 从API version 9 开始不再维护,建议使用[sensor.on.ROTATION_VECTOR](#rotation_vector9)<sup>9+</sup>代替。 7397 7398**系统能力**:SystemCapability.Sensors.Sensor 7399 7400**参数**: 7401 7402| 参数名 | 类型 | 必填 | 说明 | 7403| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7404| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ROTATION_VECTOR | 是 | 要订阅的旋转矢量传感器类型为SENSOR_TYPE_ID_ROTATION_VECTOR。 | 7405| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 注册旋转矢量传感器的回调函数,上报的数据类型为RotationVectorResponse。 | 7406| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7407 7408**示例**: 7409 7410```ts 7411import { sensor } from '@kit.SensorServiceKit'; 7412 7413sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 7414 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x); 7415 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y); 7416 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z); 7417 console.info('Succeeded in invoking on. Scalar quantity: ' + data.w); 7418}, 7419 { interval: 100000000 } 7420); 7421``` 7422 7423### WEAR_DETECTION<sup>(deprecated)</sup> 7424 7425on(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback<WearDetectionResponse>,options?: Options): void 7426 7427监听所佩戴的检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。 7428 7429> **说明**: 7430> 7431> 从API version 9 开始不再维护,建议使用[sensor.on.WEAR_DETECTION](#wear_detection9)<sup>9+</sup>代替。 7432 7433**系统能力**:SystemCapability.Sensors.Sensor 7434 7435**参数**: 7436 7437| 参数名 | 类型 | 必填 | 说明 | 7438| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7439| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_WEAR_DETECTION | 是 | 要订阅的佩戴检测传感器类型为SENSOR_TYPE_ID_WEAR_DETECTION。 | 7440| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 注册佩戴检测传感器的回调函数,上报的数据类型为WearDetectionResponse。 | 7441| options | [Options](#options) | 否 | 可选参数列表,用于设置传感器上报频率,默认值为200000000ns。 | 7442 7443**示例**: 7444 7445```ts 7446import { sensor } from '@kit.SensorServiceKit'; 7447 7448sensor.on(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 7449 console.info('Succeeded in invoking on. Wear status: ' + data.value); 7450}, 7451 { interval: 100000000 } 7452); 7453``` 7454 7455## sensor.once<sup>(deprecated)</sup> 7456 7457### ACCELEROMETER<sup>(deprecated)</sup> 7458 7459once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>): void 7460 7461监听加速度传感器的数据变化一次。 7462 7463> **说明**: 7464> 7465> 从API version 9 开始不再维护,建议使用[sensor.once.ACCELEROMETER](#accelerometer9-1)<sup>9+</sup>代替。 7466 7467**需要权限**:ohos.permission.ACCELEROMETER 7468 7469**系统能力**:SystemCapability.Sensors.Sensor 7470 7471**参数**: 7472 7473| 参数名 | 类型 | 必填 | 说明 | 7474| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7475| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER | 是 | 加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。 | 7476| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 是 | 注册一次加速度传感器的回调函数,上报的数据类型为AccelerometerResponse。 | 7477 7478**示例**: 7479 7480```ts 7481import { sensor } from '@kit.SensorServiceKit'; 7482 7483sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 7484 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7485 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7486 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7487}); 7488``` 7489 7490### LINEAR_ACCELERATION<sup>(deprecated)</sup> 7491 7492once(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>): void 7493 7494监听线性加速度传感器数据变化一次。 7495 7496> **说明**: 7497> 7498> 从API version 9 开始不再维护,建议使用[sensor.once.LINEAR_ACCELEROMETER](#linear_accelerometer9-1)<sup>9+</sup>代替。 7499 7500**需要权限**:ohos.permission.ACCELERATION 7501 7502**系统能力**:SystemCapability.Sensors.Sensor 7503 7504**参数**: 7505 7506| 参数名 | 类型 | 必填 | 说明 | 7507| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7508| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_LINEAR_ACCELERATION | 是 | 线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。 | 7509| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 是 | 注册一次线性加速度传感器的回调函数,上报的数据类型为LinearAccelerometerResponse。 | 7510 7511### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 7512 7513once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>): void 7514 7515监听未校准加速度传感器的数据变化一次。 7516 7517> **说明**: 7518> 7519> 从API version 9 开始不再维护,建议使用[sensor.once.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9-1)<sup>9+</sup>代替。 7520 7521**需要权限**:ohos.permission.ACCELEROMETER 7522 7523**系统能力**:SystemCapability.Sensors.Sensor 7524 7525**参数**: 7526 7527| 参数名 | 类型 | 必填 | 说明 | 7528| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7529| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 是 | 未校准加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。 | 7530| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 是 | 注册一次未校准加速度传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。 | 7531 7532**示例**: 7533 7534```ts 7535import { sensor } from '@kit.SensorServiceKit'; 7536 7537sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, (data: sensor.AccelerometerUncalibratedResponse) => { 7538 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7539 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7540 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7541 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 7542 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 7543 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 7544}); 7545``` 7546 7547### GRAVITY<sup>(deprecated)</sup> 7548 7549once(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>): void 7550 7551监听重力传感器的数据变化一次。 7552 7553> **说明**: 7554> 7555> 从API version 9 开始不再维护,建议使用[sensor.once.GRAVITY](#gravity9-1)<sup>9+</sup>代替。 7556 7557**系统能力**:SystemCapability.Sensors.Sensor 7558 7559**参数**: 7560 7561| 参数名 | 类型 | 必填 | 说明 | 7562| -------- | ---------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7563| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GRAVITY | 是 | 重力传感器类型为SENSOR_TYPE_ID_GRAVITY。 | 7564| callback | Callback<[GravityResponse](#gravityresponse)> | 是 | 注册一次重力传感器的回调函数,上报的数据类型为GravityResponse。 | 7565 7566**示例**: 7567 7568```ts 7569import { sensor } from '@kit.SensorServiceKit'; 7570 7571sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, (data: sensor.GravityResponse) => { 7572 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7573 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7574 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7575 }); 7576``` 7577 7578### GYROSCOPE<sup>(deprecated)</sup> 7579 7580once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>): void 7581 7582监听陀螺仪传感器的数据变化一次。 7583 7584> **说明**: 7585> 7586> 从API version 9 开始不再维护,建议使用[sensor.once.GYROSCOPE](#gyroscope9-1)<sup>9+</sup>代替。 7587 7588**需要权限**:ohos.permission.GYROSCOPE 7589 7590**系统能力**:SystemCapability.Sensors.Sensor 7591 7592**参数**: 7593 7594| 参数名 | 类型 | 必填 | 说明 | 7595| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7596| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE | 是 | 陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。 | 7597| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 是 | 注册一次陀螺仪传感器的回调函数,上报的数据类型为GyroscopeResponse。 | 7598 7599**示例**: 7600 7601```ts 7602import { sensor } from '@kit.SensorServiceKit'; 7603 7604sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, (data: sensor.GyroscopeResponse) => { 7605 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7606 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7607 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7608}); 7609``` 7610 7611### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 7612 7613once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback: Callback<GyroscopeUncalibratedResponse>): void 7614 7615监听未校准陀螺仪传感器的数据变化一次。 7616 7617> **说明**: 7618> 7619> 从API version 9 开始不再维护,建议使用[sensor.once.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9-1)<sup>9+</sup>代替。 7620 7621**需要权限**:ohos.permission.GYROSCOPE 7622 7623**系统能力**:SystemCapability.Sensors.Sensor 7624 7625**参数**: 7626 7627| 参数名 | 类型 | 必填 | 说明 | 7628| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7629| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 是 | 未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。 | 7630| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 是 | 注册一次未校准陀螺仪传感器的回调函数,上报的数据类型为GyroscopeUncalibratedResponse。 | 7631 7632**示例**: 7633 7634 7635```ts 7636import { sensor } from '@kit.SensorServiceKit'; 7637 7638sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (data: sensor.GyroscopeUncalibratedResponse) => { 7639 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7640 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7641 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7642 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 7643 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 7644 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 7645}); 7646``` 7647 7648### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 7649 7650once(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION,callback: Callback<SignificantMotionResponse>): void 7651 7652监听有效运动传感器的数据变化一次。 7653 7654> **说明**: 7655> 7656> 从API version 9 开始不再维护,建议使用[sensor.once.SIGNIFICANT_MOTION](#significant_motion9-1)<sup>9+</sup>代替。 7657 7658**系统能力**:SystemCapability.Sensors.Sensor 7659 7660**参数**: 7661 7662| 参数名 | 类型 | 必填 | 说明 | 7663| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7664| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 是 | 有效运动传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。 | 7665| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 是 | 注册一次有效运动传感器的回调函数,上报的数据类型为SignificantMotionResponse。 | 7666 7667**示例**: 7668 7669```ts 7670import { sensor } from '@kit.SensorServiceKit'; 7671 7672sensor.once(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, (data: sensor.SignificantMotionResponse) => { 7673 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 7674}); 7675``` 7676 7677### PEDOMETER_DETECTION<sup>(deprecated)</sup> 7678 7679once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION,callback: Callback<PedometerDetectionResponse>): void 7680 7681监听计步检测传感器数据变化一次。 7682 7683> **说明**: 7684> 7685> 从API version 9 开始不再维护,建议使用[sensor.once.PEDOMETER_DETECTION](#pedometer_detection9-1)<sup>9+</sup>代替。 7686 7687**需要权限**:ohos.permission.ACTIVITY_MOTION 7688 7689**系统能力**:SystemCapability.Sensors.Sensor 7690 7691**参数**: 7692 7693| 参数名 | 类型 | 必填 | 说明 | 7694| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7695| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER_DETECTION | 是 | 计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。 | 7696| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 是 | 注册一次计步检测传感器的回调函数,上报的数据类型为PedometerDetectionResponse。 | 7697 7698**示例**: 7699 7700```ts 7701import { sensor } from '@kit.SensorServiceKit'; 7702 7703sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, (data: sensor.PedometerDetectionResponse) => { 7704 console.info('Succeeded in invoking once. Scalar data: ' + data.scalar); 7705}); 7706``` 7707 7708### PEDOMETER<sup>(deprecated)</sup> 7709 7710once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>): void 7711 7712监听计步器传感器数据变化一次。 7713 7714> **说明**: 7715> 7716> 从API version 9 开始不再维护,建议使用[sensor.once.PEDOMETER](#pedometer9-1)<sup>9+</sup>代替。 7717 7718**需要权限**:ohos.permission.ACTIVITY_MOTION 7719 7720**系统能力**:SystemCapability.Sensors.Sensor 7721 7722**参数**: 7723 7724| 参数名 | 类型 | 必填 | 说明 | 7725| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7726| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER | 是 | 计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。 | 7727| callback | Callback<[PedometerResponse](#pedometerresponse)> | 是 | 注册一次计步传感器的回调函数,上报的数据类型为PedometerResponse。 | 7728 7729**示例**: 7730 7731```ts 7732import { sensor } from '@kit.SensorServiceKit'; 7733 7734sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, (data: sensor.PedometerResponse) => { 7735 console.info('Succeeded in invoking once. Steps: ' + data.steps); 7736}); 7737``` 7738 7739### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 7740 7741once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback: Callback<AmbientTemperatureResponse>): void 7742 7743监听环境温度传感器数据变化一次。 7744 7745> **说明**: 7746> 7747> 从API version 9 开始不再维护,建议使用[sensor.once.AMBIENT_TEMPERATURE](#ambient_temperature9-1)<sup>9+</sup>代替。 7748 7749**系统能力**:SystemCapability.Sensors.Sensor 7750 7751**参数**: 7752 7753| 参数名 | 类型 | 必填 | 说明 | 7754| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7755| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 是 | 环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。 | 7756| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 是 | 注册一次环境温度传感器的回调函数,上报的数据类型为AmbientTemperatureResponse。 | 7757 7758**示例**: 7759 7760```ts 7761import { sensor } from '@kit.SensorServiceKit'; 7762 7763sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, (data: sensor.AmbientTemperatureResponse) => { 7764 console.info('Succeeded in invoking once. Temperature: ' + data.temperature); 7765}); 7766``` 7767 7768### MAGNETIC_FIELD<sup>(deprecated)</sup> 7769 7770once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>): void 7771 7772监听磁场传感器数据变化一次。 7773 7774> **说明**: 7775> 7776> 从API version 9 开始不再维护,建议使用[sensor.once.MAGNETIC_FIELD](#magnetic_field9-1)<sup>9+</sup>代替。 7777 7778**系统能力**:SystemCapability.Sensors.Sensor 7779 7780**参数**: 7781 7782| 参数名 | 类型 | 必填 | 说明 | 7783| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7784| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD | 是 | 磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。 | 7785| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 是 | 注册一次磁场传感器的回调函数,上报的数据类型为MagneticFieldResponse。 | 7786 7787**示例**: 7788 7789```ts 7790import { sensor } from '@kit.SensorServiceKit'; 7791 7792sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => { 7793 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7794 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7795 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7796}); 7797``` 7798 7799### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 7800 7801once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>): void 7802 7803监听未校准磁场传感器数据变化一次。 7804 7805> **说明**: 7806> 7807> 从API version 9 开始不再维护,建议使用[sensor.once.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9-1)<sup>9+</sup>代替。 7808 7809**系统能力**:SystemCapability.Sensors.Sensor 7810 7811**参数**: 7812 7813| 参数名 | 类型 | 必填 | 说明 | 7814| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7815| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 是 | 未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。 | 7816| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 是 | 注册一次未校准磁场传感器的回调函数,上报的数据类型为MagneticFieldUncalibratedResponse。 | 7817 7818**示例**: 7819 7820```ts 7821import { sensor } from '@kit.SensorServiceKit'; 7822 7823sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, (data: sensor.MagneticFieldUncalibratedResponse) => { 7824 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 7825 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 7826 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 7827 console.info('Succeeded in invoking once. X-coordinate bias: ' + data.biasX); 7828 console.info('Succeeded in invoking once. Y-coordinate bias: ' + data.biasY); 7829 console.info('Succeeded in invoking once. Z-coordinate bias: ' + data.biasZ); 7830}); 7831``` 7832 7833### PROXIMITY<sup>(deprecated)</sup> 7834 7835once(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: Callback<ProximityResponse>): void 7836 7837监听接近光传感器数据变化一次。 7838 7839> **说明**: 7840> 7841> 从API version 9 开始不再维护,建议使用[sensor.once.PROXIMITY](#proximity9-1)<sup>9+</sup>代替。 7842 7843**系统能力**:SystemCapability.Sensors.Sensor 7844 7845**参数**: 7846 7847| 参数名 | 类型 | 必填 | 说明 | 7848| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7849| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PROXIMITY | 是 | 接近光传感器类型为SENSOR_TYPE_ID_PROXIMITY。 | 7850| callback | Callback<[ProximityResponse](#proximityresponse)> | 是 | 注册一次接近光传感器的回调函数,上报的数据类型为ProximityResponse。 | 7851 7852**示例**: 7853 7854```ts 7855import { sensor } from '@kit.SensorServiceKit'; 7856 7857sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, (data: sensor.ProximityResponse) => { 7858 console.info('Succeeded in invoking once. Distance: ' + data.distance); 7859} 7860); 7861``` 7862 7863### HUMIDITY<sup>(deprecated)</sup> 7864 7865once(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: Callback<HumidityResponse>): void 7866 7867监听湿度传感器数据变化一次。 7868 7869> **说明**: 7870> 7871> 从API version 9 开始不再维护,建议使用[sensor.once.HUMIDITY](#humidity9-1)<sup>9+</sup>代替。 7872 7873**系统能力**:SystemCapability.Sensors.Sensor 7874 7875**参数**: 7876 7877| 参数名 | 类型 | 必填 | 说明 | 7878| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7879| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HUMIDITY | 是 | 湿度传感器类型为SENSOR_TYPE_ID_HUMIDITY。 | 7880| callback | Callback<[HumidityResponse](#humidityresponse)> | 是 | 注册一次湿度传感器的回调函数,上报的数据类型为HumidityResponse。 | 7881 7882**示例**: 7883 7884```ts 7885import { sensor } from '@kit.SensorServiceKit'; 7886 7887sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, (data: sensor.HumidityResponse) => { 7888 console.info('Succeeded in invoking once. Humidity: ' + data.humidity); 7889}); 7890``` 7891 7892### BAROMETER<sup>(deprecated)</sup> 7893 7894once(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: Callback<BarometerResponse>): void 7895 7896监听气压计传感器数据变化一次。 7897 7898> **说明**: 7899> 7900> 从API version 9 开始不再维护,建议使用[sensor.once.BAROMETER](#barometer9-1)<sup>9+</sup>代替。 7901 7902**系统能力**:SystemCapability.Sensors.Sensor 7903 7904**参数**: 7905 7906| 参数名 | 类型 | 必填 | 说明 | 7907| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7908| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_BAROMETER | 是 | 气压计传感器类型为SENSOR_TYPE_ID_BAROMETER。 | 7909| callback | Callback<[BarometerResponse](#barometerresponse)> | 是 | 注册一次气压计传感器的回调函数,上报的数据类型为BarometerResponse。 | 7910 7911**示例**: 7912 7913```ts 7914import { sensor } from '@kit.SensorServiceKit'; 7915 7916sensor.once(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, (data: sensor.BarometerResponse) => { 7917 console.info('Succeeded in invoking once. Atmospheric pressure: ' + data.pressure); 7918}); 7919``` 7920 7921### HALL<sup>(deprecated)</sup> 7922 7923once(type: SensorType.SENSOR_TYPE_ID_HALL, callback: Callback<HallResponse>): void 7924 7925监听霍尔传感器数据变化一次。 7926 7927> **说明**: 7928> 7929> 从API version 9 开始不再维护,建议使用[sensor.once.HALL](#hall9-1)<sup>9+</sup>代替。 7930 7931**系统能力**:SystemCapability.Sensors.Sensor 7932 7933**参数**: 7934 7935| 参数名 | 类型 | 必填 | 说明 | 7936| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 7937| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HALL | 是 | 霍尔传感器类型为SENSOR_TYPE_ID_HALL。 | 7938| callback | Callback<[HallResponse](#hallresponse)> | 是 | 注册一次霍尔传感器的回调函数,上报的数据类型为HallResponse。 | 7939 7940**示例**: 7941 7942```ts 7943import { sensor } from '@kit.SensorServiceKit'; 7944 7945sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HALL, (data: sensor.HallResponse) => { 7946 console.info('Succeeded in invoking once. Status: ' + data.status); 7947}); 7948``` 7949 7950### AMBIENT_LIGHT<sup>(deprecated)</sup> 7951 7952once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: Callback<LightResponse>): void 7953 7954监听环境光传感器数据变化一次。 7955 7956> **说明**: 7957> 7958> 从API version 9 开始不再维护,建议使用[sensor.once.AMBIENT_LIGHT](#ambient_light9-1)<sup>9+</sup>代替。 7959 7960**系统能力**:SystemCapability.Sensors.Sensor 7961 7962**参数**: 7963 7964| 参数名 | 类型 | 必填 | 说明 | 7965| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7966| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_LIGHT | 是 | 环境光传感器类型为SENSOR_TYPE_ID_AMBIENT_LIGHT。 | 7967| callback | Callback<[LightResponse](#lightresponse)> | 是 | 注册一次环境光传感器的回调函数,上报的数据类型为LightResponse。 | 7968 7969**示例**: 7970 7971```ts 7972import { sensor } from '@kit.SensorServiceKit'; 7973 7974sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, (data: sensor.LightResponse) => { 7975 console.info('Succeeded in invoking once. invoking once. Illumination: ' + data.intensity); 7976}); 7977``` 7978 7979### ORIENTATION<sup>(deprecated)</sup> 7980 7981once(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: Callback<OrientationResponse>): void 7982 7983监听方向传感器数据变化一次。 7984 7985> **说明**: 7986> 7987> 从API version 9 开始不再维护,建议使用[sensor.once.ORIENTATION](#orientation9-1)<sup>9+</sup>代替。 7988 7989**系统能力**:SystemCapability.Sensors.Sensor 7990 7991**参数**: 7992 7993| 参数名 | 类型 | 必填 | 说明 | 7994| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7995| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ORIENTATION | 是 | 方向传感器类型为SENSOR_TYPE_ID_ORIENTATION。 | 7996| callback | Callback<[OrientationResponse](#orientationresponse)> | 是 | 注册一次方向传感器的回调函数,上报的数据类型为OrientationResponse。 | 7997 7998**示例**: 7999 8000```ts 8001import { sensor } from '@kit.SensorServiceKit'; 8002 8003sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, (data: sensor.OrientationResponse) => { 8004 console.info('Succeeded in invoking the device rotating at an angle around the X axis: ' + data.beta); 8005 console.info('Succeeded in invoking the device rotating at an angle around the Y axis: ' + data.gamma); 8006 console.info('Succeeded in invoking the device rotating at an angle around the Z axis: ' + data.alpha); 8007}); 8008``` 8009 8010### ROTATION_VECTOR<sup>(deprecated)</sup> 8011 8012once(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback: Callback<RotationVectorResponse>): void 8013 8014监听旋转矢量传感器数据变化一次。 8015 8016> **说明**: 8017> 8018> 从API version 9 开始不再维护,建议使用[sensor.once.ROTATION_VECTOR](#rotation_vector9-1)<sup>9+</sup>代替。 8019 8020**系统能力**:SystemCapability.Sensors.Sensor 8021 8022**参数**: 8023 8024| 参数名 | 类型 | 必填 | 说明 | 8025| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8026| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ROTATION_VECTOR | 是 | 旋转矢量传感器类型为SENSOR_TYPE_ID_ROTATION_VECTOR。 | 8027| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 是 | 注册一次旋转矢量传感器的回调函数,上报的数据类型为RotationVectorResponse。 | 8028 8029**示例**: 8030 8031```ts 8032import { sensor } from '@kit.SensorServiceKit'; 8033 8034sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, (data: sensor.RotationVectorResponse) => { 8035 console.info('Succeeded in invoking once. X-coordinate component: ' + data.x); 8036 console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y); 8037 console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z); 8038 console.info('Succeeded in invoking once. Scalar quantity: ' + data.w); 8039}); 8040``` 8041 8042### HEART_RATE<sup>(deprecated)</sup> 8043 8044once(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback<HeartRateResponse>): void 8045 8046监听心率传感器数据变化一次。 8047 8048> **说明**: 8049> 8050> 从API version 9 开始不再维护,建议使用[sensor.once.HEART_RATE](#heart_rate9-1)<sup>9+</sup>代替。 8051 8052**需要权限**:ohos.permission.HEART_RATE 8053 8054**系统能力**:SystemCapability.Sensors.Sensor 8055 8056**参数**: 8057 8058| 参数名 | 类型 | 必填 | 说明 | 8059| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8060| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HEART_RATE | 是 | 心率传感器类型为SENSOR_TYPE_ID_HEART_RATE。 | 8061| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 是 | 注册一次心率传感器的回调函数,上报的数据类型为HeartRateResponse。 | 8062 8063**示例**: 8064 8065 8066```ts 8067import { sensor } from '@kit.SensorServiceKit'; 8068 8069sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, (data: sensor.HeartRateResponse) => { 8070 console.info("Succeeded in invoking once. Heart rate: " + data.heartRate); 8071}); 8072``` 8073 8074### WEAR_DETECTION<sup>(deprecated)</sup> 8075 8076once(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback<WearDetectionResponse>): void 8077 8078监听所佩戴的检测传感器的数据变化一次。 8079 8080> **说明**: 8081> 8082> 从API version 9 开始不再维护,建议使用[sensor.once.WEAR_DETECTION](#wear_detection9-1)<sup>9+</sup>代替。 8083 8084**系统能力**:SystemCapability.Sensors.Sensor 8085 8086**参数**: 8087 8088| 参数名 | 类型 | 必填 | 说明 | 8089| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8090| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_WEAR_DETECTION | 是 | 佩戴检测传感器类型为SENSOR_TYPE_ID_WEAR_DETECTION。 | 8091| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 是 | 注册一次穿戴检测传感器的回调函数,上报的数据类型为WearDetectionResponse。 | 8092 8093**示例**: 8094 8095 8096```ts 8097import { sensor } from '@kit.SensorServiceKit'; 8098 8099sensor.once(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, (data: sensor.WearDetectionResponse) => { 8100 console.info("Succeeded in invoking once. Wear status: " + data.value); 8101}); 8102``` 8103 8104## sensor.off<sup>(deprecated)</sup> 8105 8106### ACCELEROMETER<sup>(deprecated)</sup> 8107 8108off(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback?: Callback<AccelerometerResponse>): void 8109 8110取消订阅传感器数据。 8111 8112> **说明**: 8113> 8114> 从API version 9 开始不再维护,建议使用[sensor.off.ACCELEROMETER<sup>9+</sup>](#accelerometer9-2)代替。 8115 8116**需要权限**:ohos.permission.ACCELEROMETER 8117 8118**系统能力**:SystemCapability.Sensors.Sensor 8119 8120**参数**: 8121 8122| 参数名 | 类型 | 必填 | 说明 | 8123| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8124| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER | 是 | 要取消订阅的加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。 | 8125| callback | Callback<[AccelerometerResponse](#accelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8126 8127**示例**: 8128 8129```ts 8130import { sensor } from '@kit.SensorServiceKit'; 8131 8132function callback(data: sensor.AccelerometerResponse) { 8133 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8134 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8135 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8136} 8137 8138sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 8139``` 8140 8141### ACCELEROMETER_UNCALIBRATED<sup>(deprecated)</sup> 8142 8143off(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, callback?: Callback<AccelerometerUncalibratedResponse>): void 8144 8145取消订阅传感器数据。 8146 8147> **说明**: 8148> 8149> 从API version 9 开始不再维护,建议使用[sensor.off.ACCELEROMETER_UNCALIBRATED](#accelerometer_uncalibrated9-2)<sup>9+</sup>代替。 8150 8151**需要权限**:ohos.permission.ACCELEROMETER 8152 8153**系统能力**:SystemCapability.Sensors.Sensor 8154 8155**参数**: 8156 8157| 参数名 | 类型 | 必填 | 说明 | 8158| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8159| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 是 | 要取消订阅的未校准加速度计传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。 | 8160| callback | Callback<[AccelerometerUncalibratedResponse](#accelerometeruncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8161 8162**示例**: 8163 8164```ts 8165import { sensor } from '@kit.SensorServiceKit'; 8166 8167function callback(data: sensor.AccelerometerUncalibratedResponse) { 8168 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8169 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8170 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8171 console.info('Succeeded in invoking off. X-coordinate bias: ' + data.biasX); 8172 console.info('Succeeded in invoking off. Y-coordinate bias: ' + data.biasY); 8173 console.info('Succeeded in invoking off. Z-coordinate bias: ' + data.biasZ); 8174} 8175 8176sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, callback); 8177``` 8178 8179### AMBIENT_LIGHT<sup>(deprecated)</sup> 8180 8181off(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback?: Callback<LightResponse>): void 8182 8183取消订阅传感器数据。 8184 8185> **说明**: 8186> 8187> 从API version 9 开始不再维护,建议使用[sensor.off.AMBIENT_LIGHT](#ambient_light9-2)<sup>9+</sup>代替。 8188 8189**系统能力**:SystemCapability.Sensors.Sensor 8190 8191**参数**: 8192 8193| 参数名 | 类型 | 必填 | 说明 | 8194| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8195| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_LIGHT | 是 | 要取消订阅的环境光传感器类型为SENSOR_TYPE_ID_AMBIENT_LIGHT。 | 8196| callback | Callback<[LightResponse](#lightresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8197 8198**示例**: 8199 8200```ts 8201import { sensor } from '@kit.SensorServiceKit'; 8202 8203function callback(data: sensor.LightResponse) { 8204 console.info('Succeeded in invoking off. Illumination: ' + data.intensity); 8205} 8206 8207sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback); 8208``` 8209 8210### AMBIENT_TEMPERATURE<sup>(deprecated)</sup> 8211 8212off(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, callback?: Callback<AmbientTemperatureResponse>): void 8213 8214取消订阅传感器数据。 8215 8216> **说明**: 8217> 8218> 从API version 9 开始不再维护,建议使用[sensor.off.AMBIENT_TEMPERATURE](#ambient_temperature9-2)<sup>9+</sup>代替。 8219 8220**系统能力**:SystemCapability.Sensors.Sensor 8221 8222**参数**: 8223 8224| 参数名 | 类型 | 必填 | 说明 | 8225| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8226| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 是 | 要取消订阅的环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。 | 8227| callback | Callback<[AmbientTemperatureResponse](#ambienttemperatureresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8228 8229**示例**: 8230 8231```ts 8232import { sensor } from '@kit.SensorServiceKit'; 8233 8234function callback(data: sensor.AmbientTemperatureResponse) { 8235 console.info('Succeeded in invoking off. Temperature: ' + data.temperature); 8236} 8237 8238sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, callback); 8239``` 8240 8241### BAROMETER<sup>(deprecated)</sup> 8242 8243off(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback?: Callback<BarometerResponse>): void 8244 8245取消订阅传感器数据。 8246 8247> **说明**: 8248> 8249> 从API version 9 开始不再维护,建议使用[sensor.off.BAROMETER](#barometer9-2)<sup>9+</sup>代替。 8250 8251**系统能力**:SystemCapability.Sensors.Sensor 8252 8253**参数**: 8254 8255| 参数名 | 类型 | 必填 | 说明 | 8256| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8257| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_BAROMETER | 是 | 要取消订阅的气压计传感器类型为SENSOR_TYPE_ID_BAROMETER。 | 8258| callback | Callback<[BarometerResponse](#barometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8259 8260**示例**: 8261 8262```ts 8263import { sensor } from '@kit.SensorServiceKit'; 8264 8265function callback(data: sensor.BarometerResponse) { 8266 console.info('Succeeded in invoking off. Atmospheric pressure: ' + data.pressure); 8267} 8268 8269sensor.off(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, callback); 8270``` 8271 8272### GRAVITY<sup>(deprecated)</sup> 8273 8274off(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback?: Callback<GravityResponse>): void 8275 8276取消订阅传感器数据。 8277 8278> **说明**: 8279> 8280> 从API version 9 开始不再维护,建议使用[sensor.off.GRAVITY](#gravity9-2)<sup>9+</sup>代替。 8281 8282**系统能力**:SystemCapability.Sensors.Sensor 8283 8284**参数**: 8285 8286| 参数名 | 类型 | 必填 | 说明 | 8287| -------- | ---------------------------------------------------------- | ---- | ------------------------------------------------------------ | 8288| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GRAVITY | 是 | 要取消订阅的重力传感器类型为SENSOR_TYPE_ID_GRAVITY。 | 8289| callback | Callback<[GravityResponse](#gravityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8290 8291**示例**: 8292 8293```ts 8294import { sensor } from '@kit.SensorServiceKit'; 8295 8296function callback(data: sensor.GravityResponse) { 8297 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8298 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8299 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8300} 8301 8302sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, callback); 8303``` 8304 8305### GYROSCOPE<sup>(deprecated)</sup> 8306 8307off(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback?: Callback<GyroscopeResponse>): void 8308 8309取消订阅传感器数据。 8310 8311> **说明**: 8312> 8313> 从API version 9 开始不再维护,建议使用[sensor.off.GYROSCOPE](#gyroscope9-2)<sup>9+</sup>代替。 8314 8315**需要权限**:ohos.permission.GYROSCOPE 8316 8317**系统能力**:SystemCapability.Sensors.Sensor 8318 8319**参数**: 8320 8321| 参数名 | 类型 | 必填 | 说明 | 8322| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8323| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE | 是 | 要取消订阅的陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。 | 8324| callback | Callback<[GyroscopeResponse](#gyroscoperesponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8325 8326**示例**: 8327 8328```ts 8329import { sensor } from '@kit.SensorServiceKit'; 8330 8331function callback(data: sensor.GyroscopeResponse) { 8332 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8333 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8334 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8335} 8336 8337sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback); 8338``` 8339 8340### GYROSCOPE_UNCALIBRATED<sup>(deprecated)</sup> 8341 8342off(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, callback?: Callback<GyroscopeUncalibratedResponse>): void 8343 8344取消订阅传感器数据。 8345 8346> **说明**: 8347> 8348> 从API version 9 开始不再维护,建议使用[sensor.off.GYROSCOPE_UNCALIBRATED](#gyroscope_uncalibrated9-2)<sup>9+</sup>代替。 8349 8350**需要权限**:ohos.permission.GYROSCOPE 8351 8352**系统能力**:SystemCapability.Sensors.Sensor 8353 8354**参数**: 8355 8356| 参数名 | 类型 | 必填 | 说明 | 8357| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8358| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 是 | 要取消订阅的未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。 | 8359| callback | Callback<[GyroscopeUncalibratedResponse](#gyroscopeuncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8360 8361**示例**: 8362 8363```ts 8364import { sensor } from '@kit.SensorServiceKit'; 8365 8366function callback(data: sensor.GyroscopeUncalibratedResponse) { 8367 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8368 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8369 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8370} 8371 8372sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, callback); 8373``` 8374 8375### HALL<sup>(deprecated)</sup> 8376 8377off(type: SensorType.SENSOR_TYPE_ID_HALL, callback?: Callback<HallResponse>): void 8378 8379取消订阅传感器数据。 8380 8381> **说明**: 8382> 8383> 从API version 9 开始不再维护,建议使用[sensor.off.HALL](#hall9-2)<sup>9+</sup>代替。 8384 8385**系统能力**:SystemCapability.Sensors.Sensor 8386 8387**参数**: 8388 8389| 参数名 | 类型 | 必填 | 说明 | 8390| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 8391| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HALL | 是 | 要取消订阅的霍尔传感器类型为SENSOR_TYPE_ID_HALL。 | 8392| callback | Callback<[HallResponse](#hallresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8393 8394**示例**: 8395 8396```ts 8397import { sensor } from '@kit.SensorServiceKit'; 8398 8399function callback(data: sensor.HallResponse) { 8400 console.info('Succeeded in invoking off. Status: ' + data.status); 8401} 8402 8403sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HALL, callback); 8404``` 8405 8406### HEART_RATE<sup>(deprecated)</sup> 8407 8408off(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback?: Callback<HeartRateResponse>): void 8409 8410取消订阅传感器数据。 8411 8412> **说明**: 8413> 8414> 从API version 9 开始不再维护,建议使用[sensor.off.HEART_RATE](#heart_rate9-2)<sup>9+</sup>代替。 8415 8416**需要权限**:ohos.permission.HEALTH_DATA 8417 8418**系统能力**:SystemCapability.Sensors.Sensor 8419 8420**参数**: 8421 8422| 参数名 | 类型 | 必填 | 说明 | 8423| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8424| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HEART_RATE | 是 | 要取消订阅的心率传感器类型为SENSOR_TYPE_ID_HEART_RATE。 | 8425| callback | Callback<[HeartRateResponse](#heartrateresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8426 8427**示例**: 8428 8429```ts 8430import { sensor } from '@kit.SensorServiceKit'; 8431 8432function callback(data: sensor.HeartRateResponse) { 8433 console.info('Succeeded in invoking off. Heart rate: ' + data.heartRate); 8434} 8435 8436sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, callback); 8437``` 8438 8439### HUMIDITY<sup>(deprecated)</sup> 8440 8441off(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback?: Callback<HumidityResponse>): void 8442 8443取消订阅传感器数据。 8444 8445> **说明**: 8446> 8447> 从API version 9 开始不再维护,建议使用[sensor.off.HUMIDITY](#humidity9-2)<sup>9+</sup>代替。 8448 8449**系统能力**:SystemCapability.Sensors.Sensor 8450 8451**参数**: 8452 8453| 参数名 | 类型 | 必填 | 说明 | 8454| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 8455| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_HUMIDITY | 是 | 要取消订阅的湿度传感器类型为SENSOR_TYPE_ID_HUMIDITY。 | 8456| callback | Callback<[HumidityResponse](#humidityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8457 8458**示例**: 8459 8460```ts 8461import { sensor } from '@kit.SensorServiceKit'; 8462 8463function callback(data: sensor.HumidityResponse) { 8464 console.info('Succeeded in invoking off. Humidity: ' + data.humidity); 8465} 8466 8467sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, callback); 8468``` 8469 8470### LINEAR_ACCELERATION<sup>(deprecated)</sup> 8471 8472off(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback?: Callback<LinearAccelerometerResponse>): void 8473 8474取消订阅传感器数据。 8475 8476> **说明**: 8477> 8478> 从API version 9 开始不再维护,建议使用[sensor.off.LINEAR_ACCELEROMETER](#linear_accelerometer9-2)<sup>9+</sup>代替。 8479 8480**需要权限**:ohos.permission.ACCELEROMETER 8481 8482**系统能力**:SystemCapability.Sensors.Sensor 8483 8484**参数**: 8485 8486| 参数名 | 类型 | 必填 | 说明 | 8487| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8488| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_LINEAR_ACCELERATION | 是 | 要取消订阅的线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。 | 8489| callback | Callback<[LinearAccelerometerResponse](#linearaccelerometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8490 8491**示例**: 8492 8493```ts 8494import { sensor } from '@kit.SensorServiceKit'; 8495 8496function callback(data: sensor.LinearAccelerometerResponse) { 8497 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8498 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8499 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8500} 8501 8502sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback); 8503``` 8504 8505### MAGNETIC_FIELD<sup>(deprecated)</sup> 8506 8507 off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback?: Callback<MagneticFieldResponse>): void 8508 8509取消订阅传感器数据。 8510 8511> **说明**: 8512> 8513> 从API version 9 开始不再维护,建议使用[sensor.off.MAGNETIC_FIELD](#magnetic_field9-2)<sup>9+</sup>代替。 8514 8515**系统能力**:SystemCapability.Sensors.Sensor 8516 8517**参数**: 8518 8519| 参数名 | 类型 | 必填 | 说明 | 8520| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8521| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD | 是 | 要取消订阅的磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。 | 8522| callback | Callback<[MagneticFieldResponse](#magneticfieldresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8523 8524**示例**: 8525 8526```ts 8527import { sensor } from '@kit.SensorServiceKit'; 8528 8529function callback(data: sensor.MagneticFieldResponse) { 8530 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8531 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8532 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8533} 8534 8535sensor.off(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback); 8536``` 8537 8538### MAGNETIC_FIELD_UNCALIBRATED<sup>(deprecated)</sup> 8539 8540 off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, callback?: Callback<MagneticFieldUncalibratedResponse>): void 8541 8542取消订阅传感器数据。 8543 8544> **说明**: 8545> 8546> 从API version 9 开始不再维护,建议使用[sensor.off.MAGNETIC_FIELD_UNCALIBRATED](#magnetic_field_uncalibrated9-2)<sup>9+</sup>代替。 8547 8548**系统能力**:SystemCapability.Sensors.Sensor 8549 8550**参数**: 8551 8552| 参数名 | 类型 | 必填 | 说明 | 8553| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8554| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 是 | 要取消订阅的未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。 | 8555| callback | Callback<[MagneticFieldUncalibratedResponse](#magneticfielduncalibratedresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8556 8557**示例**: 8558 8559```ts 8560import { sensor } from '@kit.SensorServiceKit'; 8561 8562function callback(data: sensor.MagneticFieldUncalibratedResponse) { 8563 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8564 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8565 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8566 console.info('Succeeded in invoking off. X-coordinate bias: ' + data.biasX); 8567 console.info('Succeeded in invoking off. Y-coordinate bias: ' + data.biasY); 8568 console.info('Succeeded in invoking off. Z-coordinate bias: ' + data.biasZ); 8569} 8570 8571sensor.off(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, callback); 8572``` 8573 8574### ORIENTATION<sup>(deprecated)</sup> 8575 8576 off(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback?: Callback<OrientationResponse>): void 8577 8578取消订阅传感器数据。 8579 8580> **说明**: 8581> 8582> 从API version 9 开始不再维护,建议使用[sensor.off.ORIENTATION](#orientation9-2)<sup>9+</sup>代替。 8583 8584**系统能力**:SystemCapability.Sensors.Sensor 8585 8586**参数**: 8587 8588| 参数名 | 类型 | 必填 | 说明 | 8589| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8590| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ORIENTATION | 是 | 要取消订阅的方向传感器类型为SENSOR_TYPE_ID_ORIENTATION。 | 8591| callback | Callback<[OrientationResponse](#orientationresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8592 8593**示例**: 8594 8595```ts 8596import { sensor } from '@kit.SensorServiceKit'; 8597 8598function callback(data: sensor.OrientationResponse) { 8599 console.info('Succeeded in invoking off. The device rotates at an angle around the X axis: ' + data.beta); 8600 console.info('Succeeded in invoking off. The device rotates at an angle around the Y axis: ' + data.gamma); 8601 console.info('Succeeded in invoking off. The device rotates at an angle around the Z axis: ' + data.alpha); 8602} 8603 8604sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, callback); 8605``` 8606 8607### PEDOMETER<sup>(deprecated)</sup> 8608 8609off(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback?: Callback<PedometerResponse>): void 8610 8611取消订阅传感器数据。 8612 8613> **说明**: 8614> 8615> 从API version 9 开始不再维护,建议使用[sensor.off.PEDOMETER](#pedometer9-2)<sup>9+</sup>代替。 8616 8617**需要权限**:ohos.permission.ACTIVITY_MOTION 8618 8619**系统能力**:SystemCapability.Sensors.Sensor 8620 8621**参数**: 8622 8623| 参数名 | 类型 | 必填 | 说明 | 8624| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8625| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER | 是 | 要取消订阅的计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。 | 8626| callback | Callback<[PedometerResponse](#pedometerresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8627 8628**示例**: 8629 8630```ts 8631import { sensor } from '@kit.SensorServiceKit'; 8632 8633function callback(data: sensor.PedometerResponse) { 8634 console.info('Succeeded in invoking off. Steps: ' + data.steps); 8635} 8636 8637sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, callback); 8638``` 8639 8640### PEDOMETER_DETECTION<sup>(deprecated)</sup> 8641 8642off(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback?: Callback<PedometerDetectionResponse>): void 8643 8644取消订阅传感器数据。 8645 8646> **说明**: 8647> 8648> 从API version 9 开始不再维护,建议使用[sensor.off.PEDOMETER_DETECTION](#pedometer_detection9-2)<sup>9+</sup>代替。 8649 8650**需要权限**:ohos.permission.ACTIVITY_MOTION 8651 8652**系统能力**:SystemCapability.Sensors.Sensor 8653 8654**参数**: 8655 8656| 参数名 | 类型 | 必填 | 说明 | 8657| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8658| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PEDOMETER_DETECTION | 是 | 要取消订阅的计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。 | 8659| callback | Callback<[PedometerDetectionResponse](#pedometerdetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8660 8661**示例**: 8662 8663```ts 8664import { sensor } from '@kit.SensorServiceKit'; 8665 8666function callback(data: sensor.PedometerDetectionResponse) { 8667 console.info('Succeeded in invoking off. Scalar data: ' + data.scalar); 8668} 8669 8670sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback); 8671``` 8672 8673### PROXIMITY<sup>(deprecated)</sup> 8674 8675off(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback?: Callback<ProximityResponse>): void 8676 8677取消订阅传感器数据。 8678 8679> **说明**: 8680> 8681> 从API version 9 开始不再维护,建议使用[sensor.off.PROXIMITY](#proximity9-2)<sup>9+</sup>代替。 8682 8683**系统能力**:SystemCapability.Sensors.Sensor 8684 8685**参数**: 8686 8687| 参数名 | 类型 | 必填 | 说明 | 8688| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8689| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_PROXIMITY | 是 | 要取消订阅的接近光传感器类型为SENSOR_TYPE_ID_PROXIMITY。 | 8690| callback | Callback<[ProximityResponse](#proximityresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8691 8692**示例**: 8693 8694```ts 8695import { sensor } from '@kit.SensorServiceKit'; 8696 8697function callback(data: sensor.ProximityResponse) { 8698 console.info('Succeeded in invoking off. Distance: ' + data.distance); 8699} 8700 8701sensor.off(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, callback); 8702``` 8703 8704### ROTATION_VECTOR<sup>(deprecated)</sup> 8705 8706off(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback?: Callback<RotationVectorResponse>): void 8707 8708取消订阅传感器数据。 8709 8710> **说明**: 8711> 8712> 从API version 9 开始不再维护,建议使用[sensor.off.ROTATION_VECTOR](#rotation_vector9-2)<sup>9+</sup>代替。 8713 8714**系统能力**:SystemCapability.Sensors.Sensor 8715 8716**参数**: 8717 8718| 参数名 | 类型 | 必填 | 说明 | 8719| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8720| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_ROTATION_VECTOR | 是 | 要取消订阅的旋转矢量传感器类型为SENSOR_TYPE_ID_ROTATION_VECTOR。 | 8721| callback | Callback<[RotationVectorResponse](#rotationvectorresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8722 8723**示例**: 8724 8725```ts 8726import { sensor } from '@kit.SensorServiceKit'; 8727 8728function callback(data: sensor.RotationVectorResponse) { 8729 console.info('Succeeded in invoking off. X-coordinate component: ' + data.x); 8730 console.info('Succeeded in invoking off. Y-coordinate component: ' + data.y); 8731 console.info('Succeeded in invoking off. Z-coordinate component: ' + data.z); 8732 console.info('Succeeded in invoking off. Scalar quantity: ' + data.w); 8733} 8734 8735sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 8736``` 8737 8738### SIGNIFICANT_MOTION<sup>(deprecated)</sup> 8739 8740off(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback?: Callback<SignificantMotionResponse>): void 8741 8742取消订阅有效运动传感器数据。 8743 8744> **说明**: 8745> 8746> 从API version 9 开始不再维护,建议使用[sensor.off.SIGNIFICANT_MOTION](#significant_motion9-2)<sup>9+</sup>代替。 8747 8748**系统能力**:SystemCapability.Sensors.Sensor 8749 8750**参数**: 8751 8752| 参数名 | 类型 | 必填 | 说明 | 8753| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8754| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 是 | 要取消订阅的有效运动传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。 | 8755| callback | Callback<[SignificantMotionResponse](#significantmotionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8756 8757**示例**: 8758 8759```ts 8760import { sensor } from '@kit.SensorServiceKit'; 8761 8762function callback(data: sensor.SignificantMotionResponse) { 8763 console.info('Succeeded in invoking off. Scalar data: ' + data.scalar); 8764} 8765 8766sensor.off(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback); 8767``` 8768 8769### WEAR_DETECTION<sup>(deprecated)</sup> 8770 8771off(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback?: Callback<WearDetectionResponse>): void 8772 8773取消订阅传感器数据。 8774 8775> **说明**: 8776> 8777> 从API version 9 开始不再维护,建议使用[sensor.off.WEAR_DETECTION](#wear_detection9-2)<sup>9+</sup>代替。 8778 8779**系统能力**:SystemCapability.Sensors.Sensor 8780 8781**参数**: 8782 8783| 参数名 | 类型 | 必填 | 说明 | 8784| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8785| type | [SensorType](#sensortypedeprecated).SENSOR_TYPE_ID_WEAR_DETECTION | 是 | 要取消订阅的佩戴检测传感器类型为SENSOR_TYPE_ID_WEAR_DETECTION。 | 8786| callback | Callback<[WearDetectionResponse](#weardetectionresponse)> | 否 | 需要取消订阅的回调函数,若无此参数,则取消订阅当前类型的所有回调函数。 | 8787 8788**示例**: 8789 8790```ts 8791import { sensor } from '@kit.SensorServiceKit'; 8792 8793function accCallback(data: sensor.WearDetectionResponse) { 8794 console.info('Succeeded in invoking off. Wear status: ' + data.value); 8795} 8796 8797sensor.off(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, accCallback); 8798``` 8799 8800## sensor.transformCoordinateSystem<sup>(deprecated)</sup> 8801 8802transformCoordinateSystem(inRotationVector: Array<number>, coordinates: CoordinatesOptions, callback: AsyncCallback<Array<number>>): void 8803 8804旋转提供的旋转矩阵,使其可以以不同的方式表示坐标系,使用Callback异步方式返回结果。 8805 8806> **说明**: 8807> 8808> 从API version 9 开始不再维护,建议使用[sensor.transformRotationMatrix](#sensortransformrotationmatrix9)<sup>9+</sup>代替。 8809 8810**系统能力**:SystemCapability.Sensors.Sensor 8811 8812**参数**: 8813 8814| 参数名 | 类型 | 必填 | 说明 | 8815| ---------------- | ----------------------------------------- | ---- | -------------------------- | 8816| inRotationVector | Array<number> | 是 | 表示旋转矩阵。 | 8817| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 表示坐标系方向。 | 8818| callback | AsyncCallback<Array<number>> | 是 | 异步返回转换后的旋转矩阵。 | 8819 8820**示例**: 8821 8822```ts 8823import { sensor } from '@kit.SensorServiceKit'; 8824import { BusinessError } from '@kit.BasicServicesKit'; 8825 8826sensor.transformCoordinateSystem([1, 0, 0, 0, 1, 0, 0, 0, 1], { x: 2, y: 3 }, 8827 (err: BusinessError, data: Array<number>) => { 8828 if (err) { 8829 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 8830 return; 8831 } 8832 console.info("Succeeded in starting Operation. Data obtained: " + data); 8833 for (let i = 0; i < data.length; i++) { 8834 console.info("Succeeded in getting transformCoordinateSystem data[ " + i + "] = " + data[i]); 8835 } 8836}) 8837``` 8838## sensor.transformCoordinateSystem<sup>(deprecated)</sup> 8839 8840transformCoordinateSystem(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>> 8841 8842旋转提供的旋转矩阵,使其可以以不同的方式表示坐标系,使用Promise异步方式返回结果。 8843 8844> **说明**: 8845> 8846> 从API version 9 开始不再维护,建议使用[sensor.transformRotationMatrix](#sensortransformrotationmatrix9-1)<sup>9+</sup>代替。 8847 8848**系统能力**:SystemCapability.Sensors.Sensor 8849 8850**参数**: 8851 8852| 参数名 | 类型 | 必填 | 说明 | 8853| ---------------- | ---------------------------------------- | ---- | -------- | 8854| inRotationVector | Array<number> | 是 | 表示旋转矩阵。 | 8855| coordinates | [CoordinatesOptions](#coordinatesoptions) | 是 | 表示坐标系方向。 | 8856 8857**返回值**: 8858 8859| 类型 | 说明 | 8860| ---------------------------------- | ---------------------------------- | 8861| Promise<Array<number>> | 使用异步方式返回转换后的旋转矩阵。 | 8862 8863**示例**: 8864 8865```ts 8866import { sensor } from '@kit.SensorServiceKit'; 8867import { BusinessError } from '@kit.BasicServicesKit'; 8868 8869const promise = sensor.transformCoordinateSystem([1, 0, 0, 0, 1, 0, 0, 0, 1], { x: 2, y: 3 }); 8870promise.then((data: Array<number>) => { 8871 console.info("Succeeded in starting Operation"); 8872 for (let i = 0; i < data.length; i++) { 8873 console.info("Succeeded in getting transformCoordinateSystem data[ " + i + "] = " + data[i]); 8874 } 8875}).catch((err: BusinessError) => { 8876 console.error(`Failed to operate.`); 8877}) 8878``` 8879 8880## sensor.getGeomagneticField<sup>(deprecated)</sup> 8881 8882getGeomagneticField(locationOptions: LocationOptions, timeMillis: number, callback: AsyncCallback<GeomagneticResponse>): void 8883 8884获取地球上特定位置的地磁场,使用callback异步方式返回结果。 8885 8886> **说明**: 8887> 8888> 从API version 9 开始不再维护,建议使用[sensor.getGeomagneticInfo](#sensorgetgeomagneticinfo9)<sup>9+</sup>代替。 8889 8890**系统能力**:SystemCapability.Sensors.Sensor 8891 8892**参数**: 8893 8894| 参数名 | 类型 | 必填 | 说明 | 8895| --------------- | ------------------------------------------------------------ | ---- | ---------------------------------- | 8896| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置。 | 8897| timeMillis | number | 是 | 表示获取磁偏角的时间,单位为毫秒。 | 8898| callback | AsyncCallback<[GeomagneticResponse](#geomagneticresponse)> | 是 | 异步返回磁场信息。 | 8899 8900**示例**: 8901 8902```ts 8903import { sensor } from '@kit.SensorServiceKit'; 8904import { BusinessError } from '@kit.BasicServicesKit'; 8905 8906sensor.getGeomagneticField({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000, 8907 (err: BusinessError, data: sensor.GeomagneticResponse) => { 8908 if (err) { 8909 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 8910 return; 8911 } 8912 console.info('Succeeded in getting sensor_getGeomagneticField_callback x: ' + data.x + ',y: ' + data.y + ',z: ' + 8913 data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + 8914 ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); 8915}); 8916``` 8917## sensor.getGeomagneticField<sup>(deprecated)</sup> 8918 8919getGeomagneticField(locationOptions: LocationOptions, timeMillis: number): Promise<GeomagneticResponse> 8920 8921获取地球上特定位置的地磁场,使用Promise异步方式返回结果。 8922 8923> **说明**: 8924> 8925> 从API version 9 开始不再维护,建议使用[sensor.getGeomagneticInfo](#sensorgetgeomagneticinfo9-1)<sup>9+</sup>代替。 8926 8927**系统能力**:SystemCapability.Sensors.Sensor 8928 8929**参数**: 8930 8931| 参数名 | 类型 | 必填 | 说明 | 8932| --------------- | ----------------------------------- | ---- | ----------------- | 8933| locationOptions | [LocationOptions](#locationoptions) | 是 | 地理位置。 | 8934| timeMillis | number | 是 | 表示获取磁偏角的时间,单位为毫秒。 | 8935 8936**返回值**: 8937 8938| 类型 | 说明 | 8939| ---------------------------------------------------------- | -------------------------- | 8940| Promise<[GeomagneticResponse](#geomagneticresponse)> | 使用异步方式返回磁场信息。 | 8941 8942**示例**: 8943 8944```ts 8945import { sensor } from '@kit.SensorServiceKit'; 8946import { BusinessError } from '@kit.BasicServicesKit'; 8947 8948const promise = sensor.getGeomagneticField({ latitude: 80, longitude: 0, altitude: 0 }, 1580486400000); 8949promise.then((data: sensor.GeomagneticResponse) => { 8950 console.info('Succeeded in getting sensor_getGeomagneticField_promise x: ' + data.x + ',y: ' + data.y + ',z: ' + 8951 data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + 8952 ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); 8953}).catch((reason: BusinessError) => { 8954 console.error(`Failed to operate.`); 8955}) 8956``` 8957 8958## sensor.getAltitude<sup>(deprecated)</sup> 8959 8960getAltitude(seaPressure: number, currentPressure: number, callback: AsyncCallback<number>): void 8961 8962根据气压值获取设备所在的海拔高度,使用Callback异步方式返回结果。 8963 8964> **说明**: 8965> 8966> 从API version 9 开始不再维护,建议使用[sensor.getDeviceAltitude](#sensorgetdevicealtitude9)<sup>9+</sup>代替。 8967 8968**系统能力**:SystemCapability.Sensors.Sensor 8969 8970**参数**: 8971 8972| 参数名 | 类型 | 必填 | 说明 | 8973| --------------- | --------------------------- | ---- | -------------------------------------- | 8974| seaPressure | number | 是 | 表示海平面气压值,单位为hPa。 | 8975| currentPressure | number | 是 | 表示设备所在高度的气压值,单位为hPa。 | 8976| callback | AsyncCallback<number> | 是 | 异步返回设备所在的海拔高度,单位为米。 | 8977 8978**示例**: 8979 8980```ts 8981import { sensor } from '@kit.SensorServiceKit'; 8982import { BusinessError } from '@kit.BasicServicesKit'; 8983 8984sensor.getAltitude(0, 200, (err: BusinessError, data: number) => { 8985 if (err) { 8986 console.error(`Failed to operate. Code: ${err.code}, message: ${err.message}`); 8987 return; 8988 } 8989 console.info("Succeeded in getting getAltitude interface get data: " + data); 8990}); 8991``` 8992 8993## sensor.getAltitude<sup>(deprecated)</sup> 8994 8995getAltitude(seaPressure: number, currentPressure: number): Promise<number> 8996 8997根据气压值获取设备所在的海拔高度,使用Promise异步方式返回结果。 8998 8999> **说明**: 9000> 9001> 从API version 9 开始不再维护,建议使用[sensor.getDeviceAltitude](#sensorgetdevicealtitude9-1)<sup>9+</sup>代替。 9002 9003**系统能力**:SystemCapability.Sensors.Sensor 9004 9005**参数**: 9006 9007| 参数名 | 类型 | 必填 | 说明 | 9008| --------------- | ------ | ---- | -------------------- | 9009| seaPressure | number | 是 | 表示海平面气压值,单位为hPa。 | 9010| currentPressure | number | 是 | 表示设备所在高度的气压值,单位为hPa。 | 9011 9012**返回值**: 9013 9014| 类型 | 说明 | 9015| --------------------- | ------------------------------------------------ | 9016| Promise<number> | 使用异步方式返回设备所在的海拔高度(单位:米)。 | 9017 9018**示例**: 9019 9020```ts 9021import { sensor } from '@kit.SensorServiceKit'; 9022import { BusinessError } from '@kit.BasicServicesKit'; 9023 9024const promise = sensor.getAltitude(0, 200); 9025promise.then((data: number) => { 9026 console.info('Succeeded in getting sensor_getAltitude_Promise success', data); 9027}).catch((err: BusinessError) => { 9028 console.error(`Failed to operate.`); 9029}) 9030``` 9031 9032 9033## sensor.getGeomagneticDip<sup>(deprecated)</sup> 9034 9035getGeomagneticDip(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void 9036 9037根据倾斜矩阵计算地磁倾斜角,使用Callback异步方式返回结果。 9038 9039> **说明**: 9040> 9041> 从API version 9 开始不再维护,建议使用[sensor.getInclination](#sensorgetinclination9)<sup>9+</sup>代替。 9042 9043**系统能力**:SystemCapability.Sensors.Sensor 9044 9045**参数**: 9046 9047| 参数名 | 类型 | 必填 | 说明 | 9048| ----------------- | --------------------------- | ---- | -------------------------------- | 9049| inclinationMatrix | Array<number> | 是 | 表示倾斜矩阵。 | 9050| callback | AsyncCallback<number> | 是 | 异步返回地磁倾斜角,单位为弧度。 | 9051 9052**示例**: 9053 9054```ts 9055import { sensor } from '@kit.SensorServiceKit'; 9056import { BusinessError } from '@kit.BasicServicesKit'; 9057 9058sensor.getGeomagneticDip([1, 0, 0, 0, 1, 0, 0, 0, 1], (err: BusinessError, data: number) => { 9059 if (err) { 9060 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9061 return; 9062 } 9063 console.info("Succeeded in getting getGeomagneticDip interface get data: " + data); 9064}) 9065``` 9066 9067## sensor.getGeomagneticDip<sup>(deprecated)</sup> 9068 9069getGeomagneticDip(inclinationMatrix: Array<number>): Promise<number> 9070 9071根据倾斜矩阵计算地磁倾斜角,使用Promise异步方式返回结果。 9072 9073> **说明**: 9074> 9075> 从API version 9 开始不再维护,建议使用[sensor.getInclination](#sensorgetinclination9-1)<sup>9+</sup>代替。 9076 9077**系统能力**:SystemCapability.Sensors.Sensor 9078 9079**参数**: 9080 9081| 参数名 | 类型 | 必填 | 说明 | 9082| ----------------- | ------------------- | ---- | ------- | 9083| inclinationMatrix | Array<number> | 是 | 表示倾斜矩阵。 | 9084 9085**返回值**: 9086 9087| 类型 | 说明 | 9088| --------------------- | ---------------------------------------- | 9089| Promise<number> | 使用异步方式返回地磁倾斜角,单位为弧度。 | 9090 9091**示例**: 9092 9093```ts 9094import { sensor } from '@kit.SensorServiceKit'; 9095import { BusinessError } from '@kit.BasicServicesKit'; 9096 9097const promise = sensor.getGeomagneticDip([1, 0, 0, 0, 1, 0, 0, 0, 1]); 9098promise.then((data: number) => { 9099 console.info('Succeeded in get GeomagneticDip_promise', data); 9100}).catch((err: BusinessError) => { 9101 console.error(`Failed to operate.`); 9102}) 9103``` 9104 9105## sensor. getAngleModify<sup>(deprecated)</sup> 9106 9107getAngleModify(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 9108 9109获取两个旋转矩阵之间的角度变化,使用Callback异步方式返回结果。 9110 9111> **说明**: 9112> 9113> 从API version 9 开始不再维护,建议使用[sensor.getAngleVariation](#sensorgetanglevariation9)<sup>9+</sup>代替。 9114 9115**系统能力**:SystemCapability.Sensors.Sensor 9116 9117**参数**: 9118 9119| 参数名 | 类型 | 必填 | 说明 | 9120| --------------------- | ---------------------------------------- | ---- | ------------------------------------- | 9121| currentRotationMatrix | Array<number> | 是 | 表示当前旋转矩阵。 | 9122| preRotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 9123| callback | AsyncCallback<Array<number>> | 是 | 异步返回z、x、y轴方向的旋转角度变化。 | 9124 9125**示例**: 9126 9127```ts 9128import { sensor } from '@kit.SensorServiceKit'; 9129import { BusinessError } from '@kit.BasicServicesKit'; 9130 9131sensor.getAngleModify([1, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0.87, -0.50, 0, 0.50, 0.87], 9132 (err: BusinessError, data: Array<number>) => { 9133 if (err) { 9134 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9135 return; 9136 } 9137 for (let i = 0; i < data.length; i++) { 9138 console.info("data[" + i + "]: " + data[i]); 9139 } 9140}) 9141``` 9142 9143## sensor. getAngleModify<sup>(deprecated)</sup> 9144 9145getAngleModify(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>> 9146 9147获取两个旋转矩阵之间的角度变化,使用Promise异步方式返回结果。 9148 9149> **说明**: 9150> 9151> 从API version 9 开始不再维护,建议使用[sensor.getAngleVariation](#sensorgetanglevariation9-1)<sup>9+</sup>代替。 9152 9153**系统能力**:SystemCapability.Sensors.Sensor 9154 9155**参数**: 9156 9157| 参数名 | 类型 | 必填 | 说明 | 9158| --------------------- | ------------------- | ---- | --------- | 9159| currentRotationMatrix | Array<number> | 是 | 表示当前旋转矩阵。 | 9160| preRotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 9161 9162**返回值**: 9163 9164| 类型 | 说明 | 9165| ---------------------------------- | --------------------------------------------- | 9166| Promise<Array<number>> | 使用异步方式返回z、x、y轴方向的旋转角度变化。 | 9167 9168**示例**: 9169 9170```ts 9171import { sensor } from '@kit.SensorServiceKit'; 9172import { BusinessError } from '@kit.BasicServicesKit'; 9173 9174const 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]); 9175promise.then((data: Array<number>) => { 9176 console.info('Succeeded in getting AngleModify_promise.'); 9177 for (let i = 0; i < data.length; i++) { 9178 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 9179 } 9180}).catch((reason: BusinessError) => { 9181 let e: BusinessError = reason as BusinessError; 9182 console.info("Succeeded in getting promise::catch", e); 9183}) 9184``` 9185 9186## sensor.createRotationMatrix<sup>(deprecated)</sup> 9187 9188createRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 9189 9190将旋转矢量转换为旋转矩阵,使用Callback异步方式返回结果。 9191 9192> **说明**: 9193> 9194> 从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9)<sup>9+</sup>代替。 9195 9196**系统能力**:SystemCapability.Sensors.Sensor 9197 9198**参数**: 9199 9200| 参数名 | 类型 | 必填 | 说明 | 9201| -------------- | ---------------------------------------- | ---- | ------------------ | 9202| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 9203| callback | AsyncCallback<Array<number>> | 是 | 异步返回旋转矩阵。 | 9204 9205**示例**: 9206 9207```ts 9208import { sensor } from '@kit.SensorServiceKit'; 9209import { BusinessError } from '@kit.BasicServicesKit'; 9210 9211sensor.createRotationMatrix([0.20046076, 0.21907, 0.73978853, 0.60376877], 9212 (err: BusinessError, data: Array<number>) => { 9213 if (err) { 9214 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9215 return; 9216 } 9217 for (let i = 0; i < data.length; i++) { 9218 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 9219 } 9220}) 9221``` 9222 9223## sensor.createRotationMatrix<sup>(deprecated)</sup> 9224 9225createRotationMatrix(rotationVector: Array<number>): Promise<Array<number>> 9226 9227将旋转矢量转换为旋转矩阵,使用Promise异步方式返回结果。 9228 9229> **说明**: 9230> 9231> 从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9-1)<sup>9+</sup>代替。 9232 9233**系统能力**:SystemCapability.Sensors.Sensor 9234 9235**参数**: 9236 9237| 参数名 | 类型 | 必填 | 说明 | 9238| -------------- | ------------------- | ---- | ------- | 9239| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 9240 9241**返回值**: 9242 9243| 类型 | 说明 | 9244| ---------------------------------- | -------------------------- | 9245| Promise<Array<number>> | 使用异步方式返回旋转矩阵。 | 9246 9247**示例**: 9248 9249 ```ts 9250import { sensor } from '@kit.SensorServiceKit'; 9251import { BusinessError } from '@kit.BasicServicesKit'; 9252 9253const promise = sensor.createRotationMatrix([0.20046076, 0.21907, 0.73978853, 0.60376877]); 9254promise.then((data: Array<number>) => { 9255 console.info('Succeeded in getting createRotationMatrix_promise'); 9256 for (let i = 0; i < data.length; i++) { 9257 console.info("data[" + i + "]: " + data[i]); 9258 } 9259}).catch((reason: BusinessError) => { 9260 console.info("Succeeded in getting promise::catch", reason); 9261}) 9262 ``` 9263 9264## sensor.createQuaternion<sup>(deprecated)</sup> 9265 9266createQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void 9267 9268将旋转矢量转换为四元数,使用Callback异步方式返回结果。 9269 9270> **说明**: 9271> 9272> 从API version 9 开始不再维护,建议使用[sensor.getQuaternion](#sensorgetquaternion9)<sup>9+</sup>代替。 9273 9274**系统能力**:SystemCapability.Sensors.Sensor 9275 9276**参数**: 9277 9278| 参数名 | 类型 | 必填 | 说明 | 9279| -------------- | ---------------------------------------- | ---- | ---------------- | 9280| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 9281| callback | AsyncCallback<Array<number>> | 是 | 异步返回四元数。 | 9282 9283**示例**: 9284 9285```ts 9286import { sensor } from '@kit.SensorServiceKit'; 9287import { BusinessError } from '@kit.BasicServicesKit'; 9288 9289sensor.createQuaternion([0.20046076, 0.21907, 0.73978853, 0.60376877], 9290 (err: BusinessError, data: Array<number>) => { 9291 if (err) { 9292 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9293 return; 9294 } 9295 for (let i = 0; i < data.length; i++) { 9296 console.info("Succeeded in getting data[" + i + "]: " + data[i]); 9297 } 9298}) 9299``` 9300 9301## sensor.createQuaternion<sup>(deprecated)</sup> 9302 9303createQuaternion(rotationVector: Array<number>): Promise<Array<number>> 9304 9305将旋转矢量转换为四元数,使用Promise异步方式返回结果。 9306 9307> **说明**: 9308> 9309> 从API version 9 开始不再维护,建议使用[sensor.getQuaternion](#sensorgetquaternion9-1)<sup>9+</sup>代替。 9310 9311**系统能力**:SystemCapability.Sensors.Sensor 9312 9313**参数**: 9314 9315| 参数名 | 类型 | 必填 | 说明 | 9316| -------------- | ------------------- | ---- | ------- | 9317| rotationVector | Array<number> | 是 | 表示旋转矢量。 | 9318 9319**返回值**: 9320 9321| 类型 | 说明 | 9322| ---------------------------------- | ------------------------ | 9323| Promise<Array<number>> | 使用异步方式返回四元数。 | 9324 9325**示例**: 9326 9327```ts 9328import { sensor } from '@kit.SensorServiceKit'; 9329import { BusinessError } from '@kit.BasicServicesKit'; 9330 9331const promise = sensor.createQuaternion([0.20046076, 0.21907, 0.73978853, 0.60376877]); 9332promise.then((data: Array<number>) => { 9333 console.info('Succeeded in getting createQuaternion_promise'); 9334 for (let i = 0; i < data.length; i++) { 9335 console.info("data[" + i + "]: " + data[i]); 9336 } 9337}).catch((err: BusinessError) => { 9338 console.error(`Failed to get promise.`); 9339}) 9340``` 9341 9342## sensor.getDirection<sup>(deprecated)</sup> 9343 9344getDirection(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void 9345 9346根据旋转矩阵计算设备的方向,使用Callback异步方式返回结果。 9347 9348> **说明**: 9349> 9350> 从API version 9 开始不再维护,建议使用[sensor.getOrientation](#sensorgetorientation9)<sup>9+</sup>代替。 9351 9352**系统能力**:SystemCapability.Sensors.Sensor 9353 9354**参数**: 9355 9356| 参数名 | 类型 | 必填 | 说明 | 9357| -------------- | ---------------------------------------- | ---- | ------------------------------------- | 9358| rotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 9359| callback | AsyncCallback<Array<number>> | 是 | 异步返回围绕z、x、y轴方向的旋转角度。 | 9360 9361**示例**: 9362 9363```ts 9364import { sensor } from '@kit.SensorServiceKit'; 9365import { BusinessError } from '@kit.BasicServicesKit'; 9366 9367sensor.getDirection([1, 0, 0, 0, 1, 0, 0, 0, 1], (err: BusinessError, data: Array<number>) => { 9368 if (err) { 9369 console.error(`Failed to register data. Code: ${err.code}, message: ${err.message}`); 9370 return; 9371 } 9372 console.info("Succeeded in getting getDirection interface get data: " + data); 9373 for (let i = 1; i < data.length; i++) { 9374 console.info("Succeeded in getting sensor_getDirection_callback" + data[i]); 9375 } 9376}) 9377``` 9378 9379## sensor.getDirection<sup>(deprecated)</sup> 9380 9381getDirection(rotationMatrix: Array<number>): Promise<Array<number>> 9382 9383根据旋转矩阵计算设备的方向,使用Promise异步方式返回结果。 9384 9385> **说明**: 9386> 9387> 从API version 9 开始不再维护,建议使用[sensor.getOrientation](#sensorgetorientation9-1)<sup>9+</sup>代替。 9388 9389**系统能力**:SystemCapability.Sensors.Sensor 9390 9391**参数**: 9392 9393| 参数名 | 类型 | 必填 | 说明 | 9394| -------------- | ------------------- | ---- | ------- | 9395| rotationMatrix | Array<number> | 是 | 表示旋转矩阵。 | 9396 9397**返回值**: 9398 9399| 类型 | 说明 | 9400| ---------------------------------- | --------------------------------------------- | 9401| Promise<Array<number>> | 使用异步方式返回围绕z、x、y轴方向的旋转角度。 | 9402 9403**示例**: 9404 9405```ts 9406import { sensor } from '@kit.SensorServiceKit'; 9407import { BusinessError } from '@kit.BasicServicesKit'; 9408 9409const promise = sensor.getDirection([1, 0, 0, 0, 1, 0, 0, 0, 1]); 9410promise.then((data: Array<number>) => { 9411 console.info('Succeeded in getting sensor_getAltitude_Promise', data); 9412 for (let i = 1; i < data.length; i++) { 9413 console.info("Succeeded in getting sensor_getDirection_promise" + data[i]); 9414 } 9415}).catch((err: BusinessError) => { 9416 console.error(`Failed to get promise.`); 9417}) 9418``` 9419 9420## sensor.createRotationMatrix<sup>(deprecated)</sup> 9421 9422createRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void 9423 9424根据重力矢量和地磁矢量计算旋转矩阵,使用Callback异步方式返回结果。 9425 9426> **说明**: 9427> 9428> 从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9-2)<sup>9+</sup>代替。 9429 9430**系统能力**:SystemCapability.Sensors.Sensor 9431 9432**参数**: 9433 9434| 参数名 | 类型 | 必填 | 说明 | 9435| ----------- | ------------------------------------------------------------ | ---- | ------------------ | 9436| gravity | Array<number> | 是 | 表示重力向量。 | 9437| geomagnetic | Array<number> | 是 | 表示地磁矢量。 | 9438| callback | AsyncCallback<[RotationMatrixResponse](#rotationmatrixresponse)> | 是 | 异步返回旋转矩阵。 | 9439 9440**示例**: 9441 9442```ts 9443import { sensor } from '@kit.SensorServiceKit'; 9444import { BusinessError } from '@kit.BasicServicesKit'; 9445 9446sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444], 9447 (err: BusinessError, data: sensor.RotationMatrixResponse) => { 9448 if (err) { 9449 console.error(`Failed to get create rotationMatrix. Code: ${err.code}, message: ${err.message}`); 9450 return; 9451 } 9452 console.info(JSON.stringify(data)); 9453}) 9454``` 9455 9456## sensor.createRotationMatrix<sup>(deprecated)</sup> 9457 9458createRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>): Promise<RotationMatrixResponse> 9459 9460根据重力矢量和地磁矢量计算旋转矩阵,使用Promise异步方式返回结果。 9461 9462> **说明**: 9463> 9464> 从API version 9 开始不再维护,建议使用[sensor.getRotationMatrix](#sensorgetrotationmatrix9-3)<sup>9+</sup>代替。 9465 9466**系统能力**:SystemCapability.Sensors.Sensor 9467 9468**参数**: 9469 9470| 参数名 | 类型 | 必填 | 说明 | 9471| ----------- | ------------------- | ---- | ------- | 9472| gravity | Array<number> | 是 | 表示重力向量。 | 9473| geomagnetic | Array<number> | 是 | 表示地磁矢量。 | 9474 9475**返回值**: 9476 9477| 类型 | 说明 | 9478| ------------------------------------------------------------ | -------------------------- | 9479| Promise<[RotationMatrixResponse](#rotationmatrixresponse)> | 使用异步方式返回旋转矩阵。 | 9480 9481**示例**: 9482 9483```ts 9484import { sensor } from '@kit.SensorServiceKit'; 9485import { BusinessError } from '@kit.BasicServicesKit'; 9486 9487const promise = sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444]); 9488promise.then((data: sensor.RotationMatrixResponse) => { 9489 console.info(JSON.stringify(data)); 9490}).catch((err: BusinessError) => { 9491 console.error(`Failed to get promise.`); 9492}) 9493``` 9494 9495## SensorType<sup>(deprecated)</sup> 9496 9497表示要订阅或取消订阅的传感器类型。 9498 9499**系统能力**:SystemCapability.Sensors.Sensor 9500 9501| 名称 | 值 | 说明 | 9502| ------------------------------------------ | ---- | ---------------------- | 9503| SENSOR_TYPE_ID_ACCELEROMETER | 1 | 加速度传感器。 | 9504| SENSOR_TYPE_ID_GYROSCOPE | 2 | 陀螺仪传感器。 | 9505| SENSOR_TYPE_ID_AMBIENT_LIGHT | 5 | 环境光传感器。 | 9506| SENSOR_TYPE_ID_MAGNETIC_FIELD | 6 | 磁场传感器。 | 9507| SENSOR_TYPE_ID_BAROMETER | 8 | 气压计传感器。 | 9508| SENSOR_TYPE_ID_HALL | 10 | 霍尔传感器。 | 9509| SENSOR_TYPE_ID_PROXIMITY | 12 | 接近光传感器。 | 9510| SENSOR_TYPE_ID_HUMIDITY | 13 | 湿度传感器。 | 9511| SENSOR_TYPE_ID_ORIENTATION | 256 | 方向传感器。 | 9512| SENSOR_TYPE_ID_GRAVITY | 257 | 重力传感器。 | 9513| SENSOR_TYPE_ID_LINEAR_ACCELERATION | 258 | 线性加速度传感器。 | 9514| SENSOR_TYPE_ID_ROTATION_VECTOR | 259 | 旋转矢量传感器。 | 9515| SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 260 | 环境温度传感器。 | 9516| SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 261 | 未校准磁场传感器。 | 9517| SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED | 263 | 未校准陀螺仪传感器。 | 9518| SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 264 | 有效运动传感器。 | 9519| SENSOR_TYPE_ID_PEDOMETER_DETECTION | 265 | 计步检测传感器。 | 9520| SENSOR_TYPE_ID_PEDOMETER | 266 | 计步传感器。 | 9521| SENSOR_TYPE_ID_HEART_RATE | 278 | 心率传感器。 | 9522| SENSOR_TYPE_ID_WEAR_DETECTION | 280 | 佩戴检测传感器。 | 9523| SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 281 | 未校准加速度计传感器。 | 9524 9525