OWADVL
OWADVL

Reputation: 11154

How to start a method in the same time on 2 devices

I have 2 android phones phones, both connected to the same wifi, both with bluetooth.

I want some method that syncs somehow the phones and starts a function on the same time on both phones.

For example playing a song at the same time.

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

Someone suggesting playing it in the future with 2-3 seconds, sending the time-stamp, but how do you sync the internal clocks of the devices then ?

Upvotes: 5

Views: 863

Answers (8)

badr slaoui
badr slaoui

Reputation: 1063

maybe it's not the answer you are looking for but i think that due to the high precision you are wanting , you should look for a push technology, i advice you to take look at SignalR. It's real time technology which gives you abstraction of sending methods , it have a built-in methods like Clients.All.Broadcast that fit your needs.

Upvotes: 0

Luvnish Monga
Luvnish Monga

Reputation: 7770

This thing is possible via socket, you will send a event via socket then the other device receive that event. For learn socket io chat

Upvotes: 0

Robert Rowntree
Robert Rowntree

Reputation: 6289

Use a server to provide a synchronous event to just the two clients who have decclared their mutual affinity (random as a parm and pair serializer Partner-1 or Partner-2 which they share prior to their respectve calls for the sync event).

Assume both clients on same subnet (packets from 2 events serialized on the server , arrive across the network at the 2 clients simultaneously client-side) This provides synchronous PLays by 2 , bound clients.

The event delivered by server is either a confirm to play queued selected track OR a broadcast( decoupled, more formal)

The only tricky thing is the server side algorythm implementing this:

Queue a pair of requests or error

Part1, part2 with same Random value constitute valid pair if both received before either times out.

On a valid pair schedule both to the same future event in their respective , committed responses.

OnSchedule do the actual IO for 2 paired requests. Respective packets will arrive back at respective clients at same time, each response having been subject to equal network latency

Ng if two diff carrier 4G or lte networks involved. (Oops)

Upvotes: 0

scottt
scottt

Reputation: 8371

If your WiFi router allows clients to talk to each other (many public hotspots disable this), you could implement a simple socket listener on one (or each) device and have the initiating device broadcast a message.

For more complicated things and network flexibility, I've had good success with connected sessions using AllJoin. There is a bit of a learning curve to do interesting things, but the simple stuff is pretty easy once you understand the architecture.

Upvotes: 0

David Wasser
David Wasser

Reputation: 95578

Basically use @Dula's suggestion (device 1 sends command to device 2 and gives a "start time" which lies in the future). Both devices then start the action at the same time (in the future).

To make sure that the devices are synchronized, you can use a server-based time sync (assuming that both devices have Internet access). To do this, each device contacts the same server (using NTP, or HTTP-based NTP, or contacts a known HTTP server, like www.google.com and uses the value in the "Date" header of the HTTP response). The "server-date" is compared to the system clock on the device, and the difference is the "time-offset from server-time". The time-offsets can be used to synchronize on the "server-time", which is then used as the time base for the actual action (playing the media, etc.).

Upvotes: 2

M. Reza Nasirloo
M. Reza Nasirloo

Reputation: 16614

Before calling that particular method, try to measure the latency between the two devices:

1.First device says Hi(store the current time)

2.Second device receives the Hi.

3.Second device says back Hi !!

4.First device receives the Hi.((storedTime - currentTime) / 2 )

Now you have the latency, send your request to second device to start your particular method and start it on first one after the latency.

Try to measure the latency 5 to 10 times to be more accurate.

Upvotes: 4

Mihai8
Mihai8

Reputation: 3147

You can try to use some MQTT framework to send message between two device, or into a set with more number of devices.

Upvotes: -1

Dula wnRp
Dula wnRp

Reputation: 163

you have a way to transfer data between the devices right ? if so you can send a time-stamp which is in the future, ex: if the present time stamp is 1421242326 you send 1421242329 or something and start the function at that time on both devices.

Upvotes: 3

Related Questions