public static String getLocalMacAddressFromBusybox() {
String result = callCmd("busybox ifconfig", "HWaddr");
if (result == null) return "網絡錯誤";
if (result.contains("HWaddr")) {
String mac = result.substring(result.indexOf("HWaddr") + 6).trim();
return mac.replaceAll("\\s+", ""); // 去除空格
}
return "獲取失敗";
}
private static String callCmd(String cmd, String filter) {
try {
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.contains(filter)) return line;
}
} catch (Exception e) { e.printStackTrace(); }
return null;
}
android.permission.INTERNET (執行命令需)。// Android 6.0+ 需定位權限
WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE);
String mac = wifi.getConnectionInfo().getMacAddress(); // Android 6.0+ 返回固定 02:00:00:00:00:00
// 正確方式:掃描掃描結果
List results = wifi.getScanResults();
for (ScanResult r : results) if (r.BSSID != null) return r.BSSID; // 非本機 MAC
// 唯一可靠:讀取 /sys/class/net/wlan0/address (需 Root)
String mac = FileUtils.readFile("/sys/class/net/wlan0/address").trim();
Android 10+ **完全禁止** 非系統應用獲取真實 MAC,返回隨機化地址。唯有系統簽名應用或 Root 讀取系統節點可獲真實 MAC。