求问,如何获取Android设备正确的MAC地址

2025-03-21 19:49:05
推荐回答(1个)
回答1:

获取mac地址的话,可以在命令行窗口获取,代码如下:
Android 底层是 Linux,我们还是用Linux的方法来获取:
1 cpu号:
文件在: /proc/cpuinfo
通过Adb shell 查看:
adb shell cat /proc/cpuinfo
2 mac 地址
文件路径 /sys/class/net/wlan0/address
adb shell cat /sys/class/net/wlan0/address
xx:xx:xx:xx:xx:aa

具体的实现代码为:
public static String getLocalMac() {
String mac=null;
String str = "";
try
{
Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;)
{
str = input.readLine();
if (str != null)
{
mac = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
return mac;

}