android 获取mac地址

android获取Mac地址的两种方式,适用于6.0版本以下

public static String tryGetWifiMac(Context context) {
    WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();
    if (wi == null || wi.getMacAddress() == null) {
        return null;
    }
    if ("02:00:00:00:00:00".equals(wi.getMacAddress().trim())) {
        return null;
    } else {
        return wi.getMacAddress().trim();
    }
}

public static String getMac() {
    String mac = null;
    StringBuffer fileData = new StringBuffer(16);

    try {
        BufferedReader reader;
        reader = new BufferedReader(new FileReader("/sys/class/net/wlan0/address"));

        char[] buf = new char[16];
        boolean var5 = false;

        int numRead;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }

        reader.close();
        mac = fileData.toString();
    } catch (FileNotFoundException var7) {
        var7.printStackTrace();
    } catch (IOException var8) {
        var8.printStackTrace();
    }
    if (TextUtils.isEmpty(mac)) {
        return "uy0iTYfVGkhpC3yi";
    } else {
        mac = mac.replaceAll("\\\\n", "").replaceAll("\\n", "").trim();
        return mac;
    }
}

你可能感兴趣的:(util,android,mac,安卓)