public static void populateLanMasks(Context context, String[] names, InterfaceDetails ret) { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); if (dhcp != null) { ret.lanMaskV4 = intToDottedQuad(dhcp.ipAddress) + "/" + intToDottedQuad(dhcp.netmask); ret.wifiName = "UNKNOWN"; } }
@TargetApi(Build.VERSION_CODES.GINGERBREAD) public static void populateLanMasks(Context context, String[] names, InterfaceDetails ret) { try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface intf = en.nextElement(); boolean match = false; if (!intf.isUp() || intf.isLoopback()) { continue; } for (String pattern : ITFS_WIFI) { if (intf.getName().startsWith(truncAfter(pattern, "\\+"))) { match = true; break; } } if (!match) continue; ret.wifiName = intf.getName(); Iterator<InterfaceAddress> addrList = intf.getInterfaceAddresses().iterator(); while (addrList.hasNext()) { InterfaceAddress addr = addrList.next(); InetAddress ip = addr.getAddress(); String mask = truncAfter(ip.getHostAddress(), "%") + "/" + addr.getNetworkPrefixLength(); if (ip instanceof Inet4Address) { ret.lanMaskV4 = mask; } else if (ip instanceof Inet6Address) { ret.lanMaskV6 = mask; } } } } catch (SocketException e) { Log.e(TAG, "Error fetching network interface list"); } catch (Exception e) { Log.e(TAG, "Error fetching network interface list"); } }