Android设备获取系统Mac地址适配

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";
    }
}

参考:手机设备信息与 Android build.prop 文件 - 简书

你可能感兴趣的:(笔记,android)