Using UDP to Broadcast over Wifi Direct
I am new to wifi direct and I want to be able to broadcast a message because I have a graph and when I click the Mail button I want all connected devices to display this message on their timeline. I can send a peer-to-peer data file. I searched this thread and I found using UDP is a good choice, but I don't know how to implement it in wifi direct.
I found this code which uses UDP for Wi-Fi to get the broadcast address
InetAddress getBroadcastAddress() throws IOException {
WifiManager wifi = mContext.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
// handle null somehow
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
return InetAddress.getByAddress(quads);}
and this is for sending and receiving UDP broadcast packets
DatagramSocket socket = new DatagramSocket(PORT);
socket.setBroadcast(true);
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),
getBroadcastAddress(), DISCOVERY_PORT);
socket.send(packet);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
Could you please help me and explain to me how it works thanks in advance.
source to share
One solution is to multicast the packet to a multicast group. All devices connect to a multicast IP address, and the sender sends a packet to that multicast IP address. Make sure the IP address you assigned falls within the multicast IP address range. When working with multicast, the device must acquire a multicast block. Note, since multicast is UDP based, some transmission errors are expected.
AsyncTask class for devices that will receive the package:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class ReceiverMulticastAsyncTask extends AsyncTask<Void, Integer ,String > {
@Override
protected String doInBackground(Void... params) {
//Acquire the MulticastLock
WifiManager wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
//Join a Multicast Group
InetAddress address=null;
MulticastSocket clientSocket=null;
try {
clientSocket = new MulticastSocket(1212);
address = InetAddress.getByName("224.0.0.1");
clientSocket.joinGroup(address);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DatagramPacket packet=null;
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
//Receive packet and get the Data
try {
clientSocket.receive(packet);
byte[] data = packet.getData();
Log.d("DATA", data.toString()+"");
} catch (Exception e) {
e.printStackTrace();
}
multicastLock.release();
try {
clientSocket.leaveGroup(address);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clientSocket.close();
return "";
}
@Override
protected void onPostExecute(String result) {
//do whatever...
}
}
The AsyncTask class for the device that will send the packet:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class SenderMulticastAsyncTask extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... params) {
int port =1212;
DatagramSocket socket=null;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InetAddress group = null;
try {
group = InetAddress.getByName("224.0.0.1");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
socket.close();
e.printStackTrace();
}
//Sending to Multicast Group
String message_to_send ="Test";
byte[] buf = message_to_send.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
try {
socket.send(packet);
Log.d("Send", "Sending Packet");
} catch (IOException e) {
// TODO Auto-generated catch block
socket.close();
e.printStackTrace();
}
socket.close();
return "";
}
@Override
protected void onPostExecute(String result) {
//do whatever ...
}
}
source to share
In Android Wi-Fi P2P, there is the concept of "group owner", which is a device that acts as a hotspot. For the current API, the IP address of the group owner appears to be set to 192.168.49.1, which I think is hardcoded. A quick guess about the broadcast address for the multicast network would be 192.168.49.255 . For all (of several) devices I've tested so far, this turned out to be the case.
source to share