NetworkInterface.getNetworkInterfaces() 硬件地址列表中没有 wlan0
NetworkInterface.getNetworkInterfaces() hardware address does not have wlan0 in list
我开发了 2 个移动应用程序。第一个在 Kotlin 中,第二个在 Java 中。我使用相同的代码在两者中获取 MAC 地址。但在 Kontlin 中它有效:
val all = Collections.list(NetworkInterface.getNetworkInterfaces())
for (nif in all) {
if (nif.name.contains("wlan") || nif.name.contains("eth")) {
val macBytes = nif.hardwareAddress ?: return ""
val res1 = StringBuilder()
for (b in macBytes) {
res1.append(String.format("%02X:", b))
}
if (res1.length > 0) {
res1.deleteCharAt(res1.length - 1)
}
return res1.toString().toUpperCase()
}
}
Collections.list(NetworkInterface.getNetworkInterfaces()) returns 25项,包括wlan0,
但在 Java 代码中(同一设备)
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (nif.getName().contains("wlan") || nif.getName().contains("eth")) {
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
mac = res1.toString().toUpperCase();
}
}
Collections.list(NetworkInterface.getNetworkInterfaces()) returns 3项,没有wlan0,但有swlan,所有列表项硬件地址为空。
两个应用都有权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我该如何解决?
我认为这两个应用程序可能具有不同的目标 SDK 版本。 NetworkInterface.getNetworkInterfaces()
的文档指出:
For non-system apps with targetSdkVersion >= android.os.Build.VERSION_CODES.R, this method will only return information for NetworkInterfaces that are associated with an InetAddress.
https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces()
我开发了 2 个移动应用程序。第一个在 Kotlin 中,第二个在 Java 中。我使用相同的代码在两者中获取 MAC 地址。但在 Kontlin 中它有效:
val all = Collections.list(NetworkInterface.getNetworkInterfaces())
for (nif in all) {
if (nif.name.contains("wlan") || nif.name.contains("eth")) {
val macBytes = nif.hardwareAddress ?: return ""
val res1 = StringBuilder()
for (b in macBytes) {
res1.append(String.format("%02X:", b))
}
if (res1.length > 0) {
res1.deleteCharAt(res1.length - 1)
}
return res1.toString().toUpperCase()
}
}
Collections.list(NetworkInterface.getNetworkInterfaces()) returns 25项,包括wlan0,
但在 Java 代码中(同一设备)
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (nif.getName().contains("wlan") || nif.getName().contains("eth")) {
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
mac = res1.toString().toUpperCase();
}
}
Collections.list(NetworkInterface.getNetworkInterfaces()) returns 3项,没有wlan0,但有swlan,所有列表项硬件地址为空。 两个应用都有权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我该如何解决?
我认为这两个应用程序可能具有不同的目标 SDK 版本。 NetworkInterface.getNetworkInterfaces()
的文档指出:
For non-system apps with targetSdkVersion >= android.os.Build.VERSION_CODES.R, this method will only return information for NetworkInterfaces that are associated with an InetAddress.
https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces()