Question

I tried to get the value of air pressure from my pressure sensor and get the value of height from this. This is how I tried:

            @Override
            public void onSensorChanged(SensorEvent event) {
              // when pressure value is changed, this method will be called.
              float pressure_value = 0.0f;
              float height = 0.0f;

              // if you use this listener as listener of only one sensor (ex, Pressure), then you don't need to check sensor type.
              if( Sensor.TYPE_PRESSURE == event.sensor.getType() ) {
                pressure_value = event.values[0];
                System.out.println("PRESSURE" + pressure_value);
                height = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_value);
                System.out.println("HÄÄÄÄÄÄÄÄ" + height);
                tv1.setText("Druck: " + pressure_value);

                tv2.setText("Höhe: " + height);

              }
            }

The value I get for my place is 440m. The real value is about 490 metres. I checked that the pressure I get is already wrong. What can I do to get the exact value from the sensor??

No correct solution

OTHER TIPS

The code you have is working as expected. The pressure changes with the weather you can get a big range even at sea level e.g. say -10% +5%..

For real altitude information you need to do some calibration which means you use a calibrated value instead of SensorManager.PRESSURE_STANDARD_ATMOSPHERE in your code above.

How you do the calibration if at all really depends on what you are trying to do. one method would be to work out the calibration value at a known altitude. e.g. at a known altitude get the pressure and then change the calibration value in the call in your code till you get something close enough to the known altitude you want out. You need to be aware that the calibration value will change with the weather. How long it will be good enough for will depend on your use case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top