android获取Mac地址的两种方式,适用于6.0版本以下

public static String tryGetWifiMac(Context context) {

WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

WifiInfo wi = wm.getConnectionInfo();

if (wi == null || wi.getMacAddress() == null) {

return null;

}

if ("02:00:00:00:00:00".equals(wi.getMacAddress().trim())) {

return null;

} else {

return wi.getMacAddress().trim();

}

}

public static String getMac() {

String mac = null;

StringBuffer fileData = new StringBuffer(16);

try {

BufferedReader reader;

reader = new BufferedReader(new FileReader("/sys/class/net/wlan0/address"));

char[] buf = new char[16];

boolean var5 = false;

int numRead;

while ((numRead = reader.read(buf)) != -1) {

String readData = String.valueOf(buf, 0, numRead);

fileData.append(readData);

}

reader.close();

mac = fileData.toString();

} catch (FileNotFoundException var7) {

var7.printStackTrace();

} catch (IOException var8) {

var8.printStackTrace();

}

if (TextUtils.isEmpty(mac)) {

return "uy0iTYfVGkhpC3yi";

} else {

mac = mac.replaceAll("\\\\n", "").replaceAll("\\n", "").trim();

return mac;

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐