I am trying to develop an application that allows other Android phones to work as speakers and thus create a party like thing. The application will be similar to the group game feature provided by Samsung.
To implement this function, I decided to follow these steps -
- The host creates an access point and publishes the member name in the access point and waits for connections.
- Now the client can see the available party names in the list and select the side to connect.
- After establishing a connection between the host and the client, the host is directed to activity, which displays a list of songs. The host selects one of the songs.
- The selected composition is transferred to the client using socket programming and stored in a folder.
- After that, the host receives the current system time and adds 5 seconds to it and sends these seconds to the client. For ex - 10:35 - the current time. Hmm, we will play at 10:40
- After the host has sent the time and the client received the time, both of them create an alarm that triggers them to start playing the MP3 file using the media player.
** Problems I am facing - ** After I did this, I noticed that both Android devices have different system times, so I synchronized the system times using the ClockSync application, which uses NTP to synchronize time. I do not want my users to use a third-party application that requires root access. So how can I synchronize the clock of two Android phones? How can i solve the problem?
-
AsyncTask Class NTP .
public class offSetAsyncTask extends AsyncTask<Void,Void,Double> {
private String serverName;
private double localClockOffset;
private double destinationTimestamp;
private double roundTripDelay;
double total = 0;
Context context;
double avg;
@Override
protected Double doInBackground(Void... params) {
getAllForMe();
getAllForMe();
getAllForMe();
getAllForMe();
getAllForMe();
System.out.println("!!!!!!!" + total);
avg = total/5;
System.out.println("~~~avg. Lag: " + avg);
response.processFinish(avg);
return avg;
}
public interface AsyncResponse{
void processFinish(double offSet);
}
public AsyncResponse response = null;
public offSetAsyncTask(AsyncResponse res, String name, Context c){
response = res;
serverName = name;
context = c;
}
private void getAllForMe(){
try{
DatagramSocket socket = new DatagramSocket();
InetAddress address = InetAddress.getByName(serverName);
byte[] buf = new NtpMessage().toByteArray();
DatagramPacket packet =
new DatagramPacket(buf, buf.length, address, 123);
NtpMessage.encodeTimestamp(packet.getData(), 40,
(System.currentTimeMillis()/1000.0) + 2208988800.0);
socket.send(packet);
System.out.println("NTP request sent, waiting for response...\n");
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
destinationTimestamp =
(System.currentTimeMillis()/1000.0) + 2208988800.0;
NtpMessage msg = new NtpMessage(packet.getData());
roundTripDelay = (destinationTimestamp-msg.originateTimestamp) -
(msg.transmitTimestamp-msg.receiveTimestamp);
localClockOffset =
((msg.receiveTimestamp - msg.originateTimestamp) +
(msg.transmitTimestamp - destinationTimestamp)) / 2;
total+=localClockOffset;
System.out.println("NTP server: " + serverName);
System.out.println(msg.toString());
System.out.println("Dest. timestamp: " +
NtpMessage.timestampToString(destinationTimestamp));
System.out.println("Round-trip delay: " +
new DecimalFormat("0.00").format(roundTripDelay*1000) + " ms");
System.out.println("Local clock offset: " +
new DecimalFormat("0.00").format(localClockOffset*1000) + " ms");
socket.close();
} catch (Exception e){
e.printStackTrace();
}
}
}