Android 获取MAC地址最靠谱的方法。

       众所周知在安卓开发中,咱们常用WifiManager获取MAC地址作设备映射,但会出现这样那样的问题,好比说开了WIFI获取不到地址了、不开WIFI获取不到地址了、刚开机获取不到地址了、网卡未启动获取不到地址了。综上所述最靠谱的方法仍是直接用busybox读取系统文件中的MAC地址比较稳妥。java

   public String getMacAddress() {
        String result = "";
        String Mac = "";
        result = callCmd("busybox ifconfig", "HWaddr");

        if (result == null) {
            return "网络出错,请检查网络";
        }
        if (result.length() > 0 && result.contains("HWaddr")) {
            Mac = result.substring(result.indexOf("HWaddr") + 6, result.length() - 1);
            if (Mac.length() > 1) {
                result = Mac.toLowerCase();
            }
        }
        return result.trim();
    }
相关文章
相关标签/搜索