Listing available sensors – an introduction to the Android Sensor Framework

Android includes support for hardware sensors using the Android Sensor Framework. The framework includes the following classes and interfaces:

  • SensorManager
  • Sensor
  • SensorEventListener
  • SensorEvent

Most Android devices include hardware sensors, but they vary greatly between different manufacturers and models. If your application utilizes sensors, you have two choices:

  • Specify the sensor in the Android Manifest
  • Check for the sensor at runtime

To specify your application uses a sensor, include the <uses-feature> declaration in the Android Manifest. Here is an example requiring a compass to be available:

<uses-feature android:name="android.hardware.sensor.compass" android:required="true"/> ...

Get Android Application Development Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.