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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Android Sensor Tutorial: Barometer Sensor"

  1. Harsha says:

    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. ken says:

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

Leave a Reply


6 × four =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close