【发布时间】:2013-07-07 16:47:14
【问题描述】:
在我的应用程序中,我想广播一些 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);
}
->https://code.google.com/p/boxeeremote/wiki/AndroidUDP
这很好,但如果设备已激活热点并尝试在抛出 SocketException 后广播数据包:SocketException: sendto failed: ENETUNREACH (Network is unreachable)
如何在“提供”热点的设备上获得正确的广播地址?
我尝试过的所有单播地址都运行良好...
谢谢和问候
PS:最低 SDK 为 API 8 !
【问题讨论】:
标签: android udp wifi broadcast android-networking