Android设备获取系统Mac地址适配
2024-01-01 08:17:09
public static String getMacAddress() {
try {
return loadFileAsString(getEthernet() != "0" ? "/sys/class/net/eth0/address" : "/sys/class/net/wlan0/address").toUpperCase(Locale.ENGLISH).substring(0, 17);
} catch (IOException e) {
return "null";
}
}
//获取Mac地址 filePath判断是网卡还是wifi,wifi获取的mac地址会变
private static String loadFileAsString(String filePath) throws java.io.IOException {
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}
// 判断是否插入网线
public static String getEthernet() {
try {
return loadFileAsString("/sys/class/net/eth0/carrier").toUpperCase(Locale.ENGLISH).substring(0, 1);
} catch (IOException e) {
return "0";
}
}
文章来源:https://blog.csdn.net/PymBoy/article/details/135250467
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!