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"; } }
public static boolean checkForNewCfg(Context context) { InterfaceDetails newCfg = getInterfaceDetails(context); if (currentCfg != null && currentCfg.equals(newCfg)) { return false; } currentCfg = newCfg; if (!newCfg.netEnabled) { Log.i(TAG, "Now assuming NO connection (all interfaces down)"); } else { if (newCfg.netType == ConnectivityManager.TYPE_WIFI) { Log.i(TAG, "Now assuming wifi connection"); } else if (newCfg.netType == ConnectivityManager.TYPE_MOBILE) { Log.i( TAG, "Now assuming 3G connection (" + (newCfg.isRoaming ? "roaming, " : "") + (newCfg.isTethered ? "tethered" : "non-tethered") + ")"); } if (!newCfg.lanMaskV4.equals("")) { Log.i(TAG, "IPv4 LAN netmask on " + newCfg.wifiName + ": " + newCfg.lanMaskV4); } if (!newCfg.lanMaskV6.equals("")) { Log.i(TAG, "IPv6 LAN netmask on " + newCfg.wifiName + ": " + newCfg.lanMaskV6); } } return true; }
private static void getTetherStatus(Context context, InterfaceDetails d) { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); Method[] wmMethods = wifi.getClass().getDeclaredMethods(); d.isTethered = false; d.tetherStatusKnown = false; for (Method method : wmMethods) { if (method.getName().equals("isWifiApEnabled")) { try { d.isTethered = ((Boolean) method.invoke(wifi)).booleanValue(); d.tetherStatusKnown = true; Log.d(TAG, "isWifiApEnabled is " + d.isTethered); } catch (Exception e) { e.printStackTrace(); } } } }
@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"); } }
private static InterfaceDetails getInterfaceDetails(Context context) { InterfaceDetails ret = new InterfaceDetails(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if (info == null || info.isConnected() == false) { return ret; } switch (info.getType()) { case ConnectivityManager.TYPE_MOBILE: case ConnectivityManager.TYPE_MOBILE_DUN: case ConnectivityManager.TYPE_MOBILE_HIPRI: case ConnectivityManager.TYPE_MOBILE_MMS: case ConnectivityManager.TYPE_MOBILE_SUPL: case ConnectivityManager.TYPE_WIMAX: ret.isRoaming = info.isRoaming(); ret.netType = ConnectivityManager.TYPE_MOBILE; ret.netEnabled = true; break; case ConnectivityManager.TYPE_WIFI: case ConnectivityManager.TYPE_BLUETOOTH: case ConnectivityManager.TYPE_ETHERNET: ret.netType = ConnectivityManager.TYPE_WIFI; ret.netEnabled = true; break; } getTetherStatus(context, ret); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) { OldInterfaceScanner.populateLanMasks(context, ITFS_WIFI, ret); } else { NewInterfaceScanner.populateLanMasks(context, ITFS_WIFI, ret); } return ret; }