Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Sensors in Android

4.48/5 (13 votes)
14 Jun 2015CPOL3 min read 22.6K  
This contains information about Sensors in Android.

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.

  1. Motion Sensors
  2. Environmental Sensors
  3. 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.

Java
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)

Java
List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL)
Java
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.

  1. onAccuracyChanged()
  2. 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:

  1. SENSOR_STATUS_ACCURACY_LOW
  2. SENSOR_STATUS_ACCURACY_MEDIUM
  3. SENSOR_STATUS_ACCURACY_HIGH
  4. 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.

Java
private SensorManager mSensorManger = getSystemService(Context.SENSOR_SERVICE);
if(mSensorManager.getDefaultSensor()) {
   //Sensor exists - Do something........
} else {
   //Sensor does not exist - Disable feature.
}

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.

Java
private Sensor mSensor;
...
...
if((mSensor.getVendor().contains("Google Inc.")) &&
       (mSensor.get(i).getVersion() == 2)){
      // Use the version 2 gravity sensor.
      ...
    }

What is Google Play Filter?

We can filter out devices, which doesn't have specific feature, by using uses-feature in manifest file.

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

In Google play, application will only be visible to devices that have accelerometer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)