3

So, I'm trying to use an NTP server to help me synchronize an app across two Android devices (to have them perform some action at the exact same time, give or take < 20 ms).

I'm using the SNTP client code here as provided from the StackOverflow post here and it works just fine. The problem is that at the exact instant that the NTP server gets its timestamp, I need to know what time it is locally on my device (so not the time that the device received the returned timestamp, but the actual device time that corresponds to the timestamp from the NTP server). This would mean I'd have a sort of "snapshot" with two different timestamps (one device, one NTP server) that took place at the "exact" same time.

I am having a very hard time figuring this out and am starting to think it's not possible at all (which sort of makes sense). My best effort has been to try and assume that the local device time that corresponds to the time returned by the NTP server would be halfway through the roundtrip request-response time, but even the obvious error there is made even worse the longer the NTP request takes, so that's solution's simply not good enough.

How can I move forward with this on Android?

nt22
  • 31
  • 1
  • 3
  • It's impossible. In order to make it work, you would need to know exactly how long the request took to get from your computer to the timeserver. But to know that, you'd need to have the clocks on those two servers exactly synchronized. – kdgregory Jan 20 '14 at 16:01
  • But, do you really need two perfectly synchronized clocks? The whole point of NTP is to smooth out network latencies, to synchronize clocks within a few milliseconds. If you need more accuracy than that, you'll need dedicated hardware, and more important a real-time operating system. – kdgregory Jan 20 '14 at 16:02
  • Thanks @kdgregory, I was afraid of this conclusion but your reasoning makes sense. I don't actually need the clocks perfectly synchronized but I would like less than 20 (or at worst 50) millisecond accuracy as I would be having multiple devices playing a short sound clip at the same time. I'm thinking anything above 50 ms would begin to be noticeable to the human ear. – nt22 Jan 21 '14 at 00:54
  • How accurate is the [gps clock](http://stackoverflow.com/questions/4037408/how-accurate-is-the-gps-clock) in android phones? –  Jan 21 '14 at 03:48
  • SNTP is not a high-accuracy protocol. – Blrfl Jan 21 '14 at 17:07
  • @Blrfl Do you know of any high-accuracy protocols available in Android that would help me make this work? – nt22 Jan 22 '14 at 05:26
  • NTP is a possibility, but it takes time for that to settle and you need a rooted handset to change the system clock. If the two handsets are on the same network segment and can talk directly to each other, you'd probably get decent results having one send the other a message to trigger playback. – Blrfl Jan 22 '14 at 14:55
  • @MichaelT can you tell me how to get date using NTP in android? – SathishKumar Apr 04 '14 at 08:39
  • @SathishKumar no clue at all. You might try asking such a question on StackOverflow... after searching to make sure it hasn't already been asked. –  Apr 04 '14 at 19:51

0 Answers0