Since the latest Android update (v. 8), I realized a very strange behavior while trying to read sensors. More specifically, I am speaking about WiFi and Cell Towers. Here are two examples:
While I read WiFi access point information data and try to convert accessPoint.timestamp
to an absolute timestamp using this code:
long timeInMillis = System.currentTimeMillis() + ((accessPoint.timestamp * 1000L -
SystemClock.elapsedRealtimeNanos()) / 1000000L);
However, the same code does not work properly when I read Cell Towers using nearbyCellTowers = mTelephonyManager.getAllCellInfo();
and I have to use this other code:
long timeInMillis = System.currentTimeMillis() + ((gsmRecord.getTimeStamp() -
System.nanoTime()) / 1000000L);
If you did not notice the difference, it is in using either SystemClock.elapsedRealtimeNanos()
or System.nanoTime()
.
According to Android documentation getTimeStamp() is:
getTimeStamp(): Approximate time of this cell information in nanos since boot
A similar for WiFi:
timestamp: timestamp in microseconds (since boot) when this result was last seen.
While the descriptions seem identical (time since boot), but the values are totally different. As you can see, WiFi timestamp is comparable to the value of SystemClock.elapsedRealtimeNanos()
, while CellInfo timestamp is comparable to System.nanoTime()
.
I can never say which one would work with which of the two functions unless I debug and look at the results. Am I missing something here? Can someone clarify this for me? What is the main difference between these two functions and why the two timestamps with the same description have different values?