Home » Android » Android Core » Android Sensor Tutorial: Barometer Sensor

About Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience in JEE architecture. I'm SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. I'm an android enthusiast and i've worked for long time in the mobile development field.

Android Sensor Tutorial: Barometer Sensor

One of the most interesting topics in my opinion is how to use Sensor in Android. Nowadays our smartphone are full of sensors and we can use it to control somehow our app. The most common sensors are:

  • GPS
  • Proximity sensor
  • Light sensor
  • Temperature sensor
  • Barometer sensor
  • NFC

just to mention some of them. In this post we will explain how to obtain a list of sensor and how we can use one of them (i.e Barometer sensor). We want to create an app that show the current pressure:

android_barometer_sensor[9]

Using sensor in android

When we develop an android app and we need a specific sensor so that our app can run we have two different choices:

  • Specify it the sensor in the AndroidManifest.xml
  • Detect the sensor list and check if the one we are interested on is available

If we specify it in the AndroidManifest.xml, we simply need to add this line:

Feature[9]

and once we have selected ‘User feature’, we have:

android_feature_1[4]

We can retrieve the sensor list too and we need a bit of code.

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 // Get the reference to the sensor manager
 sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);

 // Get the list of sensor 
 List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);

 List<Map<String, String>> sensorData = new ArrayList<Map<String,String>>();

 for (Sensor sensor: sensorList) {
     Map<String, String> data = new HashMap<String, String>();
     data.put("name", sensor.getName());
     data.put("vendor", sensor.getVendor());
     sensorData.add(data);
 }
}

At line 7 we get the reference to the SensorManager, used to handle sensor, then at line 10 we get the sensor list. In this case we want to have all the sensors present in our smartphone so we use Sensor.TYPE_ALL. If we wanted just one time we can filter the list passing the type of the sensor we are looking for. For example, if we want to have all the barometer sensor we can use:

List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_PRESSURE);

Once we have the list we can simply show it using a ListView and SimpleAdapter. The result (in my smartphone) is

android_sensor_list[4]

What’s now? We can have several information from the Sensor class, for example Vendor sensor resolution, min and max  range. You have to keep in mind that sensor range can vary among different sensors. Once we have the list we can check if the smartphone supports our sensor. Now we have our sensors we want to get information from them.

Sensor Events

To get information from a sensor there’s a simple method: register a listener. First we have to select the sensor we are interested on and then register our listener. In our case, we are interesting on barometer sensor so we have:

// Look for barometer sensor
SensorManager snsMgr = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
Sensor pS = snsMgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
snsMgr.registerListener(this, pS, SensorManager.SENSOR_DELAY_UI);

At line 4 we register our listener. Notice that the last parameter represents how fast we want to get notified when the value measured by the sensor changes. There are several values but notice that a too fast notification rate can have some side effects on your apps.  To register a class as a listener we have simply implements an interface SensorEventListener for example:

public class PressActivity extends Activity implements SensorEventListener {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        pressView.setText("" + values[0]);
    }
}

At line 3 we override a method that is called when the accuracy changes.  This parameter represents the confidence level of the value we get from the sensor. The other method (the one more interesting) is onSensorChanged that is called when the value changes. In this case we simply get the first value e show it in a TextVIew. The result is shown below:

android_barometer_sensor[4]

For example a typical application can show the pressure trend to know if the sun will shy or we will have clouds.

Source code available soon.
 

Reference: Android Sensor Tutorial: Barometer Sensor from our JCG partner Francesco Azzola at the Surviving w/ Android blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

4 comments

  1. can i have the source code to get sensor data,
    if u can it will be big help for me,
    because im doing research project and i cant get sensor data properly if u can please send me those source code to my mail : harshagayan90@gmail.com
    Thanks!!!

  2. Hi, Could I have the source code? I am working on my senior project now.
    Thanks

  3. HI
    Could I have the source code? I am working on my senior project now
    if u can it will be big help for me

  4. In the original post (http://www.survivingwithandroid.com/2013/09/android-sensor-tutorial-barometer-sensor.html) you can find the link to github where you can download the source code.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


8 − six =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close