I am trying to plot the data of Light sensor from my Android smartphone using GraphView library. For the Data points' x-axis value I need the time at which the sensor gives its reading. What is the best way to get the most accurate sensor event time?
Asked
Active
Viewed 633 times
0
-
Possible duplicate of [Accelerometer SensorEvent timestamp](http://stackoverflow.com/questions/5500765/accelerometer-sensorevent-timestamp) – Madhukar Hebbar Sep 15 '16 at 12:22
2 Answers
0
@Override
public void onSensorChanged(SensorEvent event) {
event.timestamp // is the timestamp of event occurence
}
event.timestamp
is in nanosecons not miliseconds so be careful

Karol Żygłowicz
- 2,442
- 2
- 26
- 35
0
You can use sensorEvent.timestamp
You will get the time in nanoseconds.
So, you can use this -
(System.nanoTime() - sensorEvent.timestamp)/Math.pow(10,6)
to log the time in milliseconds!

CodeWalker
- 2,281
- 4
- 23
- 50
-
I tried (System.nanoTime() - sensorEvent.timestamp)/1000000000L; to get time in seconds but it gives me negative numbers having 6 digits. – Marry35 Sep 15 '16 at 12:36