android6.0获取mac地址

之前用的这个,但是不能获取到6.0的
public static String getMac() {
    String str = "";
    String macSerial = "";
    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) {
                macSerial = str.trim();// 去空格
                break;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (macSerial == null || "".equals(macSerial)) {
        try {
            return loadFileAsString("/sys/class/net/eth0/address")
                    .toUpperCase().substring(0, 17);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
    return macSerial;
}
现在用的这个,6.0的可以获取到mac的wifi地址
/**
 * 获取手机的WLAN MAC地址  通用
 *
 * @return
 */

public static String getMacAddr() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }
            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(String.format("%02X:",b));
            }
            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {

    }
    return "02:00:00:00:00:00";
}

猜你喜欢

转载自blog.csdn.net/qq_37451877/article/details/79878939