Home » Android » Magnetic Field Sensor calibration on ANDROID

Magnetic Field Sensor calibration on ANDROID

Posted by: admin May 14, 2020 Leave a comment


I’m making an application that works as a compass..

I’m using the accelerometer and the magnetic field sensors to compute the azimuth angle through, sensor.getOrientation().

I’m searching for something that can improve the magnetic field sensor accuracy, since I’m getting it state of accuracy as UNRELIABLE!

Any one knows anything about this?I’m looking for something that can be either hardcoded or for instance just physically moving the phone until it gets calibrated!

How to&Answers:

This is not a final answer (I don’t know anything for sure), but my understanding from online posts is that waving the phone around in a figure of 8 a few times while the compass is in use is supposed to trigger automatic recalibration. This is what the google maps app suggests, for example. I don’t know whether this is dependent on application functionality (something in maps that detects the waving by accelerometer and triggers a recalibration), or something in the android stack, or something specific to per-phone implementations. Try it and see!

Eg discussion: http://androidforums.com/epic-4g-support-troubleshooting/217317-cant-get-compass-calibrate.html

This reference appears to suggest this per-axis / figure-8 rotation process is built-in functionality: http://m.eclipsim.com/gpsstatus/

And here another article that claims this is built-in functionality, and that you don’t even need to be running a compass-consuming app for the recalibration to work: http://www.ichimusai.org/2009/06/20/how-to-calibrate-the-htc-magic-compass/


Just a few points

The figure 8 motion works sometimes and not others, I have no idea why, they really need to have some kind of code based way to check if the 8 motion worked (Assuming that the physical motion is actually required)

They also need a way to detect that calibration is required, I looked at the code for the accuracy output (the unreliable constant) and once they send it to you they will not send it again, so for instance if you calibrate but then come within a strong magnetic field it will not resend (not sure why they did that)

One not completely reliable way to detect ongoing issues is that you can also use the magnetic sensor output and do something like field=sqrt(x*x+y*y+z*z) and check that field falls between say 25 and 65 and then ask the user to calibrate if it does not.

The bottom line after testing 18 phones is that I would never depend on a Android based compass with the current crop of phones, accuracy would always be in question.

I have also found even if you are lucky and have a fairly reliable phone you can never be sure that it’s calibrated without checking it against a real compass, which kind of defeats the purpose.

NOTE: On a lot of the mis-behaving phones we have found that the sensor writes a calibration file and a tmp file with the same name. If you delete those files and re-boot the phones the calibration file is recreated with zero’d values and the cold start and general calibration problems resolve themselves.

The bad news is that they are stored in /data/misc and require root privileges to get at (thanks Google & Sensor mfg!) so even though I suspect this would solve a lot of problems for a lot of developers it just is not viable from a marketplace app perspective.


I am developing for Android. I’m using Titanium Alloy as development tool with the Titanium Geolocation module.

I have only tested 2 devices [Galaxy Note and S4] against a commercial magnetic compass. Following a calibration process [tilt along the 3 axis] and using 2 different compass apps and the app I’m working on, the Android compass seems accurate enough for basic use … correlation was good enough for my purpose anyway. I also found the device compass reading to be very sensitive to other magnetic and electrical field interference … initial mistake I made was to use the compass feature whilst device was in a device protector with a magnetic closure facility [quite common on tabs] … this interference is particularly strong. I thus need to suggest to users of my app to remove device protectors, keep device free of other electronics and then do standard calibration before initializing the app.


Another option is:

Go To sensors menu: #*0#*

Then if you see a red line in Magnetic Sensor section and a Need for Calibration you should recalibrate your compass.


According those guys;

Turn the Samsung Galaxy Mini S5 around all of its axes until the red
line in the black circle changes color from red to blue. You can also
run through a motion that follows the shape of an 8. It may be that
several attempts are needed to calibrate the compass…