|
|
|
@ -22,6 +22,7 @@ import java.net.InetAddress; |
|
|
|
import java.net.NetworkInterface; |
|
|
|
import java.net.Socket; |
|
|
|
import java.net.SocketException; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Enumeration; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
@ -71,30 +72,6 @@ public class DeviceUtil { |
|
|
|
return getLocalIPByWIFI(); |
|
|
|
} |
|
|
|
|
|
|
|
// public static String getLocalIP() {
|
|
|
|
// try {
|
|
|
|
// String allIP = "";
|
|
|
|
// for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
|
|
|
|
// NetworkInterface intf = en.nextElement();
|
|
|
|
// for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
|
|
|
|
// InetAddress inetAddress = enumIpAddr.nextElement();
|
|
|
|
// allIP += inetAddress.getHostAddress() + "\n";
|
|
|
|
// if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress())
|
|
|
|
// return inetAddress.getHostAddress();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// } catch (Throwable t) {
|
|
|
|
// LoggerUtil.e("getLocalIP", StringUtil.getThrowableStr(t));
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return getLocalIPByWIFI();
|
|
|
|
// }
|
|
|
|
|
|
|
|
//有线网口ip
|
|
|
|
// public static String getLocalIP() {
|
|
|
|
// return getEthIpAddress();
|
|
|
|
// }
|
|
|
|
|
|
|
|
//获取有线网络ip
|
|
|
|
public static String getEthIpAddress() { |
|
|
|
String infaceName = "eth0"; |
|
|
|
@ -125,15 +102,6 @@ public class DeviceUtil { |
|
|
|
return ip; |
|
|
|
} |
|
|
|
|
|
|
|
//获取有线网络mac
|
|
|
|
public static String getLanMac(){ |
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext().getSystemService(Service.CONNECTIVITY_SERVICE); |
|
|
|
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); |
|
|
|
String extraInfo = activeNetworkInfo.getExtraInfo(); |
|
|
|
return extraInfo; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从wifi连接中获取ip地址 |
|
|
|
* |
|
|
|
@ -209,35 +177,39 @@ public class DeviceUtil { |
|
|
|
*/ |
|
|
|
public static String getLocalMacAddressFromIp() { |
|
|
|
String macAddress=""; |
|
|
|
WifiManager wifiManager = (WifiManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
|
|
|
macAddress = wifiInfo.getBSSID(); |
|
|
|
if(StringUtil.isEmpty(macAddress)){ |
|
|
|
macAddress = getMacFromNetworkInterface(); |
|
|
|
macAddress = getMacFromNetworkInterface("wlan0"); |
|
|
|
if (!StringUtil.isEmpty(macAddress)) { |
|
|
|
return macAddress; |
|
|
|
} |
|
|
|
macAddress = getMacFromNetworkInterface("eth0"); |
|
|
|
if (!StringUtil.isEmpty(macAddress)) { |
|
|
|
return macAddress; |
|
|
|
} |
|
|
|
return macAddress; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 通过网络接口获取MAC地址(备选方案) |
|
|
|
*/ |
|
|
|
private static String getMacFromNetworkInterface() { |
|
|
|
private static String getMacFromNetworkInterface(String interfaceName) { |
|
|
|
try { |
|
|
|
java.net.NetworkInterface networkInterface = java.net.NetworkInterface.getByName("wlan0"); |
|
|
|
if (networkInterface == null) return ""; |
|
|
|
|
|
|
|
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName); |
|
|
|
if (networkInterface == null) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
byte[] macBytes = networkInterface.getHardwareAddress(); |
|
|
|
if (macBytes == null) return ""; |
|
|
|
|
|
|
|
StringBuilder mac = new StringBuilder(); |
|
|
|
if (macBytes == null) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
for (byte b : macBytes) { |
|
|
|
mac.append(String.format("%02X:", b)); |
|
|
|
sb.append(String.format("%02X:", b)); |
|
|
|
} |
|
|
|
if (mac.length() > 0) { |
|
|
|
mac.deleteCharAt(mac.length() - 1); |
|
|
|
if (sb.length() > 0) { |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
} |
|
|
|
return mac.toString(); |
|
|
|
return sb.toString(); |
|
|
|
} catch (Exception e) { |
|
|
|
Log.e(TAG, "通过网络接口获取MAC失败", e); |
|
|
|
e.printStackTrace(); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
|