Hardware sensors are present in almost all the modern mobile phone devices. Different types of sensors like accelerometer sensor, magnetic field sensor, orientation sensor, proximity sensor, etc. are present in these devices. Sensors that are present vary from device to device.
In Android different types of sensors are supported. SensorManagerclass is used to access these sensors. Sensor Manager is a system service running in Android. We get an instance of this service by callingContext.getSystemService() method with SENSOR_MANAGER as the argument. Android supports different types of sensors, some of them are:
Sensor.TYPE_ACCELEROMETER - accelerometer sensor
Sensor.TYPE_GYROSCOPE - gyroscope sensor
Sensor.TYPE_LIGHT - light sensor
Sensor.TYPE_MAGNETIC_FIELD - magnetic field sensor
Sensor.TYPE_ORIENTATION - orientation sensor
Sensor.TYPE_PRESSURE - pressure sensor
Sensor.TYPE_PROXIMITY - proximity sensor
Sensor.TYPE_TEMPERATURE - temperature sensor
Not all these will present in the target device, so before start using a particular type of sensor, we need to check whether the desired sensor is present in the device or not. We can check the presence using eitherSensorManager.getDefaultSensor() orServiceManager.getSensorList(). Both methods accept sensor type parameter as an argument. In case ofServiceManager.getSensorList() method we can pass the Sensor. TYPE_ALL to get all the sensors present in the system. Once we confirm the desired sensor is present in the device, we can start getting the event updates from the sensor usingSensorManager.registerListener() method. This method accepts oneSensorEventListener callback interface. This is the interface that SensorManager uses to report the calling application whenever a sensor event occurred. This callback interface has following methods:
abstract void onAccuracyChanged(Sensor sensor, int accuracy)
abstract void onSensorChanged(SensorEvent event)
The onAccuractChanged method is called when there is a change in the sensor accuracy. First parameter is the Sensor registered and second one is the accuracy value. This can be one of the following:
SensorManager.SENSOR_STATUS_ACCURACY_HIGH - high accuracy
SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM - medium accuracy
SensorManager.SENSOR_STATUS_ACCURACY_LOW - low accuracy
SensorManager.SENSOR_STATUS_UNRELIABLE - accuracy is unreliable and cannot be trusted
The onSensorChanged is called when the sensor values are changed. The only argument SensorEvent is the sensor event occurred. This class holds the sensor values, accuracy of the sensor values, Sensor itself and a timestamp at which the event occurred.
SensorEvent.values contains the sensor values. This is an array of
float values representing the sensor values. The length and content of this array depends on the type of sensor used. For example if the sensor is an accelerometer then it will be having three elements representing Azimuth angle, Pitch and Roll. Other sensors have different values. Sensor values are described in
this link.