Android app to track magnetic feild changes using sensor
I am trying to read the magnetic field changes in an Android app using the
Magnetometer sensor and a simple magnet.
Here is the code snippet that is calculating the field strength.
@Override
public void onSensorChanged(SensorEvent event) {
float azimuth = Math.round(event.values[0]);
float pitch = Math.round(event.values[1]);
float roll = Math.round(event.values[2]);
double strength = Math.sqrt((azimuth * azimuth) + (pitch * pitch)
+ (roll * roll));
Log.i(TAG,"Magnetic Strength is "+strength);
}
The app works fine for the first few initial rounds
With the phone resting on a flat surface, When I bring the magnet closer
to the device, the app prints a value of ~200.
When the magnet is moved away from the device the app prints a value of ~40
After a few rounds of testing, the number stays fixed at ~200 and never
drops down to ~40
The pattern is something like this.
40
200
40
200
40
200
200
200
200
200
200
200
Tested the app after few device restarts and the pattern is consistant.
Wanted to know if anyone of you have observed this pattern. Not sure if
this is expected or am I missing something ?
No comments:
Post a Comment