A guide to accessing android device sensors in android
In this blog, we are going to learn how to access android core hardware sensors, also the types of sensors available on android, and explore some real world use cases in it and more. Let’s begin.
Types of common sensors available in android ??
- Accelerometer
- Gyroscope
- Proximity
- Magnetometer
- Light Sensor
Accelerometer: An accelerometer sensor reports the acceleration of the device along the three sensor axes. The measured acceleration includes both the physical acceleration (change of velocity) and the gravity.
Gyroscope: A gyroscope sensor reports the rate of rotation of the device a.k.a (Rotational Vector Sensor) around the three sensor axes.
Proximity: A proximity sensor reports the distance from the sensor to the closest visible surface. Commonly used in phone app to detect ear speaker distance to display so it can be automatically turned off the screen.
Magnetometer: A magnetic field sensor reports the ambient magnetic field, as measured along the three sensor axes. Commonly used in Compass App
Note: There are other types of sensor’s that is available in android mobiles for example: Ambient temperature, barometer sensor but these sensors are vendor specific means that is less common than other mentioned sensor above some of the handsets have one and other not, When you’re relying on these types of sensors, You should check in your application at runtime is this sensor’s is available or not and determine the capabilities of each sensor.
How to find specific sensor is available or not in the device??
To identify the sensors that are on a device, you first need to get a reference to the sensor service. To do this, you create an instance of the SensorManager
class by calling the getSystemService()
method and passing in the SENSOR_SERVICE
argument. For example:
private lateinit var sensorManager: SensorManager
...
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
if (sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) != null) {
// Success! There's a ambient temp sensor.
} else {
// Failure! No ambient temp sensor.
}
If the getDefaultSensor() method returns null for the default specified sensor type that means there is a no sensor is available on the device.
To list all the sensors available in the device use getSensorList()
from SensorManager class.
val deviceSensors: List<Sensor> = sensorManager.getSensorList(Sensor.TYPE_ALL)
Implementing using Android sensor framework
The Sensor Manager class provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, and acquiring orientation information.
Accessing specific Light sensor (Environmental sensor) in android
Also here, we implemented the SensorEventListener we can use this interface to receive two callback as a sensor events to receive sensor updates like onSensorChanged and onAccuracyChanged.
And lastly the SensorEvent Which is the important class that provide the sensor data and its time-stamp and accuracy.
Position sensors
These sensors measure the physical position of a device. This category includes orientation sensors (proximity) and magnetometers.
Magnetometer implementation
In the above code example you can notice we have combined the Accelerometer and Magnetic field sensor values by usinggetRotationMatrix()
and getOrientation()
method from SensorManager to monitor the position of the device relative to the earth’s frame of reference in North pole
In a updateOrientationAngles method i also used the remapCoordinateSystem() function from SensorManager to convert our the orientation values to your application’s frame of reference ( Necessary for compass app that rotate the angle accordingly ).
Conclusion
If you want to see a real world demo application you can check my two android repositories for sensors implementation. One for Light Sensor app named: IllumiSur to detect lux values.
And another app demonstrates the Magnetometer sensor by build a real world Geo-magnetic Compass App named: MBCompass
In this short blog we have discussed different sensor types available on android and a way to access them, and the real world use cases. If you have any doubts about implementing specific sensors, feel free to contact me from here.