Use barometric pressure sensor to calculate altitude on Android platform

Source: Internet
Author: User

Air pressure sensors have been used by handset makers for their devices two years ago, but they don't seem to be getting enough attention from developers. Like Galaxy S III, Galaxy Note 2 and Xiaomi 2 phones, but everyone is unfamiliar with barometric pressure sensors. In fact, atmospheric pressure is everywhere, and we can use it to accomplish tasks such as altitude measurements or even spatial positioning.

First of all, to alleviate the fatigue of work and study. Some days ago with friends to outdoor activities, climbing to the hillside, suddenly a friend asked me, now about how much altitude? My brain is a flash, some days ago a friend working in Millet on their Xiaomi 2 mobile phone to open the compass application said to me now how much altitude, and I now have a Millet 4 mobile phone, hurriedly take out to open the compass application, the results of air pressure is out, the altitude calculation needs to network, really let people disappointed. It is this point that has made me think of my own calculations.

If you say so, do it, it should not be too difficult anyway. First, the online query of relevant data, calculate their own altitude usually there are two methods, one is through the GPS Global Positioning system, the second is to measure the atmospheric pressure, and then calculate the altitude according to the barometric value.

Using GPS Global Positioning System to get an elevation simple is simple, as long as get to Android system to provide Loctionmanager service, set provider to Locationmanager.gps_provider, Write a GPS status listener Gpsstatus.listener, finally in the implementation of a locationlistener to real-time monitoring position changes, in the onlocationchanged callback function to remove the location parameter, which contains the altitude value (Getaltitude ()). The key code is as follows

1 PrivateLocationmanager LM;2LM =(Locationmanager) Context.getsystemservice (context.location_service);3 if(!lm.isproviderenabled (Locationmanager.gps_provider)) {4LOG.I (TAG, "Please turn on GPS first");5Showalertdialog (Context, "open GPS, positioning more accurate; Cancel uses network positioning, rough positioning");6 }7 if(!lm.isproviderenabled (Locationmanager.gps_provider)) {8Intent Intent =NewIntent (settings.action_location_source_settings);   9 context.startactivity (Intent); Ten } One Lm.addgpsstatuslistener (listener); ALm.requestlocationupdates (Locationmanager.gps_provider, 1, Locationlistener);

where listener and Locationlistener are Statuslistener and Locationlistener, respectively, the use is not difficult, the main code framework has been generated for you, just check the Android SDK API will understand , I won't say it. This way to measure elevation requires receiving a GPS signal. If you are in a jungle or cave or building, you may not receive a GPS satellite signal. At this point, consider using a pressure sensor to measure the altitude.

First of all, the principle of using a pressure sensor to measure altitude is actually very simple, some basic principles you must understand: Generally speaking, the higher the altitude, the lower the air pressure, there is a relationship between them, the following will talk about. Many scholars have studied altitude and air pressure, and have returned to a lot of altitude and air pressure data, their relationship of change probably obey the following expressions.

Because we want to calculate the altitude (a), so slightly change, you can know the formula of A is as follows.

where P is the current atmospheric pressure, P0 is the standard atmospheric pressure. This allows us to get an initial estimate of the altitude by acquiring the value of the built-in barometric pressure sensor on the Android phone. Implementation is not difficult, the key code is as follows.

1 PrivateSensormanager Sensormanager =NULL;2Sensormanager =(Sensormanager) Getsystemservice (context.sensor_service);3Mpressure =sensormanager.getdefaultsensor (sensor.type_pressure);4 if(Mpressure = =NULL)5 {6Mpressureval.settext ("Your phone does not support barometric sensors and cannot use the software features."));7     return;8 }9Maccelerate = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);

Then register the barometric pressure sensor in the onresume () function, write it off in OnPause (), and do so in order to save electricity.

Sensormanager.registerlistener (Pressurelistener, mpressure,         sensormanager.sensor_delay_normal);

if (pressurelistener!=null) {    sensormanager.unregisterlistener (pressurelistener);}    

There's a point where you have to get the value of the barometric sensor. There must be a Sensoreventlistener object (the Pressurelistener of the Sensormanager registration statement), the code is simple.

1Sensoreventlistener Pressurelistener =NewSensoreventlistener () {2     3 @Override4      Public voidonsensorchanged (Sensorevent event) {5         //TODO auto-generated Method Stub6         floatSPV = Event.values[0];7 Mpressureval.settext (string.valueof (SPV));8DecimalFormat DF =NewDecimalFormat ("0.00");9 Df.getroundingmode ();Ten         //Calculate Altitude One         DoubleHeight = 44330000* (Math.pow (double.parsedouble (Df.format (SPV))/1013.25), A(float) 1.0/5255.0))); - Maltitude.settext (Df.format (height)); -     } the      - @Override -      Public voidOnaccuracychanged (sensor sensor,intaccuracy) { -         //TODO auto-generated Method Stub +          -     } +};
View Code

The realization of altitude calculation formula is not a difficult task, it is basically a translation.

In fact, the altitude and pressure of this relationship is more complex, affected by a variety of factors, the most significant is the effect of temperature. Atmospheric pressure in general is the atmosphere on other objects, and the density of air molecules and kinetic energy related, so the same altitude, the higher the temperature, the higher the pressure. It is precisely because of this influence factor that makes the barometric altitude have the obvious error. If it is a short time, that is, the temperature change is not small, the height difference is more accurate, I have done experiments, the use of air pressure can accurately measure the height of 7 stories, haha, is not feeling very sensitive? Only the error caused by the influence factors can be eliminated. Just contact Android soon, also did not write any blog, this is purely rip, no technical difficulty, just to summarize the Android development of some things, by the way to practice lianbi only.

Use barometric pressure sensor to calculate altitude on Android platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.