@@ -234,6 +234,14 @@ static class SensorEventListenerImpl implements SensorEventListener {
234234 // high time constant.
235235 private static final float MAX_DEVIATION_FROM_GRAVITY = 1.5f ;
236236
237+ // Minimum acceleration considered, in m/s^2. Below this threshold sensor noise will have
238+ // significant impact on the calculations and in case of the vector (0, 0, 0) there is no
239+ // defined rotation or tilt at all. Low or zero readings can happen when space travelling
240+ // or free falling, but more commonly when shaking or getting bad readings from the sensor.
241+ // The accelerometer is turned off when not used and polling it too soon after it is
242+ // turned on may result in (0, 0, 0).
243+ private static final float MIN_ABS_ACCELERATION = 1.5f ;
244+
237245 // Actual sampling period corresponding to SensorManager.SENSOR_DELAY_NORMAL. There's no
238246 // way to get this information from SensorManager.
239247 // Note the actual period is generally 3-30ms larger than this depending on the device, but
@@ -347,6 +355,9 @@ public void onSensorChanged(SensorEvent event) {
347355 float deviation = Math .abs (magnitude - SensorManager .STANDARD_GRAVITY );
348356
349357 handleAccelerationDistrust (deviation );
358+ if (magnitude < MIN_ABS_ACCELERATION ) {
359+ return ; // Ignore tilt and orientation when (0, 0, 0) or low reading
360+ }
350361
351362 // only filter tilt when we're accelerating
352363 float alpha = 1 ;
0 commit comments