Получить список локальных IP адресов устройства
- 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()]);
- }
Проверка if (addr instanceof Inet4Address) нужна для исключения дублирования адресов (будет и IPv4 и IPv6)