【发布时间】:2022-01-04 05:37:27
【问题描述】:
我在我的应用程序中获得了 mac 地址。当 targetSdkVersion 为 29 时一切都正确,但是当将 targetSdkVersion 更新为 31 时,我无法获取 mac 地址。
private fun getMacAddress(): String =
NetworkInterface.getNetworkInterfaces().toList()
.firstOrNull { it.name.equals("wlan0", ignoreCase = true) }?.let {
it.hardwareAddress?.let { macBytes ->
StringBuilder().apply {
for (b in macBytes) {
append(String.format("%02X:", b))
}
if (isNotEmpty()) {
deleteCharAt(lastIndex)
}
}
}?.toString() ?: run {
"00:00:00:00:00:00"
}
} ?: "00:00:00:00:00:00"
【问题讨论】:
标签: java android kotlin mac-address