Sensor.java revision 2f8b91449efafa5fb0057cd0150233681c65c9e3
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18package android.hardware; 19 20import android.os.Build; 21 22/** 23 * Class representing a sensor. Use {@link SensorManager#getSensorList} to get 24 * the list of available Sensors. 25 * 26 * @see SensorManager 27 * @see SensorEventListener 28 * @see SensorEvent 29 * 30 */ 31public final class Sensor { 32 33 /** 34 * A constant describing an accelerometer sensor type. 35 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 36 * for more details. 37 */ 38 public static final int TYPE_ACCELEROMETER = 1; 39 40 /** 41 * A constant string describing an accelerometer sensor type. 42 * 43 * @see #TYPE_ACCELEROMETER 44 */ 45 public static final String STRING_TYPE_ACCELEROMETER = "android.sensor.accelerometer"; 46 47 /** 48 * A constant describing a magnetic field sensor type. 49 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 50 * for more details. 51 */ 52 public static final int TYPE_MAGNETIC_FIELD = 2; 53 54 /** 55 * A constant string describing a magnetic field sensor type. 56 * 57 * @see #TYPE_MAGNETIC_FIELD 58 */ 59 public static final String STRING_TYPE_MAGNETIC_FIELD = "android.sensor.magnetic_field"; 60 61 /** 62 * A constant describing an orientation sensor type. 63 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 64 * for more details. 65 * 66 * @deprecated use {@link android.hardware.SensorManager#getOrientation 67 * SensorManager.getOrientation()} instead. 68 */ 69 @Deprecated 70 public static final int TYPE_ORIENTATION = 3; 71 72 /** 73 * A constant string describing an orientation sensor type. 74 * 75 * @see #TYPE_ORIENTATION 76 * @deprecated use {@link android.hardware.SensorManager#getOrientation 77 * SensorManager.getOrientation()} instead. 78 */ 79 @Deprecated 80 public static final String STRING_TYPE_ORIENTATION = "android.sensor.orientation"; 81 82 /** 83 * A constant describing a gyroscope sensor type. 84 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 85 * for more details. */ 86 public static final int TYPE_GYROSCOPE = 4; 87 88 /** 89 * A constant string describing a gyroscope sensor type. 90 * 91 * @see #TYPE_GYROSCOPE 92 */ 93 public static final String STRING_TYPE_GYROSCOPE = "android.sensor.gyroscope"; 94 95 /** 96 * A constant describing a light sensor type. 97 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 98 * for more details. 99 */ 100 public static final int TYPE_LIGHT = 5; 101 102 /** 103 * A constant string describing a light sensor type. 104 * 105 * @see #TYPE_LIGHT 106 */ 107 public static final String STRING_TYPE_LIGHT = "android.sensor.light"; 108 109 /** 110 * A constant describing a pressure sensor type. 111 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 112 * for more details. 113 */ 114 public static final int TYPE_PRESSURE = 6; 115 116 /** 117 * A constant string describing a pressure sensor type. 118 * 119 * @see #TYPE_PRESSURE 120 */ 121 public static final String STRING_TYPE_PRESSURE = "android.sensor.pressure"; 122 123 /** 124 * A constant describing a temperature sensor type 125 * 126 * @deprecated use 127 * {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE 128 * Sensor.TYPE_AMBIENT_TEMPERATURE} instead. 129 */ 130 @Deprecated 131 public static final int TYPE_TEMPERATURE = 7; 132 133 /** 134 * A constant string describing a temperature sensor type 135 * 136 * @see #TYPE_TEMPERATURE 137 * @deprecated use 138 * {@link android.hardware.Sensor#STRING_TYPE_AMBIENT_TEMPERATURE 139 * Sensor.STRING_TYPE_AMBIENT_TEMPERATURE} instead. 140 */ 141 @Deprecated 142 public static final String STRING_TYPE_TEMPERATURE = "android.sensor.temperature"; 143 144 /** 145 * A constant describing a proximity sensor type. This is a wake up sensor. 146 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 147 * for more details. 148 * @see #isWakeUpSensor() 149 */ 150 public static final int TYPE_PROXIMITY = 8; 151 152 /** 153 * A constant string describing a proximity sensor type. 154 * 155 * @see #TYPE_PROXIMITY 156 */ 157 public static final String STRING_TYPE_PROXIMITY = "android.sensor.proximity"; 158 159 /** 160 * A constant describing a gravity sensor type. 161 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 162 * for more details. 163 */ 164 public static final int TYPE_GRAVITY = 9; 165 166 /** 167 * A constant string describing a gravity sensor type. 168 * 169 * @see #TYPE_GRAVITY 170 */ 171 public static final String STRING_TYPE_GRAVITY = "android.sensor.gravity"; 172 173 /** 174 * A constant describing a linear acceleration sensor type. 175 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 176 * for more details. 177 */ 178 public static final int TYPE_LINEAR_ACCELERATION = 10; 179 180 /** 181 * A constant string describing a linear acceleration sensor type. 182 * 183 * @see #TYPE_LINEAR_ACCELERATION 184 */ 185 public static final String STRING_TYPE_LINEAR_ACCELERATION = 186 "android.sensor.linear_acceleration"; 187 188 /** 189 * A constant describing a rotation vector sensor type. 190 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 191 * for more details. 192 */ 193 public static final int TYPE_ROTATION_VECTOR = 11; 194 195 /** 196 * A constant string describing a rotation vector sensor type. 197 * 198 * @see #TYPE_ROTATION_VECTOR 199 */ 200 public static final String STRING_TYPE_ROTATION_VECTOR = "android.sensor.rotation_vector"; 201 202 /** 203 * A constant describing a relative humidity sensor type. 204 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 205 * for more details. 206 */ 207 public static final int TYPE_RELATIVE_HUMIDITY = 12; 208 209 /** 210 * A constant string describing a relative humidity sensor type 211 * 212 * @see #TYPE_RELATIVE_HUMIDITY 213 */ 214 public static final String STRING_TYPE_RELATIVE_HUMIDITY = "android.sensor.relative_humidity"; 215 216 /** 217 * A constant describing an ambient temperature sensor type. 218 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} 219 * for more details. 220 */ 221 public static final int TYPE_AMBIENT_TEMPERATURE = 13; 222 223 /** 224 * A constant string describing an ambient temperature sensor type. 225 * 226 * @see #TYPE_AMBIENT_TEMPERATURE 227 */ 228 public static final String STRING_TYPE_AMBIENT_TEMPERATURE = 229 "android.sensor.ambient_temperature"; 230 231 /** 232 * A constant describing an uncalibrated magnetic field sensor type. 233 * <p> 234 * Similar to {@link #TYPE_MAGNETIC_FIELD} but the hard iron calibration (device calibration 235 * due to distortions that arise from magnetized iron, steel or permanent magnets on the 236 * device) is not considered in the given sensor values. However, such hard iron bias values 237 * are returned to you separately in the result {@link android.hardware.SensorEvent#values} 238 * so you may use them for custom calibrations. 239 * <p>Also, no periodic calibration is performed 240 * (i.e. there are no discontinuities in the data stream while using this sensor) and 241 * assumptions that the magnetic field is due to the Earth's poles is avoided, but 242 * factory calibration and temperature compensation have been performed. 243 * </p> 244 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more 245 * details. 246 */ 247 public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14; 248 /** 249 * A constant string describing an uncalibrated magnetic field sensor type. 250 * 251 * @see #TYPE_MAGNETIC_FIELD_UNCALIBRATED 252 */ 253 public static final String STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED = 254 "android.sensor.magnetic_field_uncalibrated"; 255 256 /** 257 * A constant describing an uncalibrated rotation vector sensor type. 258 * <p>Identical to {@link #TYPE_ROTATION_VECTOR} except that it doesn't 259 * use the geomagnetic field. Therefore the Y axis doesn't 260 * point north, but instead to some other reference, that reference is 261 * allowed to drift by the same order of magnitude as the gyroscope 262 * drift around the Z axis. 263 * <p> 264 * In the ideal case, a phone rotated and returning to the same real-world 265 * orientation should report the same game rotation vector 266 * (without using the earth's geomagnetic field). However, the orientation 267 * may drift somewhat over time. 268 * </p> 269 * <p>See {@link android.hardware.SensorEvent#values SensorEvent.values} for more 270 * details. 271 */ 272 public static final int TYPE_GAME_ROTATION_VECTOR = 15; 273 274 /** 275 * A constant string describing an uncalibrated rotation vector sensor type. 276 * 277 * @see #TYPE_GAME_ROTATION_VECTOR 278 */ 279 public static final String STRING_TYPE_GAME_ROTATION_VECTOR = 280 "android.sensor.game_rotation_vector"; 281 282 /** 283 * A constant describing an uncalibrated gyroscope sensor type. 284 * <p>Similar to {@link #TYPE_GYROSCOPE} but no gyro-drift compensation has been performed 285 * to adjust the given sensor values. However, such gyro-drift bias values 286 * are returned to you separately in the result {@link android.hardware.SensorEvent#values} 287 * so you may use them for custom calibrations. 288 * <p>Factory calibration and temperature compensation is still applied 289 * to the rate of rotation (angular speeds). 290 * </p> 291 * <p> See {@link android.hardware.SensorEvent#values SensorEvent.values} for more 292 * details. 293 */ 294 public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16; 295 296 /** 297 * A constant string describing an uncalibrated gyroscope sensor type. 298 * 299 * @see #TYPE_GYROSCOPE_UNCALIBRATED 300 */ 301 public static final String STRING_TYPE_GYROSCOPE_UNCALIBRATED = 302 "android.sensor.gyroscope_uncalibrated"; 303 304 /** 305 * A constant describing a significant motion trigger sensor. 306 * <p> 307 * It triggers when an event occurs and then automatically disables 308 * itself. The sensor continues to operate while the device is asleep 309 * and will automatically wake the device to notify when significant 310 * motion is detected. The application does not need to hold any wake 311 * locks for this sensor to trigger. This is a wake up sensor. 312 * <p>See {@link TriggerEvent} for more details. 313 * 314 * @see #isWakeUpSensor() 315 */ 316 public static final int TYPE_SIGNIFICANT_MOTION = 17; 317 318 /** 319 * A constant string describing a significant motion trigger sensor. 320 * 321 * @see #TYPE_SIGNIFICANT_MOTION 322 */ 323 public static final String STRING_TYPE_SIGNIFICANT_MOTION = 324 "android.sensor.significant_motion"; 325 326 /** 327 * A constant describing a step detector sensor. 328 * <p> 329 * A sensor of this type triggers an event each time a step is taken by the user. The only 330 * allowed value to return is 1.0 and an event is generated for each step. Like with any other 331 * event, the timestamp indicates when the event (here the step) occurred, this corresponds to 332 * when the foot hit the ground, generating a high variation in acceleration. 333 * <p> 334 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details. 335 */ 336 public static final int TYPE_STEP_DETECTOR = 18; 337 338 /** 339 * A constant string describing a step detector sensor. 340 * 341 * @see #TYPE_STEP_DETECTOR 342 */ 343 public static final String STRING_TYPE_STEP_DETECTOR = "android.sensor.step_detector"; 344 345 /** 346 * A constant describing a step counter sensor. 347 * <p> 348 * A sensor of this type returns the number of steps taken by the user since the last reboot 349 * while activated. The value is returned as a float (with the fractional part set to zero) and 350 * is reset to zero only on a system reboot. The timestamp of the event is set to the time when 351 * the first step for that event was taken. This sensor is implemented in hardware and is 352 * expected to be low power. 353 * <p> 354 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details. 355 */ 356 public static final int TYPE_STEP_COUNTER = 19; 357 358 /** 359 * A constant string describing a step counter sensor. 360 * 361 * @see #TYPE_STEP_COUNTER 362 */ 363 public static final String STRING_TYPE_STEP_COUNTER = "android.sensor.step_counter"; 364 365 /** 366 * A constant describing a geo-magnetic rotation vector. 367 * <p> 368 * Similar to {@link #TYPE_ROTATION_VECTOR}, but using a magnetometer instead of using a 369 * gyroscope. This sensor uses lower power than the other rotation vectors, because it doesn't 370 * use the gyroscope. However, it is more noisy and will work best outdoors. 371 * <p> 372 * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details. 373 */ 374 public static final int TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20; 375 376 /** 377 * A constant string describing a geo-magnetic rotation vector. 378 * 379 * @see #TYPE_GEOMAGNETIC_ROTATION_VECTOR 380 */ 381 public static final String STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR = 382 "android.sensor.geomagnetic_rotation_vector"; 383 384 /** 385 * A constant describing a heart rate monitor. 386 * <p> 387 * A sensor that measures the heart rate in beats per minute. 388 * <p> 389 * value[0] represents the beats per minute when the measurement was taken. 390 * value[0] is 0 if the heart rate monitor could not measure the rate or the 391 * rate is 0 beat per minute. 392 */ 393 public static final int TYPE_HEART_RATE = 21; 394 395 /** 396 * A constant string describing a heart rate monitor. 397 * 398 * @see #TYPE_HEART_RATE 399 */ 400 public static final String STRING_TYPE_HEART_RATE = "android.sensor.heart_rate"; 401 402 /** 403 * A non-wake up variant of proximity sensor. 404 * 405 * @see #TYPE_PROXIMITY 406 */ 407 public static final int TYPE_NON_WAKE_UP_PROXIMITY_SENSOR = 22; 408 409 /** 410 * A constant string describing a non-wake up proximity sensor type. 411 * 412 * @see #TYPE_NON_WAKE_UP_PROXIMITY_SENSOR 413 */ 414 public static final String SENSOR_STRING_TYPE_NON_WAKE_UP_PROXIMITY_SENSOR = 415 "android.sensor.non_wake_up_proximity_sensor"; 416 417 /** 418 * A constant describing a wake up variant of accelerometer sensor type. 419 * 420 * @see #isWakeUpSensor() 421 * @see #TYPE_ACCELEROMETER 422 */ 423 public static final int TYPE_WAKE_UP_ACCELEROMETER = 23; 424 425 /** 426 * A constant string describing a wake up accelerometer. 427 * 428 * @see #TYPE_WAKE_UP_ACCELEROMETER 429 */ 430 public static final String STRING_TYPE_WAKE_UP_ACCELEROMETER = 431 "android.sensor.wake_up_accelerometer"; 432 433 /** 434 * A constant describing a wake up variant of a magnetic field sensor type. 435 * 436 * @see #isWakeUpSensor() 437 * @see #TYPE_MAGNETIC_FIELD 438 */ 439 public static final int TYPE_WAKE_UP_MAGNETIC_FIELD = 24; 440 441 /** 442 * A constant string describing a wake up magnetic field sensor. 443 * 444 * @see #TYPE_WAKE_UP_MAGNETIC_FIELD 445 */ 446 public static final String STRING_TYPE_WAKE_UP_MAGNETIC_FIELD = 447 "android.sensor.wake_up_magnetic_field"; 448 449 /** 450 * A constant describing a wake up variant of an orientation sensor type. 451 * 452 * @see #isWakeUpSensor() 453 * @see #TYPE_ORIENTATION 454 */ 455 public static final int TYPE_WAKE_UP_ORIENTATION = 25; 456 457 /** 458 * A constant string describing a wake up orientation sensor. 459 * 460 * @see #TYPE_WAKE_UP_ORIENTATION 461 */ 462 public static final String STRING_TYPE_WAKE_UP_ORIENTATION = 463 "android.sensor.wake_up_orientation"; 464 465 /** 466 * A constant describing a wake up variant of a gyroscope sensor type. 467 * 468 * @see #isWakeUpSensor() 469 * @see #TYPE_GYROSCOPE 470 */ 471 public static final int TYPE_WAKE_UP_GYROSCOPE = 26; 472 473 /** 474 * A constant string describing a wake up gyroscope sensor type. 475 * 476 * @see #TYPE_WAKE_UP_GYROSCOPE 477 */ 478 public static final String STRING_TYPE_WAKE_UP_GYROSCOPE = "android.sensor.wake_up_gyroscope"; 479 480 /** 481 * A constant describing a wake up variant of a light sensor type. 482 * 483 * @see #isWakeUpSensor() 484 * @see #TYPE_LIGHT 485 */ 486 public static final int TYPE_WAKE_UP_LIGHT = 27; 487 488 /** 489 * A constant string describing a wake up light sensor type. 490 * 491 * @see #TYPE_WAKE_UP_LIGHT 492 */ 493 public static final String STRING_TYPE_WAKE_UP_LIGHT = "android.sensor.wake_up_light"; 494 495 /** 496 * A constant describing a wake up variant of a pressure sensor type. 497 * 498 * @see #isWakeUpSensor() 499 * @see #TYPE_PRESSURE 500 */ 501 public static final int TYPE_WAKE_UP_PRESSURE = 28; 502 503 /** 504 * A constant string describing a wake up pressure sensor type. 505 * 506 * @see #TYPE_WAKE_UP_PRESSURE 507 */ 508 public static final String STRING_TYPE_WAKE_UP_PRESSURE = "android.sensor.wake_up_pressure"; 509 510 /** 511 * A constant describing a wake up variant of a gravity sensor type. 512 * 513 * @see #isWakeUpSensor() 514 * @see #TYPE_GRAVITY 515 */ 516 public static final int TYPE_WAKE_UP_GRAVITY = 29; 517 518 /** 519 * A constant string describing a wake up gravity sensor type. 520 * 521 * @see #TYPE_WAKE_UP_GRAVITY 522 */ 523 public static final String STRING_TYPE_WAKE_UP_GRAVITY = "android.sensor.wake_up_gravity"; 524 525 /** 526 * A constant describing a wake up variant of a linear acceleration sensor type. 527 * 528 * @see #isWakeUpSensor() 529 * @see #TYPE_LINEAR_ACCELERATION 530 */ 531 public static final int TYPE_WAKE_UP_LINEAR_ACCELERATION = 30; 532 533 /** 534 * A constant string describing a wake up linear acceleration sensor type. 535 * 536 * @see #TYPE_WAKE_UP_LINEAR_ACCELERATION 537 */ 538 public static final String STRING_TYPE_WAKE_UP_LINEAR_ACCELERATION = 539 "android.sensor.wake_up_linear_acceleration"; 540 541 /** 542 * A constant describing a wake up variant of a rotation vector sensor type. 543 * 544 * @see #isWakeUpSensor() 545 * @see #TYPE_ROTATION_VECTOR 546 */ 547 public static final int TYPE_WAKE_UP_ROTATION_VECTOR = 31; 548 549 /** 550 * A constant string describing a wake up rotation vector sensor type. 551 * 552 * @see #TYPE_WAKE_UP_ROTATION_VECTOR 553 */ 554 public static final String STRING_TYPE_WAKE_UP_ROTATION_VECTOR = 555 "android.sensor.wake_up_rotation_vector"; 556 557 /** 558 * A constant describing a wake up variant of a relative humidity sensor type. 559 * 560 * @see #isWakeUpSensor() 561 * @see #TYPE_RELATIVE_HUMIDITY 562 */ 563 public static final int TYPE_WAKE_UP_RELATIVE_HUMIDITY = 32; 564 565 /** 566 * A constant string describing a wake up relative humidity sensor type. 567 * 568 * @see #TYPE_WAKE_UP_RELATIVE_HUMIDITY 569 */ 570 public static final String STRING_TYPE_WAKE_UP_RELATIVE_HUMIDITY = 571 "android.sensor.wake_up_relative_humidity"; 572 573 /** 574 * A constant describing a wake up variant of an ambient temperature sensor type. 575 * 576 * @see #isWakeUpSensor() 577 * @see #TYPE_AMBIENT_TEMPERATURE 578 */ 579 public static final int TYPE_WAKE_UP_AMBIENT_TEMPERATURE = 33; 580 581 /** 582 * A constant string describing a wake up ambient temperature sensor type. 583 * 584 * @see #TYPE_WAKE_UP_AMBIENT_TEMPERATURE 585 */ 586 public static final String STRING_TYPE_WAKE_UP_AMBIENT_TEMPERATURE = 587 "android.sensor.wake_up_ambient_temperature"; 588 589 /** 590 * A constant describing a wake up variant of an uncalibrated magnetic field sensor type. 591 * 592 * @see #isWakeUpSensor() 593 * @see #TYPE_MAGNETIC_FIELD_UNCALIBRATED 594 */ 595 public static final int TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED = 34; 596 597 /** 598 * A constant string describing a wake up uncalibrated magnetic field sensor type. 599 * 600 * @see #TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED 601 */ 602 public static final String STRING_TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED = 603 "android.sensor.wake_up_magnetic_field_uncalibrated"; 604 605 /** 606 * A constant describing a wake up variant of a game rotation vector sensor type. 607 * 608 * @see #isWakeUpSensor() 609 * @see #TYPE_GAME_ROTATION_VECTOR 610 */ 611 public static final int TYPE_WAKE_UP_GAME_ROTATION_VECTOR = 35; 612 613 /** 614 * A constant string describing a wake up game rotation vector sensor type. 615 * 616 * @see #TYPE_WAKE_UP_GAME_ROTATION_VECTOR 617 */ 618 public static final String STRING_TYPE_WAKE_UP_GAME_ROTATION_VECTOR = 619 "android.sensor.wake_up_game_rotation_vector"; 620 621 /** 622 * A constant describing a wake up variant of an uncalibrated gyroscope sensor type. 623 * 624 * @see #isWakeUpSensor() 625 * @see #TYPE_GYROSCOPE_UNCALIBRATED 626 */ 627 public static final int TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED = 36; 628 629 /** 630 * A constant string describing a wake up uncalibrated gyroscope sensor type. 631 * 632 * @see #TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED 633 */ 634 public static final String STRING_TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED = 635 "android.sensor.wake_up_gyroscope_uncalibrated"; 636 637 /** 638 * A constant describing a wake up variant of a step detector sensor type. 639 * 640 * @see #isWakeUpSensor() 641 * @see #TYPE_STEP_DETECTOR 642 */ 643 public static final int TYPE_WAKE_UP_STEP_DETECTOR = 37; 644 645 /** 646 * A constant string describing a wake up step detector sensor type. 647 * 648 * @see #TYPE_WAKE_UP_STEP_DETECTOR 649 */ 650 public static final String STRING_TYPE_WAKE_UP_STEP_DETECTOR = 651 "android.sensor.wake_up_step_detector"; 652 653 /** 654 * A constant describing a wake up variant of a step counter sensor type. 655 * 656 * @see #isWakeUpSensor() 657 * @see #TYPE_STEP_COUNTER 658 */ 659 public static final int TYPE_WAKE_UP_STEP_COUNTER = 38; 660 661 /** 662 * A constant string describing a wake up step counter sensor type. 663 * 664 * @see #TYPE_WAKE_UP_STEP_COUNTER 665 */ 666 public static final String STRING_TYPE_WAKE_UP_STEP_COUNTER = 667 "android.sensor.wake_up_step_counter"; 668 669 /** 670 * A constant describing a wake up variant of a geomagnetic rotation vector sensor type. 671 * 672 * @see #isWakeUpSensor() 673 * @see #TYPE_GEOMAGNETIC_ROTATION_VECTOR 674 */ 675 public static final int TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR = 39; 676 677 /** 678 * A constant string describing a wake up geomagnetic rotation vector sensor type. 679 * 680 * @see #TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR 681 */ 682 public static final String STRING_TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR = 683 "android.sensor.wake_up_geomagnetic_rotation_vector"; 684 685 /** 686 * A constant describing a wake up variant of a heart rate sensor type. 687 * 688 * @see #isWakeUpSensor() 689 * @see #TYPE_HEART_RATE 690 */ 691 public static final int TYPE_WAKE_UP_HEART_RATE = 40; 692 693 /** 694 * A constant string describing a wake up heart rate sensor type. 695 * 696 * @see #TYPE_WAKE_UP_HEART_RATE 697 */ 698 public static final String STRING_TYPE_WAKE_UP_HEART_RATE = "android.sensor.wake_up_heart_rate"; 699 700 /** 701 * A sensor of this type generates an event each time a tilt event is detected. A tilt event 702 * is generated if the direction of the 2-seconds window average gravity changed by at 703 * least 35 degrees since the activation of the sensor. It is a wake up sensor. 704 * 705 * @see #isWakeUpSensor() 706 */ 707 public static final int TYPE_WAKE_UP_TILT_DETECTOR = 41; 708 709 /** 710 * A constant string describing a wake up tilt detector sensor type. 711 * 712 * @see #TYPE_WAKE_UP_TILT_DETECTOR 713 */ 714 public static final String SENSOR_STRING_TYPE_WAKE_UP_TILT_DETECTOR = 715 "android.sensor.wake_up_tilt_detector"; 716 717 /** 718 * A constant describing a wake gesture sensor. 719 * <p> 720 * Wake gesture sensors enable waking up the device based on a device specific motion. 721 * <p> 722 * When this sensor triggers, the device behaves as if the power button was pressed, turning the 723 * screen on. This behavior (turning on the screen when this sensor triggers) might be 724 * deactivated by the user in the device settings. Changes in settings do not impact the 725 * behavior of the sensor: only whether the framework turns the screen on when it triggers. 726 * <p> 727 * The actual gesture to be detected is not specified, and can be chosen by the manufacturer of 728 * the device. This sensor must be low power, as it is likely to be activated 24/7. 729 * Values of events created by this sensors should not be used. 730 * 731 * @see #isWakeUpSensor() 732 * @hide This sensor is expected to only be used by the power manager 733 */ 734 public static final int TYPE_WAKE_GESTURE = 42; 735 736 /** 737 * A constant string describing a wake gesture sensor. 738 * 739 * @hide This sensor is expected to only be used by the power manager 740 * @see #TYPE_WAKE_GESTURE 741 */ 742 public static final String STRING_TYPE_WAKE_GESTURE = "android.sensor.wake_gesture"; 743 744 /** 745 * A constant describing all sensor types. 746 */ 747 public static final int TYPE_ALL = -1; 748 749 /* Reporting mode constants for sensors. Each sensor will have exactly one 750 reporting mode associated with it. */ 751 // Events are reported at a constant rate. 752 static int REPORTING_MODE_CONTINUOUS = 1; 753 754 // Events are reported only when the value changes. 755 static int REPORTING_MODE_ON_CHANGE = 2; 756 757 // Upon detection of an event, the sensor deactivates itself and then sends a single event. 758 static int REPORTING_MODE_ONE_SHOT = 3; 759 760 // TODO(): The following arrays are fragile and error-prone. This needs to be refactored. 761 762 // Note: This needs to be updated, whenever a new sensor is added. 763 // Holds the reporting mode and maximum length of the values array 764 // associated with 765 // {@link SensorEvent} or {@link TriggerEvent} for the Sensor 766 private static final int[] sSensorReportingModes = { 767 0, 0, // padding because sensor types start at 1 768 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_ACCELEROMETER 769 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_GEOMAGNETIC_FIELD 770 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_ORIENTATION 771 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_GYROSCOPE 772 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_LIGHT 773 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_PRESSURE 774 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_TEMPERATURE 775 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_PROXIMITY 776 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_GRAVITY 777 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_LINEAR_ACCELERATION 778 REPORTING_MODE_CONTINUOUS, 5, // SENSOR_TYPE_ROTATION_VECTOR 779 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_RELATIVE_HUMIDITY 780 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_AMBIENT_TEMPERATURE 781 REPORTING_MODE_CONTINUOUS, 6, // SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED 782 REPORTING_MODE_CONTINUOUS, 4, // SENSOR_TYPE_GAME_ROTATION_VECTOR 783 REPORTING_MODE_CONTINUOUS, 6, // SENSOR_TYPE_GYROSCOPE_UNCALIBRATED 784 REPORTING_MODE_ONE_SHOT, 1, // SENSOR_TYPE_SIGNIFICANT_MOTION 785 // added post 4.3 786 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_STEP_DETECTOR 787 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_STEP_COUNTER 788 REPORTING_MODE_CONTINUOUS, 5, // SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR 789 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_HEART_RATE_MONITOR 790 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_NON_WAKE_UP_PROXIMITY_SENSOR 791 // wake up variants of base sensors 792 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_ACCELEROMETER 793 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_MAGNETIC_FIELD 794 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_ORIENTATION 795 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_GYROSCOPE 796 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_WAKE_UP_LIGHT 797 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_PRESSURE 798 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_GRAVITY 799 REPORTING_MODE_CONTINUOUS, 3, // SENSOR_TYPE_WAKE_UP_LINEAR_ACCELERATION 800 REPORTING_MODE_CONTINUOUS, 5, // SENSOR_TYPE_WAKE_UP_ROTATION_VECTOR 801 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_WAKE_UP_RELATIVE_HUMIDITY 802 REPORTING_MODE_ON_CHANGE, 3, // SENSOR_TYPE_WAKE_UP_AMBIENT_TEMPERATURE 803 REPORTING_MODE_CONTINUOUS, 6, // SENSOR_TYPE_WAKE_UP_MAGNETIC_FIELD_UNCALIBRATED 804 REPORTING_MODE_CONTINUOUS, 4, // SENSOR_TYPE_WAKE_UP_GAME_ROTATION_VECTOR 805 REPORTING_MODE_CONTINUOUS, 6, // SENSOR_TYPE_WAKE_UP_GYROSCOPE_UNCALIBRATED 806 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_WAKE_UP_STEP_DETECTOR 807 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_WAKE_UP_STEP_COUNTER 808 REPORTING_MODE_CONTINUOUS, 5, // SENSOR_TYPE_WAKE_UP_GEOMAGNETIC_ROTATION_VECTOR 809 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_WAKE_UP_HEART_RATE_MONITOR 810 REPORTING_MODE_ON_CHANGE, 1, // SENSOR_TYPE_WAKE_UP_TILT_DETECTOR 811 REPORTING_MODE_ONE_SHOT, 1, // SENSOR_TYPE_WAKE_GESTURE 812 }; 813 814 static int getReportingMode(Sensor sensor) { 815 int offset = sensor.mType * 2; 816 if (offset >= sSensorReportingModes.length) { 817 // we don't know about this sensor, so this is probably a 818 // vendor-defined sensor, in that case, we figure out the reporting 819 // mode from the sensor meta-data. 820 int minDelay = sensor.mMinDelay; 821 if (minDelay == 0) { 822 return REPORTING_MODE_ON_CHANGE; 823 } else if (minDelay < 0) { 824 return REPORTING_MODE_ONE_SHOT; 825 } else { 826 return REPORTING_MODE_CONTINUOUS; 827 } 828 } 829 return sSensorReportingModes[offset]; 830 } 831 832 static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) { 833 int type = sensor.mType; 834 // RotationVector length has changed to 3 to 5 for API level 18 835 // Set it to 3 for backward compatibility. 836 if (type == Sensor.TYPE_ROTATION_VECTOR && 837 sdkLevel <= Build.VERSION_CODES.JELLY_BEAN_MR1) { 838 return 3; 839 } 840 int offset = type * 2 + 1; 841 if (offset >= sSensorReportingModes.length) { 842 // we don't know about this sensor, so this is probably a 843 // vendor-defined sensor, in that case, we don't know how many value 844 // it has 845 // so we return the maximum and assume the app will know. 846 // FIXME: sensor HAL should advertise how much data is returned per 847 // sensor 848 return 16; 849 } 850 return sSensorReportingModes[offset]; 851 } 852 853 /* Some of these fields are set only by the native bindings in 854 * SensorManager. 855 */ 856 private String mName; 857 private String mVendor; 858 private int mVersion; 859 private int mHandle; 860 private int mType; 861 private float mMaxRange; 862 private float mResolution; 863 private float mPower; 864 private int mMinDelay; 865 private int mFifoReservedEventCount; 866 private int mFifoMaxEventCount; 867 private String mStringType; 868 private String mRequiredPermission; 869 private int mMaxDelay; 870 private boolean mWakeUpSensor; 871 872 Sensor() { 873 } 874 875 /** 876 * @return name string of the sensor. 877 */ 878 public String getName() { 879 return mName; 880 } 881 882 /** 883 * @return vendor string of this sensor. 884 */ 885 public String getVendor() { 886 return mVendor; 887 } 888 889 /** 890 * @return generic type of this sensor. 891 */ 892 public int getType() { 893 return mType; 894 } 895 896 /** 897 * @return version of the sensor's module. 898 */ 899 public int getVersion() { 900 return mVersion; 901 } 902 903 /** 904 * @return maximum range of the sensor in the sensor's unit. 905 */ 906 public float getMaximumRange() { 907 return mMaxRange; 908 } 909 910 /** 911 * @return resolution of the sensor in the sensor's unit. 912 */ 913 public float getResolution() { 914 return mResolution; 915 } 916 917 /** 918 * @return the power in mA used by this sensor while in use 919 */ 920 public float getPower() { 921 return mPower; 922 } 923 924 /** 925 * @return the minimum delay allowed between two events in microsecond 926 * or zero if this sensor only returns a value when the data it's measuring 927 * changes. 928 */ 929 public int getMinDelay() { 930 return mMinDelay; 931 } 932 933 /** 934 * @return Number of events reserved for this sensor in the batch mode FIFO. This gives a 935 * guarantee on the minimum number of events that can be batched. 936 */ 937 public int getFifoReservedEventCount() { 938 return mFifoReservedEventCount; 939 } 940 941 /** 942 * @return Maximum number of events of this sensor that could be batched. If this value is zero 943 * it indicates that batch mode is not supported for this sensor. If other applications 944 * registered to batched sensors, the actual number of events that can be batched might be 945 * smaller because the hardware FiFo will be partially used to batch the other sensors. 946 */ 947 public int getFifoMaxEventCount() { 948 return mFifoMaxEventCount; 949 } 950 951 /** 952 * @return The type of this sensor as a string. 953 */ 954 public String getStringType() { 955 return mStringType; 956 } 957 958 /** 959 * @hide 960 * @return The permission required to access this sensor. If empty, no permission is required. 961 */ 962 public String getRequiredPermission() { 963 return mRequiredPermission; 964 } 965 966 /** @hide */ 967 public int getHandle() { 968 return mHandle; 969 } 970 971 /** 972 * This value is defined only for continuous mode sensors. It is the delay between two 973 * sensor events corresponding to the lowest frequency that this sensor supports. When 974 * lower frequencies are requested through registerListener() the events will be generated 975 * at this frequency instead. It can be used to estimate when the batch FIFO may be full. 976 * Older devices may set this value to zero. Ignore this value in case it is negative 977 * or zero. 978 * 979 * @return The max delay for this sensor in microseconds. 980 */ 981 public int getMaxDelay() { 982 return mMaxDelay; 983 } 984 985 /** 986 * Returns whether this sensor is a wake-up sensor. 987 * <p> 988 * Wake up sensors wake the application processor up when they have events to deliver. When a 989 * wake up sensor is registered to without batching enabled, each event will wake the 990 * application processor up. 991 * <p> 992 * When a wake up sensor is registered to with batching enabled, it 993 * wakes the application processor up when maxReportingLatency has elapsed or when the hardware 994 * FIFO storing the events from wake up sensors is getting full. 995 * <p> 996 * Non-wake up sensors never wake the application processor up. Their events are only reported 997 * when the application processor is awake, for example because the application holds a wake 998 * lock, or another source woke the application processor up. 999 * <p> 1000 * When a non-wake up sensor is registered to without batching enabled, the measurements made 1001 * while the application processor is asleep might be lost and never returned. 1002 * <p> 1003 * When a non-wake up sensor is registered to with batching enabled, the measurements made while 1004 * the application processor is asleep are stored in the hardware FIFO for non-wake up sensors. 1005 * When this FIFO gets full, new events start overwriting older events. When the application 1006 * then wakes up, the latest events are returned, and some old events might be lost. The number 1007 * of events actually returned depends on the hardware FIFO size, as well as on what other 1008 * sensors are activated. If losing sensor events is not acceptable during batching, you must 1009 * use the wake-up version of the sensor. 1010 * @return true if this is a wake up sensor, false otherwise. 1011 */ 1012 public boolean isWakeUpSensor() { 1013 return mWakeUpSensor; 1014 } 1015 1016 void setRange(float max, float res) { 1017 mMaxRange = max; 1018 mResolution = res; 1019 } 1020 1021 @Override 1022 public String toString() { 1023 return "{Sensor name=\"" + mName + "\", vendor=\"" + mVendor + "\", version=" + mVersion 1024 + ", type=" + mType + ", maxRange=" + mMaxRange + ", resolution=" + mResolution 1025 + ", power=" + mPower + ", minDelay=" + mMinDelay + "}"; 1026 } 1027} 1028