Skip to content

Commit bf1c90a

Browse files
author
ctj
committed
增加wifi的ip读取
1 parent 0684324 commit bf1c90a

File tree

1 file changed

+47
-23
lines changed
  • base_iotutils/src/main/java/com/face_chtj/base_iotutils

1 file changed

+47
-23
lines changed

base_iotutils/src/main/java/com/face_chtj/base_iotutils/NetUtils.java

+47-23
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class NetUtils {
8181

8282
/**
8383
* 设置dns列表
84+
*
8485
* @param dnsList xx.xx.xx.xx,xx.n.n.n.....
8586
*/
8687
public static void setDnsList(String[] dnsList) {
@@ -406,7 +407,7 @@ public static DnsBean checkNetWork() {
406407
DnsBean dnsBean = NetUtils.ping(dns[0], 1, 1);
407408
if (dnsBean.isPass) {
408409
return dnsBean;
409-
}else{
410+
} else {
410411
return checkNetWorkCallback();
411412
}
412413
} else {
@@ -415,7 +416,7 @@ public static DnsBean checkNetWork() {
415416
}
416417

417418
// 检查外部互联网连接是否正常
418-
public static boolean isInetAddressAvailable(int timeoutMillis,String host) {
419+
public static boolean isInetAddressAvailable(int timeoutMillis, String host) {
419420
try {
420421
InetAddress address = InetAddress.getByName(host);
421422
if (address != null) {
@@ -430,14 +431,14 @@ public static boolean isInetAddressAvailable(int timeoutMillis,String host) {
430431
}
431432

432433
// 检查设备是否连接到外网
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) {
435436
try {
436437
OkHttpClient client = new OkHttpClient.Builder()
437438
.followRedirects(true)
438439
.followSslRedirects(true)
439440
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
440-
.readTimeout(timeout,TimeUnit.MILLISECONDS)
441+
.readTimeout(timeout, TimeUnit.MILLISECONDS)
441442
.callTimeout(timeout, TimeUnit.MILLISECONDS)
442443
.writeTimeout(timeout, TimeUnit.MILLISECONDS)
443444
.build();
@@ -492,7 +493,7 @@ public static DnsBean ping(String ip, int c, int w, int W) {
492493
StringBuffer cbstr = new StringBuffer();
493494
try {
494495
//过滤http:// 或 https://
495-
ip=ip.replaceFirst("https?://","");
496+
ip = ip.replaceFirst("https?://", "");
496497
cbstr.append("ping");
497498
cbstr.append(c > 0 ? (" -c " + c) : (" -c 1"));
498499
cbstr.append(w > 0 ? (" -w " + w) : (" -w 1"));
@@ -902,34 +903,57 @@ public static String getEthIPv4Address() {
902903
*/
903904
public static String getLteIpAddress() {
904905
ConnectivityManager connectivityManager = (ConnectivityManager) BaseIotUtils.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
905-
906906
if (connectivityManager != null) {
907907
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();
922919
}
923920
}
924-
} catch (Exception e) {
925-
Log.e("NetworkUtils", "Error getting IP address: " + e.getMessage());
926921
}
922+
} catch (Exception e) {
923+
Log.e("NetworkUtils", "Error getting IP address: " + e.getMessage());
927924
}
928925
}
929926
}
930927
return null;
931928
}
932929

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+
933957
/**
934958
* sim卡ccid
935959
*

0 commit comments

Comments
 (0)