How to get barometric altitude in Android?

╄→гoц情女王★ 提交于 2019-12-01 08:42:35

You need to use the pressure sensor to measure the pressure then use the SensorManager.getAltitude(float, float) call to convert the measures pressure along with a reference sea level pressure to get the altitude.

The tricky bit is what to use as the reference pressure. This will change with the weather and will drift over time. Typically you would do some sort of calibration to set this values and possibly update it. It depends on your use case what the right solution is.

If properly calibrated pressure based altitude is considerably more accurate then gps based altitude especially if want you are interested in is altitude differences rather than absolute altitudes. With a bit of filtering on the raw pressure sensor values you can easily detect holding the phone at face level or arms length above your head which you want do with gps.

The arguments are: GPS altitude (it's good to use average value calculated from array of measurements) and barometric pressure. Method returns pressure at sea level (formula from SensorManager.getAltitude(float p0, float p) in reverse).

public static float getSealevelPressure (float alt, float p)
{
    float p0 = (float) (p / Math.pow(1 - (alt/44330.0f), 5.255f));
    return p0;
}

I'm using the default value: SensorManager.PRESSURE_STANDARD_ATMOSPHERE until GPS collects 10 locks. After that, the above method is called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!