9

I'm trying to synchronize the timestamps between multiple Android devices so I can kick off a task at the same time. I figure that I can use Timer and set a Date based on System.currentTimeMillis(), but the problem seems to be that the timestamps of multiple devices are just far enough apart from each other that I'm not getting the precision I want.

I'm looking at somewhere in the range of 50-100 ms, if possible. One idea I had was to use Android Beam to send one device's timestamp to the other device and then calculate a delta, but it turns out that I have to get the timestamp before the message is actually sent and received (i.e., the longer a user waits to send, the bigger the delta will be), which makes that scheme completely fall apart.

How can I sync up two or more devices so that their internal clocks will be no more than 100 ms apart from each other? Is this even possible without using an external server that keeps time?

Any other suggestions for techniques to do something across multiple devices as close to simultaneously as possible would also be welcome.

Norman Lee
  • 389
  • 3
  • 10

4 Answers4

4

Why not use the GPS time? The time tags from GPS are pretty accurate.

You could agree on all devices on a certain time and then start it when the GPS tells you to.

Stefan
  • 157
  • 9
  • 1
    I tried using the GPS location. I'm able to get it and set the delta, and while I haven't gotten the synchronization down yet, I don't know if this is going to be viable. In my practical testing, I was having to wait sometimes upwards of a minute for a GPS signal. Not to mention, this would be one more required permission for the app. I'll keep using the GPS time method for now and see if I can work out a synchronization scheme that works, but I don't know if using GPS will be reliable in the long run. – Norman Lee Aug 20 '13 at 21:16
2

Essentially, you have two choices: the simple way or the hard way.

The simple way is to use a single external source for time signals, such NIST, another internet source, or GPS as @Stefan suggests. Internet sources use NTP, see this Java NTP client question.

The hard way is to do Clock synchronization between the devices.

Community
  • 1
  • 1
andy256
  • 2,821
  • 2
  • 13
  • 19
1

Android GPS hardware generally have only seconds precision. The three last timestamp digits of milliseconds float number are 000.

Hérico
  • 11
  • 2
0

I am struggling with the same issue right now. I tried gps time, but I cannot guarantee that the user will have gps signal indoors.

My solution was to have all devices retrieve the time from a nist server and then just add or subtract the time difference with System.currentTimeMillis()

If the devices are rooted you could try to sync the clocks.

https://play.google.com/store/apps/details?id=ru.org.amip.ClockSync&hl=en

Chrystian
  • 977
  • 3
  • 11
  • 24