Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Sensor Timestamp reference time

I'm reading timestamp values from SensorEvent data but I can't work out the reference time for these values. Android documentation just says "The time in nanosecond at which the event happened" As an example:

My current Android device date, October 14th 2011 23:29:56.421 (GMT+2)

System.currentTimeMillis * 1000000 (nanosec) = 1318627796431000000 (that's ok)

sensorevent.timestamp (nanosec) = 67578436328000 = 19 hours 46 min ????

May you help me?

thanks

like image 206
Pablo A. Martínez Avatar asked Oct 14 '11 21:10

Pablo A. Martínez


People also ask

What is sensor timestamp?

The official SensorEvent#timestamp documentation states: [event. time is] The time in nanoseconds at which the event happened. For a given sensor, each new sensor event should be monotonically increasing using the same time base as SystemClock.

What is SensorEvent?

android.hardware.SensorEvent. This class represents a Sensor event and holds information such as the sensor's type, the time-stamp, accuracy and of course the sensor's data .

How do you use SensorEventListener?

Sensor listener. Once you acquired a sensor, you can register a SensorEventListener object on it. This listener will get informed, if the sensor data changes. To avoid the unnecessary usage of battery power, you can register your listener in the onResume() method and de-register it in the onPause() method.


2 Answers

It appears that what you are dealing with is the number of nanoseconds since the operating system started, also known as "uptime".

Further info on the issue: http://code.google.com/p/android/issues/detail?id=7981

I should add that the linked question SensorEvent.timestamp to absolute (utc) timestamp? deals with the same issue and is where I found the answer.

like image 110
manu3d Avatar answered Sep 30 '22 16:09

manu3d


I know that it's a very old question, but, I'm also struggling for converting SensorEvent.timestamp to a human readable time. So I'm writing here what I've understood so far and how I'm converting it in order to get better solutions from you guys. Any comments will be welcomed.

As I understood, SensorEvent.timestamp is an elapsed time since the device's boot-up. So I have to know the uptime of the device. So if there is an API returning device's boot-up, it will be very easy, but, I haven't found it. So I'm using SystemClock.elapsedRealtime() and System.currentTimeMillis() to 'estimate' a device's uptime. This is my code.

private long mUptimeMillis; // member variable of the activity or service
...
atComponentsStartUp...() {
    ...
    /* Call elapsedRealtime() and currentTimeMillis() in a row 
       in order to minimize the time gap */
    long elapsedRealtime = SystemClock.elapsedRealtime();
    long currentTimeMillis = System.currentTimeMillis();

    /* Get an uptime. It assume that elapsedRealtime() and
       currentTimeMillis() are called at the exact same time. 
       Actually they don't, but, ignore the gap 
       because it is not a significant value.
       (On my device, it's less than 1 ms)   */
    mUptimeMillis = (currentTimeMillis - elapsedRealtime); 
    ....
}
...
public void onSensorChanged(SensorEvent event) {
    ...
    eventTimeMillis = ((event.timestamp / 1000000) + mUptimeMillis);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(eventTimeMillis);
    ...
}

I think this works for Apps that a millisecond time error is okey. Please, leave your ideas.

like image 24
Youngchai Kim Avatar answered Sep 30 '22 18:09

Youngchai Kim