android连接wifi时获取广播地址代码
程序员文章站
2023-12-14 18:22:46
复制代码 代码如下:private inetaddress getbroadcastaddress() throws ioexception { wifimanager m...
复制代码 代码如下:
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);
}