我是wifi直接的新手,我希望能够广播一条消息,因为我有一个时间轴,当我点击发布按钮时,我希望所有连接的设备都在他们的时间线上显示该消息。我能够发送数据到对等。我搜索过这个主题,我发现使用UDP是一个不错的选择,但我不知道如何在wifi直接实现它。
我发现这个代码在wifi上使用UDP来获取广播地址
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);}
这用于发送和接收UDP广播包
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);
你可以帮助我并向我解释它是如何运作的
提前谢谢。
答案 0 :(得分:4)
在Android的Wi-Fi P2P中,存在“群组所有者”的概念,即作为接入点的设备。对于当前的API,组所有者的IP地址似乎设置为 192.168.49.1 ,我认为它在某处是硬编码的。快速猜测群组网络的广播地址将 192.168.49.255 。对于我迄今为止测试过的所有(少数)设备,事实证明是这样的。
答案 1 :(得分:1)
一种解决方案是将数据包组播到多播组。所有设备都加入组播IP,发送方将数据包发送到该组播IP。确保分配的IP属于多播IP范围。在处理多播时,设备需要获取多播锁。请注意,由于Multicast基于UDP,因此预计会发生一些传输错误。
将接收数据包的设备的AsyncTask类:
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...
}
}
将发送数据包的设备的AsyncTask类:
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 ...
}
}