Introduction
In this section, we will discuss sensors in android SDK. This is written in query and response format.
Questions
What is a sensor?
A sensor is capable of measuring various parameters like motion, orientation and other environmental conditions like pressure, temperature. Most android devices have build-in sensors. Sensors can provide raw data with high precision and accuracy. Some sensors are also capable of detecting changes in the ambient environment near a device.
How many types of sensors are there?
There are 3 broad categories of sensors provided by android system.
- Motion Sensors
- Environmental Sensors
- Position Sensors
Motion Sensors - These measure acceleration forces and rotational forces along three axes. e.g. accelerometer, rotational sensors, gravity sensors etc.
Environmental Sensors - These measure environmental parameters like air temperature, pressure etc. e.g. barometers, photometers and thermometers.
Position Sensors - These measures physical position of a device. e.g. orientation sensors and magnetometers.
How Android SDK has provided support for sensors?
Android SDK provides sensor framework in android.hardware package.
It contains various classes like SensorManager, Sensor, SensorEvent, SensorEventListener.
SensorManager - It lets user access all the sensors of a device by giving access to Sensor Sensor service.
Sensor - This class represents a sensor.
SensorEvent - It holds information about sensor event like sensor's type, time-stamp and accuracy.
SensorEventListener - Interface for receiving sensor notifications.
How can you check whether a particular sensor exists in your phone or not?
1. Create an instance of "SensorManager" class by calling getSystemService method and passing SENSOR_SERVICE argument.
private SensorManager mSensorManager;
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
2. Call getSensorList() method of "SensorManager" class and use TYPE_ALL as parameter for retreiving all sensors and and specific constant for getting a particular type of sensor. (e.g. TYPE_GRAVITY)
List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL)
List<Sensor> gravitySensors = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY)
How your application can get actions from sensor?
For getting actions from various Sensors we need to implement 2 callback methods of SensorEventListener interface.
- onAccuracyChanged()
- onSensorChanged()
public final void onAccuracyChanged(Sensor sensor, int accuracy) - Android system provides a Sensor object and int value for accuracy. Accuracy is represented by one of the following 4 constants:
- SENSOR_STATUS_ACCURACY_LOW
- SENSOR_STATUS_ACCURACY_MEDIUM
- SENSOR_STATUS_ACCURACY_HIGH
- SENSOR_STATUS_ACCURACY_UNRELIABLE
public final void onSensorChanged(SensorEvent event) - SensorEvent contains information about the new sensor data, like the sensor that generated the data, the timestamp at which the data was generated, and the new data that the sensor recorded, even accuracy of data.
Android has a variety of devices, how can one make sure that the sensor being used by your application is present on device?
To verify sensors at runtime getDefaultSensor of SensorManager class can be used. Not null return value indicates that sensor exist on the device and a particular feature can be enabled or disabled.
private SensorManager mSensorManger = getSystemService(Context.SENSOR_SERVICE);
if(mSensorManager.getDefaultSensor()) {
} else {
}
Android has a variety of devices, how can you optimize your application?
Sensor class provides two methods getVendor() and getVersion(). By using these functions, application can be optimized for differnt manufacturer's sensor and different version of sensors.
private Sensor mSensor;
...
...
if((mSensor.getVendor().contains("Google Inc.")) &&
(mSensor.get(i).getVersion() == 2)){
...
}
What is Google Play Filter?
We can filter out devices, which doesn't have specific feature, by using uses-feature in manifest file.
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true"/>
In Google play, application will only be visible to devices that have accelerometer.