当前位置: 编程技术>移动开发
android连接wifi时获取广播地址代码
来源: 互联网 发布时间:2014-10-12
本文导语: 代码如下:private InetAddress getBroadcastAddress() throws IOException { WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE); DhcpInfo myDhcpInfo = myWifiManager.getDhcpInfo(); if (myDhcpInfo == null) { System.out.println("Could not get broadcast address"); return null; } ...
代码如下:
private InetAddress getBroadcastAddress() throws IOException {
WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
DhcpInfo myDhcpInfo = myWifiManager.getDhcpInfo();
if (myDhcpInfo == null) {
System.out.println("Could not get broadcast address");
return null;
}
int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask)
| ~myDhcpInfo.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
return InetAddress.getByAddress(quads);
}