Synchronize the time between an android app and a server

2k Views Asked by At

I use a raspberry pi with a Bluetooth dongle to simulate a beacon. I want to measure the time needed for a mobile app to detect the UUID of a beacon when I change it in the raspberry. With this code I found out the server used by the smartphone to synchronize the tile

final Resources res = this.getResources();
        final int id = Resources.getSystem().getIdentifier(
                "config_ntpServer", "string","android");
        final String defaultServer = res.getString(id); 

Than I synchronized the time in the raspberry pi with

sudo ntpdate -u 2.android.pool.ntp.org

Before I change the id of the beacon I print the time

timestamp() {
  date +"%T,%3N"
}


timestamp # print timestamp

sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1.....

Then I compare the time when I changed the UUID and the time in the logcat when the UUID was seen for the first time and the result is alwayse negative UUID changed at 15:33:03,276 and detected at 15:33:02.301.

Is this a synchronization problem? Is there a better way to do this?

1

There are 1 best solutions below

0
On BEST ANSWER

A few thoughts:

  • Both the Android device and the Pi by default will sync their time to a NTP server automatically if they have a network connection. You should not have to do anything.

  • The NTP daemon doesn't always change the clock immediately -- it adjusts it slowly over time so as not to upset linux processes by an immediate jump. Since the Raspberry Pi has no real time clock, it always has incorrect time at boot. You may need to wait minutes after boot before it is in sync with the Android device.

  • NTP is not perfect. Don't expect to get your clocks synchronized to more than a few 10s of milliseconds when using internet time servers. Since bluetooth detection times can be very fast (also in the range of 10s of milliseconds), if you are getting detection times of -100 ms, this may be within the limits of this setup.

What you are showing is a detection time of about -1.0 second. This suggests that the time is not synchronized well. I would suspect the Pi is the problem and troubleshoot there. It might be helpful to show the time to the millisecond on both devices side by side to troubleshoot.