android 中获取所有有效网卡和对应的IP地址

在开发应用时,经常要获取网络的所有网卡的IP地址,这样可以选择绑定哪个网卡。

代码如下:

获取所有有效的网卡:

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;

public static String[] getAllNetInterface() {
        ArrayList availableInterface = new ArrayList<>();
        String [] interfaces = null;
        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();
            InetAddress ia = null;
            while (nis.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nis.nextElement();
                Enumeration ias = ni.getInetAddresses();
                while (ias.hasMoreElements()) {
                    ia = ias.nextElement();
                  if (ia instanceof Inet6Address) {
                      continue;// skip ipv6
                  }

                    String ip = ia.getHostAddress();
                    Log.d(TAG,"getAllNetInterface,available interface:"+ni.getName()+",address:"+ip);
                    // 过滤掉127段的ip地址
                    if (!"127.0.0.1".equals(ip)) {
                        availableInterface.add(ni.getName());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        Log.d(TAG,"all interface:"+availableInterface.toString());
        int size = availableInterface.size();
        if (size > 0) {
            interfaces = new String[size];
            for(int i = 0; i < size; i++) {
                interfaces[i] = availableInterface.get(i);
            }
        }
        return interfaces;
    }

获取指定网卡的IP地址:

/**
     * Get Ip address 自动获取IP地址
     *
     * @throws SocketException
     */
    public static String getIpAddress(String netInterface) throws SocketException {
        String hostIp = null;
        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();
            InetAddress ia = null;
            while (nis.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nis.nextElement();
                //Log.d(TAG,"getIpAddress,interface:"+ni.getName());
                if (ni.getName().equals(netInterface)) {
                    Enumeration ias = ni.getInetAddresses();
                    while (ias.hasMoreElements()) {
                        ia = ias.nextElement();
                        if (ia instanceof Inet6Address) {
                            continue;// skip ipv6
                        }
                        String ip = ia.getHostAddress();
                        // 过滤掉127段的ip地址
                        if (!"127.0.0.1".equals(ip)) {
                            hostIp = ia.getHostAddress();
                            break;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        Log.d(TAG,"getIpAddress,interface:"+netInterface+",ip:"+hostIp);
        return hostIp;
    }

 

你可能感兴趣的:(android)