Here is the *correct* (until proven otherwise) version of a compass for a
device lying flat on a table:
package com.magnet_test;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
public class Magnet_Listener implements SensorEventListener {
private float[] magneticFieldValues;
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
magneticFieldValues = sensorEvent.values;
float x = magneticFieldValues[0];
float y = magneticFieldValues[1];
float angle = (float) Math.atan2(y, x);
float angle_in_degrees = (float) Math.toDegrees(angle) + 180;
System.out.println("angle = " + angle_in_degrees);
}
}
*Inspired by code from Reto Meier.*
On Thursday, September 27, 2012 10:33:31 AM UTC-5, bob wrote:
>
> So, I'm trying to learn how to make an android compass.
>
> Naturally, I used a site called google and typed the following in:
>
> "making an android compass"
>
> I found myself at this URL:
>
>
> http://android-er.blogspot.com/2010/08/simple-compass-sensormanager-and.html
>
> After looking thru it, I'm thinking it's incorrect.
>
> There is this suspiciously simple method:
>
> public void onSensorChanged(SensorEvent event) {
> // TODO Auto-generated method stub
> myCompassView.updateDirection((float)event.values[0]);
> }
>
>
>
> Basically, it seems like they are using the x component of the force
> vector as if it were an angle. Can someone else look at this and let me
> know what you think?
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en