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.
Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

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


− 1 = zero



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books