From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 17:14:51 +0800 Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc"" --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/IpUtils.java | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 89 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/IpUtils.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/IpUtils.java new file mode 100644 index 0000000..aadb63e --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/IpUtils.java @@ -0,0 +1,89 @@ +package com.hdl.sdk.link.common.utils; + +import android.content.Context; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; + +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"; + } + + 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); + } +} -- Gitblit v1.8.0