Skip to content

Instantly share code, notes, and snippets.

@iewnait
Created March 21, 2012 19:30
Show Gist options
  • Save iewnait/2151740 to your computer and use it in GitHub Desktop.
Save iewnait/2151740 to your computer and use it in GitHub Desktop.
Activity Class for MagnetometerDemo App on WIMM ONE
public class MagnetometerDemoActivity extends LauncherActivity implements SensorEventListener {
public static final String TAG = MagnetometerDemoActivity.class.getSimpleName();
/**
* We could use Math.toDegrees(), but we can take away division from
* calculations by just multiplying by this static value instead.
*/
public static float TO_DEGREES = (1 / (float) Math.PI) * 180;
/*
* If the magnetometer reports values over a certain value the user is
* either near a strong magnet, we need to prompt the user to move away
* or re-calibrate the device.
*/
private static final float MAGNETIC_INTERFERENCE_THRESHOLD = SensorManager.MAGNETIC_FIELD_EARTH_MAX * 2.5f;
private static final float RECALIBRATION_THRESHOLD = MAGNETIC_INTERFERENCE_THRESHOLD * 4;
/*
* We will track the last time we displayed the Interference view and only
* show it again after this time interval.
*/
private static final int MS_BETWEEN_INTERFERENCE = 1000;
private CompassView mCompassView;
private TextView mTextView;
private static SensorManager mSensorManager;
private Sensor mMagneticFieldSensor;
private Sensor mAccelerometerSensor;
private PowerManager.WakeLock mWakeLock;
// Rotation matrix required for our Orientation calculations
private float mRotationMatrix[] = new float[9];
// Used to store device orientation about 3 axis.
private float mOrientation[] = new float[3];
private float[] mMagneticFieldVals;
private float[] mAccelerationVals;
private long mLastInterferenceTime;
private boolean mRecalibrationNeeded;
private boolean mShowCalibrationText = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* Obtain a wakelock so that we can prevent the watchface from obscuring
* our activity while the user is reading the compass.
*/
final PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MagnetometerDemoWakeLock");
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagneticFieldSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mTextView = (TextView)findViewById(R.id.textView);
mCompassView = (CompassView)findViewById(R.id.compassView);
}
//Rest of the code goes here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment