This code snippet basically collects an array of Local Addresses (Inet4Address).
We use instaceof Inet4Address to avoid duplicates with IPv4 ang IPv6.
We use instaceof Inet4Address to avoid duplicates with IPv4 ang IPv6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static Inet4Address[] getLocalAddrasses(ConnectivityManager connManager) throws SocketException { ArrayList<Inet4Address> results = new ArrayList<Inet4Address>(1); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); if (!iface.isUp() || iface.isLoopback() || !iface.supportsMulticast()) { continue; } Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) { results.add((Inet4Address) addr); } } } return results.toArray(new Inet4Address[results.size()]); } |