| | |
| | | import android.net.wifi.WifiInfo; |
| | | import android.net.wifi.WifiManager; |
| | | |
| | | import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLDeviceManager; |
| | | import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog; |
| | | |
| | | import java.net.InetAddress; |
| | |
| | | */ |
| | | |
| | | public class NetWorkUtil { |
| | | |
| | | private static boolean isGetWifiBroadCast = false; |
| | | |
| | | public static boolean isIsGetWifiBroadCast() { |
| | | return isGetWifiBroadCast; |
| | | } |
| | | |
| | | public static void setIsGetWifiBroadCast(boolean isGetWifiBroadCast) { |
| | | NetWorkUtil.isGetWifiBroadCast = isGetWifiBroadCast; |
| | | } |
| | | |
| | | public static final int GET_LOCAL_IP = 1; |
| | | public static final int GET_MAC_ADDRESS = 2; |
| | | public static final int GET_NETMASK = 3; |
| | |
| | | } |
| | | |
| | | public static String getLocalBroadCast() { |
| | | String broadCastIp = "255.255.255.255"; |
| | | try { |
| | | //2020-1-17 新或者IP地址方法 |
| | | if (isGetWifiBroadCast) { |
| | | broadCastIp = getWifiBroadAddress(); |
| | | } else { |
| | | broadCastIp = getLocalBroadCastOld(); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | if (broadCastIp == null) { |
| | | HDLLog.E("getLocalBroadCast error ip null"); |
| | | } |
| | | return broadCastIp; |
| | | } |
| | | |
| | | public static String getLocalBroadCastOld() { |
| | | String broadCastIp = null; |
| | | try { |
| | | Enumeration<?> netInterfaces = (Enumeration<?>) NetworkInterface.getNetworkInterfaces(); |
| | |
| | | return broadCastIp; |
| | | } |
| | | |
| | | public static String getWifiBroadAddress() { |
| | | |
| | | String ipaddress = "255.255.255.255"; |
| | | try { |
| | | WifiManager wifimanage = (WifiManager) HDLDeviceManager.getViewContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);// 获取WifiManager |
| | | DhcpInfo dhcpInfo = wifimanage.getDhcpInfo(); |
| | | ipaddress = intToIp((dhcpInfo.ipAddress) | (~dhcpInfo.netmask)); |
| | | |
| | | } catch (Exception ex) { |
| | | ipaddress = "255.255.255.255"; |
| | | } |
| | | return ipaddress; |
| | | } |
| | | |
| | | |
| | | public static InetAddress getLocalHostLANAddress() throws Exception { |