#核心点#
cat /sys/class/net/eth0/address
cat /sys/class/net/wlan0/address
Linux系统是规范化的,需要的东西一定会查找到具体的文件
通过运行shell命令的方式查找设备的MAC地址,通用性强、简洁易懂
protected String getMac() {
String mac = "";
String command1 = "cat /sys/class/net/eth0/address";
String command2 = "cat /sys/class/net/wlan0/address";
mac = runExe(command1);
Log.i("dd",mac);
if(mac.isEmpty()) {
mac=runExe(command2);
}
return mac;
}
public String runExe(String command) {
Runtime mRuntime = Runtime.getRuntime();
String result = "";
try {
Process mProcess = mRuntime.exec(command);
BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
StringBuffer mRespBuff = new StringBuffer();
char[] buff = new char[1024];
int ch = 0;
while ((ch = mReader.read(buff)) != -1) {
mRespBuff.append(buff, 0, ch);
}
mReader.close();
result = mRespBuff.toString();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}