通过 Wi-Fi Direct 进行广播
2022-09-03 15:13:58
我正在考虑在多个Android设备之间通过Wi-Fi Direct连接进行广播的可能性。我创建了一个简单的消息广播应用程序来测试它是否有效,但到目前为止,我还无法广播消息。当我尝试发送数据包时,我得到一个SocketException(网络无法访问):
03-20 13:23:00.148: E/UdpBroadcaster(4180): sendto failed: ENETUNREACH (Network is unreachable)
03-20 13:23:00.148: E/UdpBroadcaster(4180): java.net.SocketException: sendto failed: ENETUNREACH (Network is unreachable)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:496)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at libcore.io.IoBridge.sendto(IoBridge.java:465)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at java.net.DatagramSocket.send(DatagramSocket.java:307)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at com.example.android.wifidirect.UdpBroadcaster.sendMessage(UdpBroadcaster.java:59)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at com.example.android.wifidirect.UdpBroadcaster.run(UdpBroadcaster.java:44)
03-20 13:23:00.148: E/UdpBroadcaster(4180): Caused by: libcore.io.ErrnoException: sendto failed: ENETUNREACH (Network is unreachable)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at libcore.io.Posix.sendtoBytes(Native Method)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at libcore.io.Posix.sendto(Posix.java:146)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
03-20 13:23:00.148: E/UdpBroadcaster(4180): at libcore.io.IoBridge.sendto(IoBridge.java:463)
03-20 13:23:00.148: E/UdpBroadcaster(4180): ... 4 more
这是我的代码的本质:
InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255");
int port = 8888;
DatagramSocket socket = new DatagramSocket(port);
socket.setBroadcast(true);
socket.connect(broadcastAddress, port);
String message = "Hello";
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, broadcastAddress, port);
try {
socket.send(packet); // <----- Causes a SocketException
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
这篇文章表明,通过Wi-Fi Direct进行广播应该是可能的。
有谁知道在Android设备上通过Wi-Fi Direct广播是否真的有效?如果它应该工作,我做错了什么?
我开始认为设备不知道将广播数据包路由到哪里。在我的情况下,它需要工作,而不必根设备并手动为广播数据包添加路由。
更新
在使用Romain Hippeau建议的功能后,SocketException消失了,似乎广播正在按预期工作。但是,我在第二台设备上接收广播时遇到问题。getBroadcastAddress()
我使用以下代码接收广播:
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8888);
socket.setBroadcast(true); // Not needed?
socket.setSoTimeout(200);
DatagramPacket packet = null;
while (!mStopping) {
byte[] buffer = new byte[1024];
packet = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(packet);
if (packet.getData().length > 0) {
String receivedString = new String(packet.getData());
Log.i(TAG, "Received string: " + receivedString);
}
} catch (InterruptedIOException e) { /* Ignore */ }
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
} finally {
if (socket != null)
socket.close();
}
我还尝试通过添加 as 参数向 添加通配符地址,但没有运气。DatagramSocket
InetAddress.getByName("0.0.0.0")
建议?