Sensor in Android 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).

How to implement sensor in android

We want to create an app that show the current pressure:

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:
and once we have selected ‘User feature’, we have:
We can retrieve the sensor list too and we need a bit of code.

protected void onCreate(Bundle savedInstanceState) {

// 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());

At line 7 we get the reference to the SensorManager, used to handle sensor in Android, 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) isandroid_sensor_list

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 {
public void onAccuracyChanged(Sensor sensor, int accuracy) {

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:


For example a typical application can show the pressure trend to know if the sun will shine or we will have clouds. In this post, you learnt how to read value from sensor in android and how to handle data to build an app.

Source code available @ github.


By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.