|
|
|
@ -1,5 +1,6 @@ |
|
|
|
package qianmu.container.util; |
|
|
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
|
import android.app.Activity; |
|
|
|
import android.app.ActivityManager; |
|
|
|
import android.app.Service; |
|
|
|
@ -13,10 +14,14 @@ import android.net.wifi.WifiManager; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Environment; |
|
|
|
import android.os.StatFs; |
|
|
|
import android.provider.Settings; |
|
|
|
import android.util.DisplayMetrics; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.File; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.net.Inet4Address; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.NetworkInterface; |
|
|
|
@ -157,15 +162,16 @@ public class DeviceUtil { |
|
|
|
public static String getMacFromHardware() { |
|
|
|
String mac= ""; |
|
|
|
try { |
|
|
|
String Localmac = getLocalMacAddressFromIp(); |
|
|
|
String Localmac = getLocalMacAddress(); |
|
|
|
if(Localmac!= null && !Localmac.equals("")){ |
|
|
|
mac = Localmac.toLowerCase(); |
|
|
|
}else{ |
|
|
|
mac = getWifiMacAddress(); |
|
|
|
} |
|
|
|
LoggerUtil.e("mac获取","mac:"+mac); |
|
|
|
} catch (Exception e) { |
|
|
|
mac=""; |
|
|
|
LoggerUtil.e("mac获取","mac获取失败"+e.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
return mac; |
|
|
|
} |
|
|
|
@ -174,7 +180,7 @@ public class DeviceUtil { |
|
|
|
* 根据IP地址获取MAC地址 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String getLocalMacAddressFromIp() { |
|
|
|
public static String getLocalMacAddress() { |
|
|
|
String macAddress=""; |
|
|
|
macAddress = getMacFromNetworkInterface("wlan0"); |
|
|
|
if (!StringUtil.isEmpty(macAddress)) { |
|
|
|
@ -183,34 +189,123 @@ public class DeviceUtil { |
|
|
|
macAddress = getMacFromNetworkInterface("eth0"); |
|
|
|
if (!StringUtil.isEmpty(macAddress)) { |
|
|
|
return macAddress; |
|
|
|
}else{ |
|
|
|
macAddress = getLocalMacAddressFromIp(); |
|
|
|
} |
|
|
|
return macAddress; |
|
|
|
} |
|
|
|
|
|
|
|
public static String getMacFromNetworkInterface(String interfaceName) { |
|
|
|
try { |
|
|
|
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) { |
|
|
|
if (interfaceName.equalsIgnoreCase(networkInterface.getName())) { |
|
|
|
byte[] macBytes = networkInterface.getHardwareAddress(); |
|
|
|
if (macBytes != null) { |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
for (byte b : macBytes) { |
|
|
|
sb.append(String.format("%02X:", b)); |
|
|
|
} |
|
|
|
if (sb.length() > 0) { |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 通过网络接口获取MAC地址(备选方案) |
|
|
|
* 获取系统属性 |
|
|
|
*/ |
|
|
|
private static String getMacFromNetworkInterface(String interfaceName) { |
|
|
|
private static String getSystemProperty(String key) { |
|
|
|
try { |
|
|
|
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName); |
|
|
|
if (networkInterface == null) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
byte[] macBytes = networkInterface.getHardwareAddress(); |
|
|
|
if (macBytes == null) { |
|
|
|
return ""; |
|
|
|
Class<?> systemProperties = Class.forName("android.os.SystemProperties"); |
|
|
|
Method getMethod = systemProperties.getMethod("get", String.class); |
|
|
|
return (String) getMethod.invoke(null, key); |
|
|
|
} catch (Exception e) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 验证MAC地址格式 |
|
|
|
*/ |
|
|
|
private static boolean isValidMacAddress(String mac) { |
|
|
|
if (StringUtil.isEmpty(mac)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// MAC地址正则表达式
|
|
|
|
String macPattern = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"; |
|
|
|
return mac.matches(macPattern); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 格式化MAC地址 |
|
|
|
*/ |
|
|
|
private static String formatMacAddress(String mac) { |
|
|
|
if (StringUtil.isEmpty(mac)) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
// 移除所有分隔符
|
|
|
|
String cleanMac = mac.replace(":", "").replace("-", "").replace(" ", ""); |
|
|
|
|
|
|
|
// 重新格式化为冒号分隔
|
|
|
|
if (cleanMac.length() == 12) { |
|
|
|
StringBuilder formatted = new StringBuilder(); |
|
|
|
for (int i = 0; i < 12; i += 2) { |
|
|
|
formatted.append(cleanMac.substring(i, i + 2)); |
|
|
|
if (i < 10) { |
|
|
|
formatted.append(":"); |
|
|
|
} |
|
|
|
} |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
for (byte b : macBytes) { |
|
|
|
sb.append(String.format("%02X:", b)); |
|
|
|
return formatted.toString().toUpperCase(); |
|
|
|
} |
|
|
|
|
|
|
|
return mac.toUpperCase(); |
|
|
|
} |
|
|
|
/** |
|
|
|
* 根据IP地址获取MAC地址 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String getLocalMacAddressFromIp() { |
|
|
|
String strMacAddr = null; |
|
|
|
try { |
|
|
|
//获得IpD地址
|
|
|
|
InetAddress ip = getLocalInetAddress(); |
|
|
|
LoggerUtil.e("IP: ", ip.getHostAddress()); |
|
|
|
byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress(); |
|
|
|
StringBuffer buffer = new StringBuffer(); |
|
|
|
for (int i = 0; i < b.length; i++) { |
|
|
|
if (i != 0) { |
|
|
|
buffer.append(':'); |
|
|
|
} |
|
|
|
String str = Integer.toHexString(b[i] & 0xFF); |
|
|
|
buffer.append(str.length() == 1 ? 0 + str : str); |
|
|
|
} |
|
|
|
if (sb.length() > 0) { |
|
|
|
sb.deleteCharAt(sb.length() - 1); |
|
|
|
strMacAddr = buffer.toString().toUpperCase(); |
|
|
|
} catch (Exception e) { |
|
|
|
} |
|
|
|
return strMacAddr; |
|
|
|
} |
|
|
|
|
|
|
|
@SuppressLint("HardwareIds") |
|
|
|
public static String getWifiMacAddress() { |
|
|
|
try { |
|
|
|
WifiManager wifiManager = (WifiManager) MyApplication.getInstance().getSystemService(Context.WIFI_SERVICE); |
|
|
|
if (wifiManager != null) { |
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
|
|
|
return wifiInfo.getMacAddress(); |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 获取移动设备本地IP |
|
|
|
|