@@ -81,6 +81,7 @@ public class NetUtils {
81
81
82
82
/**
83
83
* 设置dns列表
84
+ *
84
85
* @param dnsList xx.xx.xx.xx,xx.n.n.n.....
85
86
*/
86
87
public static void setDnsList (String [] dnsList ) {
@@ -406,7 +407,7 @@ public static DnsBean checkNetWork() {
406
407
DnsBean dnsBean = NetUtils .ping (dns [0 ], 1 , 1 );
407
408
if (dnsBean .isPass ) {
408
409
return dnsBean ;
409
- }else {
410
+ } else {
410
411
return checkNetWorkCallback ();
411
412
}
412
413
} else {
@@ -415,7 +416,7 @@ public static DnsBean checkNetWork() {
415
416
}
416
417
417
418
// 检查外部互联网连接是否正常
418
- public static boolean isInetAddressAvailable (int timeoutMillis ,String host ) {
419
+ public static boolean isInetAddressAvailable (int timeoutMillis , String host ) {
419
420
try {
420
421
InetAddress address = InetAddress .getByName (host );
421
422
if (address != null ) {
@@ -430,14 +431,14 @@ public static boolean isInetAddressAvailable(int timeoutMillis,String host) {
430
431
}
431
432
432
433
// 检查设备是否连接到外网
433
- public static boolean isInternetAvailable (int timeout ,String host ) {
434
- if (NetUtils .getNetWorkType ()!= NETWORK_NO ) {
434
+ public static boolean isInternetAvailable (int timeout , String host ) {
435
+ if (NetUtils .getNetWorkType () != NETWORK_NO ) {
435
436
try {
436
437
OkHttpClient client = new OkHttpClient .Builder ()
437
438
.followRedirects (true )
438
439
.followSslRedirects (true )
439
440
.connectTimeout (timeout , TimeUnit .MILLISECONDS )
440
- .readTimeout (timeout ,TimeUnit .MILLISECONDS )
441
+ .readTimeout (timeout , TimeUnit .MILLISECONDS )
441
442
.callTimeout (timeout , TimeUnit .MILLISECONDS )
442
443
.writeTimeout (timeout , TimeUnit .MILLISECONDS )
443
444
.build ();
@@ -492,7 +493,7 @@ public static DnsBean ping(String ip, int c, int w, int W) {
492
493
StringBuffer cbstr = new StringBuffer ();
493
494
try {
494
495
//过滤http:// 或 https://
495
- ip = ip .replaceFirst ("https?://" ,"" );
496
+ ip = ip .replaceFirst ("https?://" , "" );
496
497
cbstr .append ("ping" );
497
498
cbstr .append (c > 0 ? (" -c " + c ) : (" -c 1" ));
498
499
cbstr .append (w > 0 ? (" -w " + w ) : (" -w 1" ));
@@ -902,34 +903,57 @@ public static String getEthIPv4Address() {
902
903
*/
903
904
public static String getLteIpAddress () {
904
905
ConnectivityManager connectivityManager = (ConnectivityManager ) BaseIotUtils .getContext ().getSystemService (Context .CONNECTIVITY_SERVICE );
905
-
906
906
if (connectivityManager != null ) {
907
907
NetworkInfo activeNetwork = connectivityManager .getActiveNetworkInfo ();
908
-
909
- if (activeNetwork != null ) {
910
- if (activeNetwork .getType () == ConnectivityManager .TYPE_WIFI || activeNetwork .getType () == ConnectivityManager .TYPE_MOBILE ) {
911
- try {
912
- Enumeration <NetworkInterface > interfaces = NetworkInterface .getNetworkInterfaces ();
913
- while (interfaces .hasMoreElements ()) {
914
- NetworkInterface iface = interfaces .nextElement ();
915
- Enumeration <InetAddress > addresses = iface .getInetAddresses ();
916
-
917
- while (addresses .hasMoreElements ()) {
918
- InetAddress addr = addresses .nextElement ();
919
- if (!addr .isLoopbackAddress () && addr .getAddress ().length == 4 ) {
920
- return addr .getHostAddress ();
921
- }
908
+ if (activeNetwork != null && activeNetwork .getType () == ConnectivityManager .TYPE_MOBILE ) {
909
+ try {
910
+ Enumeration <NetworkInterface > interfaces = NetworkInterface .getNetworkInterfaces ();
911
+ while (interfaces .hasMoreElements ()) {
912
+ NetworkInterface iface = interfaces .nextElement ();
913
+ Enumeration <InetAddress > addresses = iface .getInetAddresses ();
914
+
915
+ while (addresses .hasMoreElements ()) {
916
+ InetAddress addr = addresses .nextElement ();
917
+ if (!addr .isLoopbackAddress () && addr .getAddress ().length == 4 ) {
918
+ return addr .getHostAddress ();
922
919
}
923
920
}
924
- } catch (Exception e ) {
925
- Log .e ("NetworkUtils" , "Error getting IP address: " + e .getMessage ());
926
921
}
922
+ } catch (Exception e ) {
923
+ Log .e ("NetworkUtils" , "Error getting IP address: " + e .getMessage ());
927
924
}
928
925
}
929
926
}
930
927
return null ;
931
928
}
932
929
930
+ /**
931
+ * 获取wifi的ipv4地址
932
+ */
933
+ public static String getWifiIpAddress () {
934
+ WifiManager wifiManager = (WifiManager ) BaseIotUtils .getContext ().getApplicationContext ().getSystemService (Context .WIFI_SERVICE );
935
+
936
+ if (wifiManager != null && wifiManager .isWifiEnabled ()) {
937
+ WifiInfo wifiInfo = wifiManager .getConnectionInfo ();
938
+ int ipAddress = wifiInfo .getIpAddress ();
939
+
940
+ // Convert the IP address to a human-readable format
941
+ String ipAddressString = formatIpAddress (ipAddress );
942
+
943
+ Log .d ("WifiUtils" , "WiFi IP Address: " + ipAddressString );
944
+ return ipAddressString ;
945
+ }
946
+
947
+ return null ;
948
+ }
949
+
950
+ private static String formatIpAddress (int ipAddress ) {
951
+ return (ipAddress & 0xFF ) + "." +
952
+ ((ipAddress >> 8 ) & 0xFF ) + "." +
953
+ ((ipAddress >> 16 ) & 0xFF ) + "." +
954
+ (ipAddress >> 24 & 0xFF );
955
+ }
956
+
933
957
/**
934
958
* sim卡ccid
935
959
*
0 commit comments