i'm new to wifi direct and i want to be able to broadcast a message, because i have a timeline and when i click the Post button i want all the connected devices have that message displayed on their timeline. I am able to send data peer to peer.I have searched about this subject 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 that uses UDP on wifi 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 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.
Connecting via UDPSet the IP address of the PC's Network card to the same subnet than your Audia/Nexia units. In Audia/Nexia/DaVinci software, go to the Tools menu > Options > Network tab. In the Network Device Discovery Method section, make sure UDP Broadcast is selected.
UDP broadcast is a technique that allows sending UDP datagram from a single source to all computers in a LAN. In order to send a UDP datagram addressed to all computers in the local area network it needs to be sent to a special address called the Broadcast address.
In Android's Wi-Fi P2P there is a concept of "group owner", which is the device that acts as an access point. For the current API the group owner's IP address seems to be set to 192.168.49.1, which I think is hard-coded somewhere. A quick guess on the broadcast address for the group's network will be 192.168.49.255. For all (of a few) devices I have tested so far that turned out to be the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With