Android 获取MAC地址的代码

jopen 9年前

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

   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();      }