Click here to Skip to main content
15,867,488 members
Articles / Mobile Apps / Android
Tip/Trick

Sensors in Android

Rate me:
Please Sign up or sign in to vote.
4.48/5 (13 votes)
14 Jun 2015CPOL3 min read 21.8K   23   6
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)


Written By
Software Developer (Senior) Fiserv Inc.
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sid_Joshi17-Jun-15 1:36
professionalSid_Joshi17-Jun-15 1:36 
GeneralRe: My vote of 5 Pin
Sid_Joshi17-Jun-15 3:17
professionalSid_Joshi17-Jun-15 3:17 
GeneralRe: My vote of 5 Pin
Deepali Dhingra25-Jun-15 14:47
professionalDeepali Dhingra25-Jun-15 14:47 
GeneralRe: My vote of 5 Pin
Sid_Joshi25-Jun-15 20:05
professionalSid_Joshi25-Jun-15 20:05 
AnswerGood one, but has a typo Pin
Afzaal Ahmad Zeeshan15-Jun-15 12:54
professionalAfzaal Ahmad Zeeshan15-Jun-15 12:54 
GeneralRe: Good one, but has a typo Pin
Deepali Dhingra16-Jun-15 17:15
professionalDeepali Dhingra16-Jun-15 17:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.