androidnoob
androidnoob

Reputation: 355

OnSensorChanged called even when there is No Change in Sensor Readings

I am using sensor as Sensor.TYPE_ACCELEROMETER. I am implementing the OnSensorChanged() method as follows:

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;
        float[] vector;
        vector = event.values.clone();
        double normVector =
                Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
        vector[0] = vector[0] / (float) normVector;
        vector[1] = vector[1] / (float) normVector;
        vector[2] = vector[2] / (float) normVector;
        angle = (int) Math.round(Math.toDegrees(Math.acos(vector[2])));
       // do something with angle
   }

However, this method gets called even when my device is lying flat on the table and I see a stable value (7) for angle. Why is this so? Is there any way I can make sure that this method is called only when the reading of angle is changed?

Upvotes: 2

Views: 450

Answers (1)

Roy Falk
Roy Falk

Reputation: 1741

Straight from the documentation:

A sensor of this type measures the acceleration applied to the device (Ad). Conceptually, it does so by measuring forces applied to the sensor itself (Fs) using the relation: Ad = - ∑Fs / mass

In particular, the force of gravity is always influencing the measured acceleration: Ad = -g - ∑F / mass

For this reason, when the device is sitting on a table (and obviously not accelerating), the accelerometer reads a magnitude of g = 9.81 m/s^2

Similarly, when the device is in free-fall and therefore dangerously accelerating towards to ground at 9.81 m/s^2, its accelerometer reads a magnitude of 0 m/s^2.

Upvotes: 1

Related Questions