1/* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16export interface AccelerometerResponse { 17 /** 18 * X-coordinate 19 * @since 3 20 */ 21 x: number; 22 23 /** 24 * Y-coordinate 25 * @since 3 26 */ 27 y: number; 28 29 /** 30 * Z-coordinate 31 * @since 3 32 */ 33 z: number; 34} 35 36export interface CompassResponse { 37 /** 38 * Direction of the device (in degrees). 39 * @since 3 40 */ 41 direction: number; 42} 43 44export interface LightResponse { 45 /** 46 * Light intensity, in lux. 47 * @since 3 48 */ 49 intensity: number; 50} 51 52export interface StepCounterResponse { 53 /** 54 * Number of steps counted. 55 * Each time the device restarts, the value is recalculated from 0. 56 * @since 3 57 */ 58 steps: number; 59} 60 61export interface BarometerResponse { 62 /** 63 * Pressure, in pascal. 64 * @since 3 65 */ 66 pressure: number; 67} 68 69export interface DeviceOrientationResponse { 70 /** 71 * alpha 72 * @since 6 73 */ 74 alpha: number; 75 76 /** 77 * beta 78 * @since 6 79 */ 80 beta: number; 81 82 /** 83 * gamma 84 * @since 6 85 */ 86 gamma: number; 87} 88 89export interface GyroscopeResponse { 90 /** 91 * X-coordinate 92 * @since 6 93 */ 94 x: number; 95 96 /** 97 * Y-coordinate 98 * @since 6 99 */ 100 y: number; 101 102 /** 103 * Z-coordinate 104 * @since 6 105 */ 106 z: number; 107} 108 109export interface GravityResponse { 110 /** 111 * X-coordinate 112 * @since 7 113 */ 114 x: number; 115 116 /** 117 * Y-coordinate 118 * @since 7 119 */ 120 y: number; 121 122 /** 123 * Z-coordinate 124 * @since 7 125 */ 126 z: number; 127} 128 129export interface MagneticResponse { 130 /** 131 * X-coordinate 132 * @since 7 133 */ 134 x: number; 135 136 /** 137 * Y-coordinate 138 * @since 7 139 */ 140 y: number; 141 142 /** 143 * Z-coordinate 144 * @since 7 145 */ 146 z: number; 147} 148 149export interface HallResponse { 150 /** 151 * Indicates the hall sensor data. 152 * @since 7 153 */ 154 value: number; 155} 156 157/** 158 * @Syscap SysCap.ACE.UIEngine 159 */ 160export default class Sensor { 161 /** 162 * Listens to acceleration sensor data changes. 163 * If this API is called multiple times, the last call takes effect. 164 * @param options 165 */ 166 static subscribeAccelerometer(options: { 167 /** 168 * Execution frequency of the callback function for listening to acceleration sensor data. 169 * The default value is normal. 170 * @since 3 171 */ 172 interval: string; 173 174 /** 175 * Called when acceleration sensor data changes. 176 * @since 3 177 */ 178 success: (data: AccelerometerResponse) => void; 179 180 /** 181 * Called when the listening fails. 182 * @since 3 183 */ 184 fail?: (data: any, code: number) => void; 185 }): void; 186 187 /** 188 * Cancels listening to acceleration sensor data. 189 */ 190 static unsubscribeAccelerometer(): void; 191 192 /** 193 * Listens to compass sensor data changes. 194 * If this API is called multiple times, the last call takes effect. 195 * @param options Options. 196 */ 197 static subscribeCompass(options: { 198 /** 199 * Called when compass sensor data changes. 200 * @since 3 201 */ 202 success: (data: CompassResponse) => void; 203 204 /** 205 * Called when the listening fails. 206 * @since 3 207 */ 208 fail?: (data: any, code: number) => void; 209 }): void; 210 211 /** 212 * Cancels listening to compass sensor data. 213 */ 214 static unsubscribeCompass(): void; 215 216 /** 217 * Listens to distance sensor data changes. 218 * If this API is called multiple times, the last call takes effect. 219 * @param options 220 */ 221 static subscribeProximity(options: { 222 /** 223 * Called when distance sensor data changes. 224 * @since 3 225 */ 226 success: (ret: { distance: number}) => void; 227 228 /** 229 * Called when the listening fails. 230 * @since 3 231 */ 232 fail?: (data: any, code: number) => void; 233 }): void; 234 235 /** 236 * Cancels listening to distance sensor data. 237 * @param options 238 */ 239 static unsubscribeProximity(): void; 240 241 /** 242 * Listens to ambient light sensor data changes. 243 * If this API is called multiple times, the last call takes effect. 244 * @param options 245 */ 246 static subscribeLight(options: { 247 /** 248 * Called when ambient light sensor data changes. 249 * @since 3 250 */ 251 success: (data: LightResponse) => void; 252 253 /** 254 * Called when the listening fails. 255 * @since 3 256 */ 257 fail?: (data: any, code: number) => void; 258 }): void; 259 260 /** 261 * Cancels listening to ambient light sensor data. 262 */ 263 static unsubscribeLight(): void; 264 265 /** 266 * Listens to step counter sensor data changes. 267 * If this API is called multiple times, the last call takes effect. 268 * @param options 269 */ 270 static subscribeStepCounter(options: { 271 /** 272 * Called when step counter sensor data changes. 273 * @since 3 274 */ 275 success: (data: StepCounterResponse) => void; 276 277 /** 278 * Called when the listening fails. 279 * @since 3 280 */ 281 fail?: (data: any, code: number) => void; 282 }): void; 283 284 /** 285 * Cancels listening to step counter sensor data. 286 */ 287 static unsubscribeStepCounter(): void; 288 289 /** 290 * Listens to barometer sensor data changes. 291 * If this API is called multiple times, the last call takes effect. 292 * @param options 293 */ 294 static subscribeBarometer(options: { 295 /** 296 * Called when the barometer sensor data changes. 297 * @since 3 298 */ 299 success: (data: BarometerResponse) => void; 300 301 /** 302 * Called when the listening fails. 303 * @since 3 304 */ 305 fail?: (data: any, code: number) => void; 306 }): void; 307 308 /** 309 * Cancels listening to barometer sensor data. 310 */ 311 static unsubscribeBarometer(): void; 312 313 /** 314 * Listens to device orientation sensor data changes. 315 * If this API is called multiple times, the last call takes effect. 316 * @param options 317 */ 318 static subscribeDeviceOrientation(options: { 319 /** 320 * Execution frequency of the callback function for listening to device orientation sensor data. 321 * The default value is normal. 322 * @since 6 323 */ 324 interval: string; 325 326 /** 327 * Called when device orientation sensor data changes. 328 * @since 6 329 */ 330 success: (data: DeviceOrientationResponse) => void; 331 332 /** 333 * Called when the listening fails. 334 * @since 6 335 */ 336 fail?: (data: any, code: number) => void; 337 }): void; 338 339 /** 340 * Cancels listening to device orientation sensor data. 341 */ 342 static unsubscribeDeviceOrientation(): void; 343 344 /** 345 * Listens to gyroscope sensor data changes. 346 * If this API is called multiple times, the last call takes effect. 347 * @param options 348 */ 349 static subscribeGyroscope(options: { 350 /** 351 * Execution frequency of the callback function for listening to gyroscope sensor data. 352 * The default value is normal. 353 * @since 6 354 */ 355 interval: string; 356 357 /** 358 * Called when gyroscope sensor data changes. 359 * @since 6 360 */ 361 success: (data: GyroscopeResponse) => void; 362 363 /** 364 * Called when the listening fails. 365 * @since 6 366 */ 367 fail?: (data: any, code: number) => void; 368 }): void; 369 370 /** 371 * Cancels listening to gyroscope sensor data. 372 */ 373 static unsubscribeGyroscope(): void; 374 375 /** 376 * Listens to gravity sensor data changes. 377 * If this API is called multiple times, the last call takes effect. 378 * @param options 379 */ 380 static subscribeGravity(options: { 381 /** 382 * Execution frequency of the callback function for listening to gravity sensor data. 383 * The default value is normal. 384 * @since 7 385 */ 386 interval: string; 387 388 /** 389 * Called when gravity sensor data changes. 390 * @since 7 391 */ 392 success: (data: GravityResponse) => void; 393 394 /** 395 * Called when the listening fails. 396 * @since 7 397 */ 398 fail?: (data: any, code: number) => void; 399 }): void; 400 401 /** 402 * Cancels listening to gravity sensor data. 403 */ 404 static unsubscribeGravity(): void; 405 406 /** 407 * Listens to magnetic sensor data changes. 408 * If this API is called multiple times, the last call takes effect. 409 * @param options 410 */ 411 static subscribeMagnetic(options: { 412 /** 413 * Execution frequency of the callback function for listening to magnetic sensor data. 414 * The default value is normal. 415 * @since 7 416 */ 417 interval: string; 418 419 /** 420 * Called when magnetic sensor data changes. 421 * @since 7 422 */ 423 success: (data: MagneticResponse) => void; 424 425 /** 426 * Called when the listening fails. 427 * @since 7 428 */ 429 fail?: (data: any, code: number) => void; 430 }): void; 431 432 /** 433 * Cancels listening to magnetic sensor data. 434 */ 435 static unsubscribeMagnetic(): void; 436 437 /** 438 * Listens to hall sensor data changes. 439 * If this API is called multiple times, the last call takes effect. 440 * @param options 441 */ 442 static subscribeHall(options: { 443 /** 444 * Execution frequency of the callback function for listening to hall sensor data. 445 * The default value is normal. 446 * @since 7 447 */ 448 interval: string; 449 450 /** 451 * Called when hall sensor data changes. 452 * @since 7 453 */ 454 success: (data: HallResponse) => void; 455 456 /** 457 * Called when the listening fails. 458 * @since 7 459 */ 460 fail?: (data: any, code: number) => void; 461 }): void; 462 463 /** 464 * Cancels listening to hall sensor data. 465 */ 466 static unsubscribeHall(): void; 467} 468