Android Sensor

The Android classifies sensors in three categories:-

  • Motion Sensors:- These are used to measure acceleration forces and rotational forces along with three axes.
  • Environmental sensors:– These are used to measure the environmental changes such as temperature, humidity etc.
  • Position sensors:-These are used to measure the physical position of a device.

Android Sensor API

Some important classes and interfaces of sensor API are

  • SensorManager class
  • Sensor class
  • SensorEvent class
  • SensorEventListener interface

SensorManager class

It provides various methods for

  • to get sensor instance
  • to access and list sensors
  • to register and unregister sensor listeners etc

You can get the instance of SensorManager by calling the method getSystemService() and passing the SENSOR_SERVICE constant in it.

Sensor class

It provides methods to get information of the sensor such as

  • sensor name
  • sensor type
  • sensor resolution
  • sensor type etc.

To get Sensor object of Light sensor

To get Sensor object of all avaliable sensor of the device

Getting the list of supported sensors

SensorEvent class

Its instance is created by the system. It provides information about the sensor.

SensorEventListener interface

It provides two call back methods to get information when sensor values change or sensor accuracy changes.

 

Github for complete code

 

Do follow official tutorial

 

Posted in Android.