Android: Getting the Broadcast Address of your Wifi connection
Sunday, May 1st, 2011
If you ever need to know the broadcast address of your wifi connection…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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); } |







