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

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐