I've been trying to implement a feature to get the correct altitude based on the barometer sensor from Android Galaxy S5 phones. The only problem is, I don't think it is accurate. Based on http://www.whatismyelevation.com on my particular location, it shows that my altitude is around 114 meters. However, on my phone, it shows that it is 210 meters based on the barometer sensor. I am in a tall building, however, but I don't think it is 100 meters tall.
Here is my simple code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.configure_settings);
context = getApplicationContext();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensors = mSensorManager.getSensorList(Sensor.TYPE_PRESSURE);
if (sensors.size() > 0)
{
sensor = sensors.get(0);
mSensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
public void onSensorChanged(SensorEvent event)
{
float pressure = event.values[0];
altitude = String.valueOf(SensorManager.getAltitude(
SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure));
}
Thanks!
First: The barometers are very precise, but not accurate. If you place 10 Android phones next to each other on a table, you can find barometric pressure differences of up to 3 mb between devices. This is one source of error.
Second: Different groups will define 'altitude' differently, so make sure you're using the same definitions. For example, in the Location class, getAltitude is defined as
Get the altitude if available, in meters above the WGS 84 reference ellipsoid.
http://developer.android.com/reference/android/location/Location.html#getAltitude()
Third, the weather will affect the reading of the barometer by up to 40 mb. If you want to get a more accurate altitude reading from the barometer, you will have to offset from the current weather. The atmosphere can change the local pressure by up to 1-2 millibars per hour (in extreme cases)
Fourth: it is not yet possible to get a completely accurate altitude reading using the barometer in a smartphone. Nobody has solved this yet - the barometer alone is insufficient to achieve floor-level detection, for example.
I'm the developer of PressureNet, by the way - I have collected over 2 billion pressure readings from smartphones, and I see all these types of errors every day.
In closing: the reading that the barometer delivers to you requires significant interpretation before using, if you want to achieve a value for 'altitude'. Every value that is read from every barometer is 'wrong' by default; you'll have to do specific work to make it work for you, depending on what your exact needs are.