获取Mac地址

private static String getMacByADB() {
String str="";
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) {
return str.trim();// 去空格
}
}
} catch (IOException ex) {
ex.printStackTrace();
return "00:00:00:00:00:00";
}
return "00:00:00:00:00:00";
}

public static String getMacByAPI(Context context) {
String macAddress = "00:00:00:00:00:00";
try {
WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = (null == wifiMgr ? null : wifiMgr
.getConnectionInfo());
if (null != info) {
if (!TextUtils.isEmpty(info.getMacAddress())){
macAddress = info.getMacAddress();
if (macAddress.equals("02:00:00:00:00:00")) {
return "00:00:00:00:00:00";
}
}
else
return macAddress;
}
} catch (Exception e) {
e.printStackTrace();
return macAddress;
}
return macAddress;
}

从SharePreferce中获取:getMacinforAddress

100000次
getMacinforAddress耗时:434
getMacByAPI耗时:76213
getMacByADB耗时:时间太长,没有打印出来

100次
getMacinforAddress耗时:28
getMacByAPI耗时:172
getMacByADB耗时:2989

2次
getMacinforAddress耗时:3
getMacByAPI耗时:25
getMacByADB耗时:109

1次
getMacinforAddress耗时:0
getMacByAPI耗时:21
getMacByADB耗时:141

原文地址:https://www.cnblogs.com/shixm/p/5436273.html