Getting magnetic field results as degrees

by Jeffrey » Sun, 13 Dec 2009 10:13:53 GMT

Sponsored Links
 I'm working on an application that will randomly point an arrow in a
direction, and have that arrow maintain it's direction if the device
is moved. All I want to do it get the magnetic field readings as
degrees and I can do it from there. The problem I'm having is getting
the magnetic field readings. I can't find any tutorials on it and the
API demo on google's dev site uses deprecated code (figures, google's
sample code is never n00b friendly).

At this point I've got this together but I don't know what I'm
missing, all the examples I can find are using SensorListener which
has onSensorChanged(int sensor, float[] values) but
SensorEventListener does not support "float[] values"

What am I missing here?


Getting magnetic field results as degrees

by Jeffrey » Sun, 13 Dec 2009 10:32:14 GMT

 Okay, so I got a little further, I'm now stuck at the point where I
can pull values, but they are the micro-tesla measurements. How do I
get degrees from this?


Sponsored Links

Getting magnetic field results as degrees

by Peli » Sun, 13 Dec 2009 21:08:26 GMT

 You need basic trigonometry: 

if x and y denote the magnetic field strength in microtesla, then you
get the angle through the arc tangent function:
phi = Math.atan(y/x);
or phi = Math.atan2(y,x);
phi is then the angle in radians. Note that you may want to convert
phi from radians to degree (using phideg = Math.toDegree(phi);)

Note that this only works if your phone lies flat on the desk. In
arbitrary orientation, you need more sophisticated conversion that
includes x, y, and z and the accelerometer values.



Getting magnetic field results as degrees

by Jeffrey » Mon, 14 Dec 2009 04:43:32 GMT

 THANK YOU! I could NOT find any info on this anywhere! You have just
made me very happy!


Getting magnetic field results as degrees

by Ethan Rublee » Mon, 14 Dec 2009 05:34:25 GMT

 ere's my code for capturing orientation data... This is a snippet so
has not been compiled, but should give you and idea.
Keep in mind that you should register the sensors with something like
this when ever you want to start listening to sensors:

sensorMgr = (SensorManager) contex.getSystemService
orientationSensor = sensorMgr.getDefaultSensor
magnetSensor = sensorMgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
accelSensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

//this class implements SensorEventListener
orientationsupported = sensorMgr.registerListener(this, accelSensor,

//*************The following is the static data necessary for my
sensorlistener, these are fields of my class
// for storing sensor data
float[] mags = new float[3];
float[] accels = new float[3];

int matrix_size = 16;
// matrices for letting SensorManager do its magic
float[] RotationMatrix = new float[matrix_size];
float[] InclinationMatrix = new float[matrix_size];

// an output matrix, that will hold a rotation matrix that
// can be used in openGl as a modelview matrix
float[] outR = new float[matrix_size];

// the orientation rotation array
float[] values = new float[3];

// here's the meat of the sensor handling, from the
android.hardware.SensorListener interface
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;

int type = sensor.getType();

switch (type) {

mags[0] = event.values[0];
mags[1] = event.values[1];
mags[2] = event.values[2];
accels[0] = event.values[0];
accels[1] = event.values[1];
accels[2] = event.values[2];
*these are the orientation values in degrees - one of them is the
* magnetic heading....
// values = event.values.clone();


//this is key to getting your heading, it fills out the matrices
which are needed to calculate the
//heading. It is important to not that the acceleration data is
linked to the magnetic data, in that the
//physical tilt/yaw of the phone affects the mags vector. Atleast
this is my understanding.
SensorManager.getRotationMatrix(RotationMatrix, InclinationMatrix,
accels, mags);

// this is only necessary for my AR opengl purposes
SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, outR);

// This is the orientation that i need
// values[0] = compass in radians
// values[1] and values[2] are the rotations about the x and y axis
SensorManager.getOrientation(outR, values);

// I have not used this function but it may give you the magnetic
// heading
// directly, in radians of course
float magHeading = SensorManager.getInclination(InclinationMatrix);


Check out
for definitive documentation.
I would put a text field in your activity and output the values live
as the sensors

Getting magnetic field results as degrees

by Jeffrey » Tue, 15 Dec 2009 08:39:50 GMT

  got it working, the phone will only need to be lying flat, so that
made it a lot easier. It seems like google should mention somewhere
that you need to use trig to get degree's from the compass... That
doesn't quite seem like general knowledge. Though I had a problem with
the activity force closing occasionally but I fixed that so that it
wouldn't try and take sensor readings until the activity had been open
for 1 second. It also adds to the suspense of what direction the drift
will be :)

On Dec 13, 12:47am, Ethan Rublee <> wrote:


Other Threads

1. Dynamically creating Views when using AsyncTask

Hi guys

I am extending AsyncTask to push some database operations off to a different
thread to the
UI. I can successfully update Views that have been previously defined in XML
by setting them
as member variables of my Activity. This way I have access to them from my
inner AsyncTask

However, I also need to dynamically create views and don't have access to
the App context in my
extended AsyncTask. Where one is dynamically creating views,  (TableRows for
example representing
results from a DB)  what is the best way to do this in a separate thread to
the UI ?


2. ADP1 radio issues: RADIO_OFF and GENERIC_ERRORs immediately following reboot

Dear all,

As we lack an ADP1-specific group, I hope you'll forgive me posting
here instead.  I'm a summer student working on an Android project for
a professor at my university, and so I purchased an ADP1, which I
hoped would also double as my personal cell phone.  However, try as I
might, I cannot get the radio to stop behaving strangely.

Following a full factory reset, after enabling roaming and inputting
my APN information (as I am on Fido in Canada), if I'm lucky, the
radio will stay on long enough for me to authenticate my phone with
Google or make a quick voice call, then it will shut off.  Following
this, the radio will remain turned off at all times.  A logcat -b
radio reveals the following:

As you can see, the radio state gets changed to RADIO_OFF
immediately.  This occurs both with the factory default 1.1, the HTC's
official Cupcake build, JesusFreke's community build, and my own build
from source.  In all cases, I have flashed the device with the correct
radio driver (from HTC's site).  Two other professors affiliated with
this project have working ADP1s (I used precisely the same steps as
them when they registered their phones with Fido's network), and I
have confirmed that my SIM card works without flaws in their phones.

Has anyone encountered issues like this?  Should I be looking into an
RMA for the phone?

Nathan Taylor


3. Install issue w/non-market app OTA install to T-Mobile G1鈥

4. Install NP Plugin on Android's browser

5. Accessing Resources

6. How can I know whether a table exist?

7. Sending "BACK" key to the application through instrumentation