Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync Time for Recording Data on Multiple Android Devices

Tags:

android

time

gps

This question may be slightly long-winded, but is open for many suggestions.


Problem Statement:
We have several API level 8 (Android 2.2) MyTouch devices that we will be using for recording acceleration data at an amusement park, namely on a roller coaster. We also have a visualization feature that allows us to plot and view the accelerometer points as a function of time that they were recorded at. Here is where we have a dilemma! The system times on the Android devices aren't all the same, nor can they be set to the same time with millisecond precision (only manually with minute precision, which is awful).


Solution Attempts:
So, I first resorted to recording data according to a GPS time found at the start of the app. Long process short: get GPS time, get System time, get difference, and upon recording a data point, get system time again and add the difference back onto the time, label that as starting time of recording, and increment by 200 milliseconds for each recorded data point from there. However, there are 2 problems (for getting GPS time):

  • Using getLastKnownLocation() isn't too accurate. Actually, it seems very inaccurate. It is giving me times that are 9 minutes off the current GMT/UTC time. Coincidentally, however, system time is also nearly 9 minutes off on the Android device... the difference between GPS time and System time is usually between 1000 and 5000 milliseconds (1 to 5 seconds). I suppose there is a chance my code is wrong. I pasted it below for you to see.

  • requestSingleUpdate() would be great to use, as it would get a more recent location and perhaps a very accurate time. Problem? Requires API level 9... devices we don't have.


Ideas:
Here's an idea I had though - what if I somehow pulled a global time from a website and used the time pulled from there as the time of recording? Problem here - I have no idea how to do that, it is just wishful thinking of mine...

Another idea - is there some sort of global getTime()-type of function that I simply don't know about?


Reasoning why I want Android's to pull time from a similar clock:
Imagine two people sitting on a roller coaster - one in the front seat, one on the back. When recording data, the person in front will obviously experience accelerations slightly before the person in the back will (and we want to see this on our visualization graph). That is why is it crucial, to millisecond precision, that these points are recorded according to a single, global time.


Code:

Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);

if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    mLocationManager.requestLocationUpdates(mLocationManager.getBestProvider(c, true), 0, 0, AmusementPark.this);
    Criteria criteria = new Criteria();
    String bestProvider = mLocationManager.getBestProvider(criteria, false);
    Location location = mLocationManager.getLastKnownLocation(bestProvider);

    try {

        gpsTime = location.getTime();
        systemTime = System.currentTimeMillis();
        timeDif = gpsTime - systemTime;
        Log.e("timedif", "GPS: " + gpsTime + ", System: " + systemTime +", Dif: " + timeDif);

    } catch (NullPointerException e){

    }

}

Update:
Here, someone suggests a sort of "server" getTime(). I really like his code/answer, but what is this "server" and/or how do I set one up, if this is the best solution?

like image 438
Mxyk Avatar asked Jun 25 '12 14:06

Mxyk


1 Answers

I think the "server" the responder is referring to in that post is to set up a dedicated time server. Basically you'd be setting up some hardware that all of the people using your app would connect to in order to get the time. It may be more effort than you are willing to put in.

You'd need the following,

  1. Some spare hardware (could be as simple as a computer running tomcat, or as complex as a dedicated server rack depending on how many people you think will be using your app.) or a subscription to a domain server/web host.

  2. Some web service that just returns the time whenever an app requests it.

  3. You'd need to make your server/webservice public, which is a whole other headache.

I'm not sure that would be your best solution either as it would depend on network connectivity to reach your dedicated server. There are a couple of alternate solutions you can try:

I don't know if your app is going to be public or used specifically for research. If it is the latter and it will be with a group of people you know and can coordinate with, I actually recommend that you just download ClockSync, sync up your phones to the same time server, and then execute your app.

If your app is going to be distributed to people you won't have contact with, then perhaps your best bet is to get your app to do a connection to one ntp server and grab the time that way. Here are a list of public ntp servers you can use: http://www.pool.ntp.org/en/

Here's some more information:

ClockSync

Sample code for making an SNTP connection

I hope that information can be of use to you in making a decision.

like image 127
Otra Avatar answered Oct 20 '22 04:10

Otra