1. 程式人生 > >Android IPV6獲取方式

Android IPV6獲取方式

下面的程式碼 中我做了校驗真實的ipv6 獲取ipv6去除%後面的值然後通過冒號校驗位數,然後擷取第一段判斷是否包括fe 或者fc包括就表示假的

 //ipv6
    public static String getLocalIpV6() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    // logger.error("ip1       " + inetAddress);
                    logger.error("ip1  " + inetAddress.getHostAddress());
                 /*   logger.error("getHostName  " + inetAddress.getHostName());
                    logger.error("getCanonicalHostName  " + inetAddress.getCanonicalHostName());
                    logger.error("getAddress  " + Arrays.toString(inetAddress.getAddress()));
                    logger.error("getHostAddress  " + inetAddress.getHostAddress());*/

                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {
                        return inetAddress.getHostAddress();
                    }


                }
            }
        } catch (Exception ex) {
            Log.e("IP Address", ex.toString());
        }
        return null;
    }
    public static String getlocalIp() {
        String ip;

        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
//                           ip=inetAddress.getHostAddress().toString();
                        System.out.println("ip==========" + inetAddress.getHostAddress());
                        return inetAddress.getHostAddress();

                    }
                }
            }
        } catch (SocketException ignored) {

        }
        return null;
    }

    public static String validateV6() {
        getLocalHostIpv6();
        new Thread(new Runnable() {
            @Override
            public void run() {
                hostIp6 = getLocalIpV6();
            }
        }).start();

        //過濾找到真實的ipv6地址
        logger.error("v6 validateV6 " + hostIp6);
        if (hostIp6 != null && hostIp6.contains("%")) {
            String[] split = hostIp6.split("%");
            String s1 = split[0];
            logger.error("v6 remove % is " + s1);

            if (s1 != null && s1.contains(":")) {
                String[] split1 = s1.split(":");
                if (split1.length == 6||split1.length==8) {
                    if (split1[0].contains("fe") || split1[0].contains("fc")) {
                        return "0.0.0.0";
                    } else {
                        return s1;
                    }
                }
            }
        }
        return "0.0.0.0";
    }