I am trying to get time from the internet and do not want want to use the device time. I am doing the following sntp client request for time, but as expected the time received is not accurate due to request - response time. How do I overcome the latency and get a maximum variation of +/- 20ms??
Long time=0;
while (true) {
SntpClient client = new SntpClient();
if (client.requestTime("time-a.nist.gov",6000)) {
long now = client.getNtpTime() + SystemClock.elapsedRealtime()
- client.getNtpTimeReference();
Log.d("Time Difference", time-now+"");
time=now;
Thread.sleep(5000);
}
}
The log file is as follows:
- 02-10 23:27:16.125: E/Time Difference(15566): -5804
- 02-10 23:27:21.449: E/Time Difference(15566): -5272
- 02-10 23:27:26.774: E/Time Difference(15566): -5358
- 02-10 23:27:32.206: E/Time Difference(15566): -5365
- 02-10 23:27:37.528: E/Time Difference(15566): -5328
- 02-10 23:27:43.056: E/Time Difference(15566): -5429
- 02-10 23:27:48.892: E/Time Difference(15566): -5694
- 02-10 23:27:54.730: E/Time Difference(15566): -5972
- 02-10 23:28:00.563: E/Time Difference(15566): -5679
- 02-10 23:28:06.096: E/Time Difference(15566): -5686
The time difference is expected to be constant 5000 at perfect execution.