562935844@qq.com
2023-08-31 fdcf461fbfa3bcd650685743e891ad3357898f0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.hdl.sdk.common.utils;
 
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
 
import com.hdl.sdk.connect.config.HDLLinkConfig;
 
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
 
/**
 * Created by Tong on 2021/9/27.
 */
public class IpUtils {
 
//    /**
//     * @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";
    }*/
 
 
    /**
     * @return 广播地址
     */
    public static synchronized String getBroadcastAddress() {
        try {
            if (!TextUtils.isEmpty(HDLLinkConfig.getInstance().getNetworkName())) {
                for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
                     niEnum.hasMoreElements(); ) {
                    NetworkInterface ni = niEnum.nextElement();
                    if (!ni.isLoopback() && ni.getName().equalsIgnoreCase(HDLLinkConfig.getInstance().getNetworkName())) {
                        for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                            if (interfaceAddress.getBroadcast() != null) {
                                Log.d("TAG", "----->ni.getName()= " + ni.getName() + " ip= " + interfaceAddress.getBroadcast().toString().substring(1));
                                return interfaceAddress.getBroadcast().toString().substring(1);
                            }
                        }
                    }
                }
 
                for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
                     niEnum.hasMoreElements(); ) {
                    NetworkInterface ni = niEnum.nextElement();
                    if (!ni.isLoopback()) {
                        for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                            if (interfaceAddress.getBroadcast() != null) {
                                Log.d("TAG", "----->ip= " + interfaceAddress.getBroadcast().toString().substring(1));
                                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();
                for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                    if (ipAddress.equals(interfaceAddress.getAddress().getHostAddress())) {
                        return true;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return false;
    }
 
    public static String getIP(Context application) {
        WifiManager wifiManager = (WifiManager) application.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            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()) {
                            return inetAddress.getHostAddress();
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        } else {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            return intToIp(ipAddress);
        }
        return null;
    }
 
 
    private static String intToIp(int i) {
 
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }
 
    private static String getWifiManagerAddress(Context context) {
        try {
            if (context != null) {
                WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                if (wifiManager != null && wifiManager.isWifiEnabled()) {
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    int ipAddress = wifiInfo.getIpAddress();
                    return intToIp(ipAddress);
                }
            }
        } catch (Exception ignored) {
        }
        return null;
    }
 
    public static String getWifiIpV4Address(Context context) {
        try {
            //直接从wifiManager获取
            String managerAddress = getWifiManagerAddress(context);
            if (!TextUtils.isEmpty(managerAddress)) {
                return managerAddress;
            }
 
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                NetworkInterface network = enumeration.nextElement();
                if (network == null || network.isVirtual() || !network.isUp() || network.isLoopback() || !network.getName().equalsIgnoreCase("eth0")) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = network.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (!address.isLoopbackAddress() && !address.isAnyLocalAddress()
                                && !address.isLinkLocalAddress()) {
                            String hostAddress = address.getHostAddress();
                            if (hostAddress != null) {
                                boolean isIPv4 = hostAddress.indexOf(':') < 0;
                                if (isIPv4) return hostAddress;
                            }
 
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static String getEth0IpV4Address() {
        try {
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                NetworkInterface network = enumeration.nextElement();
                if (network == null || network.isVirtual() || !network.isUp() || network.isLoopback() || !network.getName().equalsIgnoreCase("eth0")) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = network.getInetAddresses();
 
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (!address.isLoopbackAddress() && !address.isAnyLocalAddress()
                                && !address.isLinkLocalAddress()) {
                            String hostAddress = address.getHostAddress();
                            if (hostAddress != null) {
                                boolean isIPv4 = hostAddress.indexOf(':') < 0;
                                if (isIPv4) return hostAddress;
                            }
 
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    /**
     * @return 获取全部有效ip地址, 优先返回有线ip
     */
    public static String getIpV4Address(Context context) {
 
        try {
            String eth0IpV4Address = getEth0IpV4Address();
            if (!TextUtils.isEmpty(eth0IpV4Address)) {
                return eth0IpV4Address;
            }
            String wifiIpV4Address = getWifiIpV4Address(context);
            if (!TextUtils.isEmpty(wifiIpV4Address)) {
                return wifiIpV4Address;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
}