package com.hdl.sdk.hdl_core.Util.NetUtil;
|
|
import android.content.Context;
|
import android.net.ConnectivityManager;
|
import android.net.DhcpInfo;
|
import android.net.NetworkInfo;
|
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;
|
import java.net.InterfaceAddress;
|
import java.net.NetworkInterface;
|
import java.util.Enumeration;
|
import java.util.List;
|
import java.util.regex.Pattern;
|
|
/**
|
* Created by Tommy on 2017/9/4.
|
*/
|
|
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 final int GET_GATEWAY = 4;
|
|
public static boolean checkNetworkAvailable(Context context) {
|
ConnectivityManager connectivity = (ConnectivityManager) context
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
if (connectivity == null) {
|
return false;
|
} else {
|
NetworkInfo[] info = connectivity.getAllNetworkInfo();
|
if (info != null) {
|
for (int i = 0; i < info.length; i++) {
|
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
|
NetworkInfo netWorkInfo = info[i];
|
if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
|
return true;
|
} else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
|
return true;
|
} else if (netWorkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
|
return true;
|
}
|
}
|
}
|
}
|
}
|
|
return false;
|
}
|
|
public static String getLocalIpString(Context context) {
|
String ip = "";
|
ConnectivityManager conMann = (ConnectivityManager)
|
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
// NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
if (wifiNetworkInfo.isConnected()) {
|
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
int ipAddress = wifiInfo.getIpAddress();
|
ip = intToIp(ipAddress);
|
}
|
return ip;
|
}
|
|
public static byte[] getWifiInfoByte(Context context, int type) {
|
byte[] address = null;
|
ConnectivityManager conMann = (ConnectivityManager)
|
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
// NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
if (wifiNetworkInfo.isConnected()) {
|
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
|
switch (type) {
|
case GET_LOCAL_IP:
|
address = bytesToIp(dhcpInfo.ipAddress);
|
// address = bytesToIp(wifiInfo.getIpAddress());
|
break;
|
case GET_MAC_ADDRESS:
|
String[] macArr = wifiInfo.getMacAddress().split(":");
|
address = new byte[]{(byte) Integer.parseInt(macArr[0], 16)
|
, (byte) Integer.parseInt(macArr[1], 16)
|
, (byte) Integer.parseInt(macArr[2], 16)
|
, (byte) Integer.parseInt(macArr[3], 16)
|
, (byte) Integer.parseInt(macArr[4], 16)
|
, (byte) Integer.parseInt(macArr[5], 16)
|
};
|
break;
|
case GET_NETMASK:
|
address = bytesToIp(dhcpInfo.netmask);
|
break;
|
case GET_GATEWAY:
|
address = bytesToIp(dhcpInfo.gateway);
|
break;
|
}
|
|
}
|
return address;
|
}
|
|
|
public static String intToIp(int ipInt) {
|
StringBuilder sb = new StringBuilder();
|
sb.append(ipInt & 0xFF).append(".");
|
sb.append((ipInt >> 8) & 0xFF).append(".");
|
sb.append((ipInt >> 16) & 0xFF).append(".");
|
sb.append((ipInt >> 24) & 0xFF);
|
return sb.toString();
|
}
|
|
private static byte[] bytesToIp(int ipInt) {
|
return new byte[]{(byte) ipInt, (byte) (ipInt >> 8), (byte) (ipInt >> 16), (byte) (ipInt >> 24)};
|
}
|
|
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();
|
while (netInterfaces.hasMoreElements()) {
|
NetworkInterface netInterface = (NetworkInterface) netInterfaces.nextElement();
|
if (!netInterface.isLoopback() && netInterface.isUp()) {
|
List<InterfaceAddress> interfaceAddresses = netInterface.getInterfaceAddresses();
|
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
|
//只有 IPv4 网络具有广播地址,因此对于 IPv6 网络将返回 null。
|
if (interfaceAddress.getBroadcast() != null) {
|
broadCastIp = interfaceAddress.getBroadcast().getHostAddress();
|
}
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
|
}
|
|
if(broadCastIp == null){
|
HDLLog.E("getLocalBroadCast error ip null");
|
}
|
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 {
|
try {
|
InetAddress candidateAddress = null;
|
// 遍历所有的网络接口
|
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
|
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
// 在所有的接口下再遍历IP
|
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
|
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
|
if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
|
if (inetAddr.isSiteLocalAddress()) {
|
// 如果是site-local地址,就是它了
|
return inetAddr;
|
} else if (candidateAddress == null) {
|
// site-local类型的地址未被发现,先记录候选地址
|
candidateAddress = inetAddr;
|
}
|
}
|
}
|
}
|
if (candidateAddress != null) {
|
return candidateAddress;
|
}
|
// 如果没有发现 non-loopback地址.只能用最次选的方案
|
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
return jdkSuppliedAddress;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static boolean isIP(String str) {
|
|
// 匹配 1
|
// String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
|
// 匹配 2
|
String regex = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
|
|
// 匹配1 和匹配2均可实现Ip判断的效果
|
Pattern pattern = Pattern.compile(regex);
|
|
return pattern.matcher(str).matches();
|
|
}
|
}
|