gpt4 book ai didi

java - 通过 Wi-Fi Direct 广播

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:07 29 4
gpt4 key购买 nike

我正在研究在多个 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);
}

This post建议可以通过 Wi-Fi Direct 进行广播。

有谁知道在 Android 设备上通过 Wi-Fi Direct 广播是否真的有效?如果它应该有效,我做错了什么?

我开始认为设备不知道将广播数据包路由到哪里。在我的例子中,它需要在不需要 root 设备和手动添加广播数据包路由的情况下工作。


更新

在使用 Romain Hippeau 建议的 getBroadcastAddress() 函数后,SocketException 消失了,广播似乎按预期工作。但是,我在第二台设备上接收广播时遇到问题。

我正在使用以下代码接收广播:

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();
}

我还尝试通过添加 InetAddress.getByName("0.0.0.0") 作为参数向 DatagramSocket 添加通配符地址,但没有成功。

建议?

最佳答案

无耻地盗自https://code.google.com/p/boxeeremote/wiki/AndroidUDP

尝试以这种方式获取网络连接:

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);
}

然后尝试以这种方式发送数据包:

DatagramSocket socket = new DatagramSocket(PORT);
socket.setBroadcast(true);
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),
getBroadcastAddress(), PORT);
socket.send(packet);

// If you want to listen for a response ...
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

编辑:从同一页阅读试试这个...

WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = wifi.createMulticastLock("dk.aboaya.pingpong");
lock.acquire();
serverSocket = new DatagramSocket(19876);
serverSocket.setSoTimeout(15000); //15 sec wait for the client to connect
byte[] data = new byte[UDPBatPositionUpdater.secretWord.length()];
DatagramPacket packet = new DatagramPacket(data, data.length);
serverSocket.receive(packet);
lock.release();
String s = new String(packet.getData());
System.out.println(s);

请记住,您需要以下权限才能运行:
<使用权限 android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

关于java - 通过 Wi-Fi Direct 广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524593/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com