How to run a method at the same time on two devices

I have 2 phones for Android phones connected to the same Wi-Fi as with Bluetooth.

I need some method that somehow synchronizes the phones and simultaneously launches the function on both phones.

For example, playing a song at the same time.

I already tried with bluetooth, but its delayed, sometimes 0.5 seconds. I want something in + - 0.01 sec, if possible.

Someone suggests playing in the future with 2-3 seconds by sending a time stamp, but how do you synchronize the device’s internal clock?

+5
source share
8 answers

Before calling this particular method, try measuring the delay between two devices:

1. The first device says Hi (save current time)

2. The second device receives Hi.

3. The second device says back Hi!

4. The first device receives Hi. ((storedTime - currentTime) / 2)

Now that you have latency, send your request to the second device to run your specific method and start it first after latency.

Try measuring latency 5 to 10 times to be more accurate.

+4
source

Do you have a way to correctly transfer data between devices? if so, you can send a timestamp, which is in the future, ex: if the current timestamp is 1421242326, you send 1421242329 or something else and run the function at that time on both devices.

+3
source

Basically use the @Dula clause (device 1 sends a command to device 2 and gives a “start time” that is in the future). Both devices then trigger the action simultaneously (in the future).

To make sure that the devices are synchronized, you can use synchronization on the server (provided that both devices have Internet access). To do this, each device communicates with the same server (using NTP or the NTP HTTP protocol), or communicates with a well-known HTTP server, for example www.google.com, and uses the value in the "Date" header of the HTTP response). The "server date" is compared with the system clock on the device, and the difference is "time offset from the server time." Time offsets can be used for synchronization in "server-time", which is then used as a temporary base for the actual action (playback of media files, etc.).

+2
source

If your WiFi router allows clients to talk to each other (many public access points prohibit this), you can implement a simple socket listener on one (or each) device and send a message to the initiating device.

For more complex things and network flexibility, I had good success with connected sessions using AllJoin . There is a little learning curve to do interesting things, but simple things are pretty easy once you understand the architecture.

0
source

Use the server to provide a synchronized event only to two clients who share their mutual proximity (random, like the pair and pair analyzer Partner-1 or Partner-2, which they share to their respect, calls to the synchronization event).

Suppose that both clients are on the same subnet (packets of two events serialized on the server arrive through the network at two clients simultaneously on the client side). This provides synchronous play on 2 connected clients.

The event sent by the server is either a confirmation for playing the selected track in the queue, or broadcast (untied, more formal)

The only tricky thing is the server side algorithm that implements this:

Queue in a couple of requests or errors

Part 1, part 2 with the same value in random order make up a real pair if both of them were received before both times ended.

In a valid pair schedule as to the same future event in their respective, perfect answers.

OnSchedule does the actual IO for two pair requests. Corresponding packets will be returned to the corresponding clients at the same time, with each response being subjected to equal network latent time

Ng if two carriers of a 4G carrier network are involved. (Unfortunately)

0
source

This thing is possible through a socket, you send an event through a socket, and another device will receive this event. To learn the io chat socket

0
source

This may not be the answer you are looking for, but I think that because of the high precision you want you should look for push technology, I advise you to take a look at SignalR. This is a real-time technology that gives you an abstraction of sending methods, it has built-in methods like Clients.All.Broadcast that suit your needs.

0
source

You can try to use some MQTT structure to send a message between two devices or in a set with a large number of devices.

-1
source

Source: https://habr.com/ru/post/1211082/


All Articles