Created
March 21, 2012 19:30
-
-
Save iewnait/2151740 to your computer and use it in GitHub Desktop.
Activity Class for MagnetometerDemo App on WIMM ONE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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