time synchronization on several Android devices - android

Can I synchronize one time on several Android devices?
For example current time is 00:00 and through the timeapi (web service) we sent this time to Android1. Due to some delay we get 00:01 instead of 00:00.
We make the same on Android2 but in this time, delay = 2sec and we get 00:02.
So can we sync these time between 2 Android devices?
Maybe you suggest another way without web serivce, via bluetooth or sent several requests to web service every 5 sec to calculate average delay?
At best I have to get 00:00 time at Android1 and Android2.

If what you want is the correct time on all devices, then you usually don't need to do anything except make sure that the built-in time synchronization is not turned off, and the time synchronization will be done via the NITZ protocol.
If your carrier doesn't provide NITZ time sync, you could use NTP.
If you want to have your own, custom time, turn off the NITZ sync, and use NTP with your own custom NTP server.

Related

Repeat a function every one minute in background with flutter

I'm writing an app with flutter to communicate with a weather station. The smartphone/tablet have to send via usb a command every one minute, recieve data from the station, and then send this data in a Firebase database.
I know that a smartphone is not the best device for do this, but I'm using an old device and I don't care about battery health (maybe a raspberry could be a better solution).
I'm using usb_serial package to communicate via usb and cron package for timing the events. And all works fine.
The problem is that the app have always to run in foreground. The first solution that i tried is using two apps: (1) one that keeps the screen on (2) and another that makes the screen black to reduce the energy consumpion. This is not the best solution.
I think that a better way is to use a background execution. I made some attempts with workmanager package but the minimum frequency is 15 min, with android alarm manager package but I'm not sure that wakes up also the network connection.
Anybody can suggest me witch is the best way to achieve my goal?
Thanks.
Refer this answer https://stackoverflow.com/a/14946013/13892187
In Dart for setting a recurring function we use the Timer class

Exact time on different devices

I need two devices to have exact same time.
I want to use GPS to get time. It can be done on iOS and Android devices.
Can i be sure that they have exact same time in milliseconds?
No you cannot do that easily.
ios overrides the gps time. If a user adds one hour, and disables auto time sync,
the time is then offset. It will not work that way. If auto time sync is enabled it should work.
However you could query via internet the correct time using a time service, and do the same that NTP do. (network time protocoll). However that looks not to easy.
As an intermediate approach, I would check whether the system time relates to the network time. If yes, one can asume that auto time sync is enabled.

Running One off GCM Network Manager task on Push

In an Android app, I perform a data poll from server on push. When the device receives a push it pings the server to get the latest data.
As the user base grows, Server could potentially get 1000's of request at the time of push taking the backend down. I am looking for a good alternative so that I can spread out the server call in a given time window say next 2 hours. What is the a good way to do it?
I was looking into GCM Network Manager One-off task. One thing I am not certain is that even if I set a time-window start now with offset of 2 hours, since the device would be connected to the network when the push is received, it would trigger the server call right away defeating the purpose.
Any suggestions on what might be a good way to resolve this?
I don't think the task will execute immediately, as the GCM network manager doesn't execute only when the network is up, but tries to batch jobs together to reduce the number of wakeups and power consumption.
However, to be safe, when you create the OneoffTask, you can set an execution window. There you can set a minimum amount of time before the task will run. I suggest using a random number of seconds, e.g. between 0 and 60 to reduce the potential load on your server.

How are Android phones programmed to automatically get time from provider?

So I'm dealing with the need for highly accurate, synced times between devices. However, the user might move. I know the phone changes timezones automatically via cell tower. But how/how often does it pull the time?
I'm going to be using NTP protocol to calculate offsets, but it will be an issue if that offset is wrong. For example the app tells the phone to run instruction one at 9pm and calculates the offset so it will go off in sync with another device. An hour later the phone has moved say, 30 miles and thus is in contact with another tower and is about to fire off another instruction.
Will that NTP offset then be wrong because the phone got a slightly different time from a different tower because it pulls the time every time it connects to a new tower? Does it only pull time from the closest tower every arbitrary interval (i.e. 24 hours) unless there's a timezone change? I don't want to be making continual unnecessary NTP calls (e.g. every minute) since the app is cycle intensive and only want to make sure the offset is correct by triggering whenever the device time gets changed.

Synchronize timestamps across multiple devices

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.
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.
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.
Android GPS hardware generally have only seconds precision. The three last timestamp digits of milliseconds float number are 000.
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

Categories

Resources