From bf1c90a1db93231525aa6749432bd3192f153bad Mon Sep 17 00:00:00 2001 From: ctj Date: Fri, 17 Nov 2023 11:11:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0wifi=E7=9A=84ip=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/face_chtj/base_iotutils/NetUtils.java | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/base_iotutils/src/main/java/com/face_chtj/base_iotutils/NetUtils.java b/base_iotutils/src/main/java/com/face_chtj/base_iotutils/NetUtils.java index c685c3f..3285875 100644 --- a/base_iotutils/src/main/java/com/face_chtj/base_iotutils/NetUtils.java +++ b/base_iotutils/src/main/java/com/face_chtj/base_iotutils/NetUtils.java @@ -81,6 +81,7 @@ public class NetUtils { /** * 设置dns列表 + * * @param dnsList xx.xx.xx.xx,xx.n.n.n..... */ public static void setDnsList(String[] dnsList) { @@ -406,7 +407,7 @@ public static DnsBean checkNetWork() { DnsBean dnsBean = NetUtils.ping(dns[0], 1, 1); if (dnsBean.isPass) { return dnsBean; - }else{ + } else { return checkNetWorkCallback(); } } else { @@ -415,7 +416,7 @@ public static DnsBean checkNetWork() { } // 检查外部互联网连接是否正常 - public static boolean isInetAddressAvailable(int timeoutMillis,String host) { + public static boolean isInetAddressAvailable(int timeoutMillis, String host) { try { InetAddress address = InetAddress.getByName(host); if (address != null) { @@ -430,14 +431,14 @@ public static boolean isInetAddressAvailable(int timeoutMillis,String host) { } // 检查设备是否连接到外网 - public static boolean isInternetAvailable(int timeout,String host) { - if (NetUtils.getNetWorkType()!=NETWORK_NO) { + public static boolean isInternetAvailable(int timeout, String host) { + if (NetUtils.getNetWorkType() != NETWORK_NO) { try { OkHttpClient client = new OkHttpClient.Builder() .followRedirects(true) .followSslRedirects(true) .connectTimeout(timeout, TimeUnit.MILLISECONDS) - .readTimeout(timeout,TimeUnit.MILLISECONDS) + .readTimeout(timeout, TimeUnit.MILLISECONDS) .callTimeout(timeout, TimeUnit.MILLISECONDS) .writeTimeout(timeout, TimeUnit.MILLISECONDS) .build(); @@ -492,7 +493,7 @@ public static DnsBean ping(String ip, int c, int w, int W) { StringBuffer cbstr = new StringBuffer(); try { //过滤http:// 或 https:// - ip=ip.replaceFirst("https?://",""); + ip = ip.replaceFirst("https?://", ""); cbstr.append("ping"); cbstr.append(c > 0 ? (" -c " + c) : (" -c 1")); cbstr.append(w > 0 ? (" -w " + w) : (" -w 1")); @@ -902,34 +903,57 @@ public static String getEthIPv4Address() { */ public static String getLteIpAddress() { ConnectivityManager connectivityManager = (ConnectivityManager) BaseIotUtils.getContext().getSystemService(Context.CONNECTIVITY_SERVICE); - if (connectivityManager != null) { NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); - - if (activeNetwork != null) { - if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - while (interfaces.hasMoreElements()) { - NetworkInterface iface = interfaces.nextElement(); - Enumeration addresses = iface.getInetAddresses(); - - while (addresses.hasMoreElements()) { - InetAddress addr = addresses.nextElement(); - if (!addr.isLoopbackAddress() && addr.getAddress().length == 4) { - return addr.getHostAddress(); - } + if (activeNetwork != null && activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface iface = interfaces.nextElement(); + Enumeration addresses = iface.getInetAddresses(); + + while (addresses.hasMoreElements()) { + InetAddress addr = addresses.nextElement(); + if (!addr.isLoopbackAddress() && addr.getAddress().length == 4) { + return addr.getHostAddress(); } } - } catch (Exception e) { - Log.e("NetworkUtils", "Error getting IP address: " + e.getMessage()); } + } catch (Exception e) { + Log.e("NetworkUtils", "Error getting IP address: " + e.getMessage()); } } } return null; } + /** + * 获取wifi的ipv4地址 + */ + public static String getWifiIpAddress() { + WifiManager wifiManager = (WifiManager) BaseIotUtils.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE); + + if (wifiManager != null && wifiManager.isWifiEnabled()) { + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + int ipAddress = wifiInfo.getIpAddress(); + + // Convert the IP address to a human-readable format + String ipAddressString = formatIpAddress(ipAddress); + + Log.d("WifiUtils", "WiFi IP Address: " + ipAddressString); + return ipAddressString; + } + + return null; + } + + private static String formatIpAddress(int ipAddress) { + return (ipAddress & 0xFF) + "." + + ((ipAddress >> 8) & 0xFF) + "." + + ((ipAddress >> 16) & 0xFF) + "." + + (ipAddress >> 24 & 0xFF); + } + /** * sim卡ccid *