Wow, ok this is surely a bug!
The timestamp of each SensorEvent is being overwritten as if it were a static variable...
When I record a string of the timestamp when the event occurs, all values are different.
The events are stored in an array unchanged.
Every SensorEvent in the array now has the same timestamp, but the values arrays are still different (i.e., they're not the same object and contain different information... EXCEPT for the timestamp).
Google/HTC, please return 3 hours of my life!
I'll go file a bug report, unless anyone can explain this behaviour. It's certainly not documented in the API.
In the meantime, try out this solution:
import android.hardware.Sensor;
import android.hardware.SensorEvent;
public class UnbrokenSensorEvent {
public long timestamp;
public float[] values;
public Sensor sensor;
public UnbrokenSensorEvent(SensorEvent event){
this.timestamp = event.timestamp;
this.values = event.values;
this.sensor = event.sensor;
}
}
Then do something like this in your listener:
ArrayList<UnbrokenSensorEvent> results = new ArrayList<UnbrokenSensorEvent>();
public void onSensorChanged(SensorEvent event) {
results.add(new UnbrokenSensorEvent(event));
}
The refactoring should be quite easy, since SensorEvent and UnbrokenSensorEvent have the same public fields. If you need to use other SensorEvent functionality, just go ahead and chuck it into the Unbroken version.
It's hacky, but IMHO a quick hack is always better than waiting for an API to be updated!