hxb
2022-02-17 af2cbc2a24b3724e7fc3e6b42e134b21a3144c75
HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/utils/IpUtils.java
@@ -19,22 +19,39 @@
     * @return 广播地址
     */
    public static String getBroadcastAddress() {
//        try {
//            for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
//                 niEnum.hasMoreElements(); ) {
//                NetworkInterface ni = niEnum.nextElement();
//                if (!ni.isLoopback()) {
//                    for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
//                        if (interfaceAddress.getBroadcast() != null) {
//                            return interfaceAddress.getBroadcast().toString().substring(1);
//                        }
//                    }
//                }
//            }
//        } catch (SocketException e) {
//            e.printStackTrace();
//        }
        return "255.255.255.255";
    }
    public static boolean isLocalIpAddress(String ipAddress) {
        try {
            for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
                 niEnum.hasMoreElements(); ) {
                NetworkInterface ni = niEnum.nextElement();
                if (!ni.isLoopback()) {
                    for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                        if (interfaceAddress.getBroadcast() != null) {
                            return interfaceAddress.getBroadcast().toString().substring(1);
                        }
                for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                    if (ipAddress.equals(interfaceAddress.getAddress().getHostAddress())) {
                        return true;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "255.255.255.255";
        return false;
    }
    public static String getIP(Context application) {