Sam
Sam

Reputation: 42417

Sensors report bad data after device is shaken

Steps to reproduce

  1. Register a listener for the gravity sensor with normal sampling period. Either use your own code (basic example below) or just install My Sensors, making sure to set the sensor update rate to Normal via the menu.

    Sensor gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
    sensorManager.registerListener(callback, gravitySensor, SENSOR_DELAY_NORMAL);
    
  2. Observe the sensor output. The Y axis (event.values[1]) should report ~9.7 when the device is upright.

  3. Give the device a hard, quick shake/swing once or twice. Then hold it upright again.
  4. Look at the sensor output after doing this:
    • For the first 2 seconds, the Y axis value transitions from 9.7 to -9.7.
    • For the next 6 seconds, the Y axis value remains inverted (-9.7).
    • For the next 2 seconds, the Y axis value transitions back to 9.7.

This look like a bug to me because it seems completely counter-intuitive to have bad data for 10 seconds every time the user shakes or swings the device.

Device details

My questions

  1. Is this normal behaviour for this device? (Does it happen on your Galaxy S5?)
  2. Is this normal behaviour for Android devices in general? (Does it happen on your Android device(s) with the above sensor types?)
  3. Is there a way to prevent this from happening?

Upvotes: 0

Views: 69

Answers (1)

Sam
Sam

Reputation: 42417

Partial workaround: use a fast sampling rate

The problem no longer occurs when I set the sensor sampling rate to any of the following:

  • SENSOR_DELAY_GAME
  • SENSOR_DELAY_FASTEST
  • 54999 or lower

Unfortunately, if the sensor is already being listened to by another app, the sampling rate that was requested by the other app is used instead of the value requested by my app. So if another app is already listening to the sensor with SENSOR_DELAY_NORMAL, for example, my app only gets sensor updates at that rate, leaving my app completely unable to workaround the bug.

Upvotes: 1

Related Questions