直接上代码, 跟上节奏
1. 首先添加相关的权限
在 AndroidManifest.xml 文件中添加INTERNET 权限
<uses-permission android:name="android.permission.INTERNET" />
2.1 获取mac地址 【JAVA方法】
public static String getMacAddress() {
try {
List<NetworkInterface> infos = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface info: infos) {
if (!info.getName().equals("wlan0")) {
continue;
}
byte[] macBytes = info.getHardwareAddress();
List<String> macByteList = new ArrayList<>();
for (Byte byt: macBytes) {
macByteList.add(String.format("%02X", byt));
}
return String.join(":", macByteList);
}
} catch (Exception e) {
e.printStackTrace();
}
return "00:00:00:00:00:00";
}
2.2 获取mac地址 kotlin方法】
fun getMacAddress(): String? {
try {
val infos: List<NetworkInterface> =
Collections.list(NetworkInterface.getNetworkInterfaces())
for (info in infos) {
if (info.name != "wlan0") {
continue
}
val macBytes = info.hardwareAddress
val macByteList: MutableList<String> = ArrayList()
for (byt in macBytes) {
macByteList.add(String.format("%02X", byt))
}
return java.lang.String.join(":", macByteList)
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return "00:00:00:00:00:00"
}
|