Android 6.0以后获取本地mac地址

来源:互联网 发布:关于美食的评价知乎 编辑:程序博客网 时间:2024/06/05 20:28
    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";    }
原创粉丝点击