如何以编程方式访问最新的Android设备上的WiFi MacAddress?
我正在寻找使其他属性与其他属性独特的属性,因此Acaddress是一个。
现在,我正在尝试找到连接的WiFi的弹药板。我正在关注此文章在Android 6.0上有效的作品(作者说),但在Android 11上遇到一个错误,该尝试在0..IT.IT.HardWareadDress上获取null Array的长度
in i .size 。这意味着HardWareadDress数组为空。
那么,我们如何在最新的Android设备上访问WiFi的磁贴?
以下是我尝试的代码:
fun getMacAddress(): String {
var stringMac = "default"
try {
val networkInterfaceList = Collections.list(NetworkInterface.getNetworkInterfaces())
for (it in networkInterfaceList) {
if (it.name.lowercase() == "wlan0") {
Log.d(TAG, "getMacAddress: Yes Equals")
for (i in 0..it.hardwareAddress.size) {
var stringMacByte =
Integer.toHexString((it.hardwareAddress[i] and 0xFF.toByte()).toInt())
if (stringMacByte.length == 1) {
stringMacByte = "0$stringMacByte"
}
stringMac = stringMac + stringMacByte.toUpperCase() + ":"
}
break
}
}
return stringMac
} catch (e: SocketException) {
return stringMac
}
}
I was looking for the attributes which make a wifi Access point unique from others so macAddress is one.
Now i'm trying to find the macAddress of connected wifi. I'm following this article which works on android 6.0 (author said) but getting an error on android 11 that Attempt to get length of null array
on i in 0..it.hardwareAddress.size
. which means hardwareAddress array is null.
So how can we access MacAddress of Wifi on the latest android devices?
below is the code i tried:
fun getMacAddress(): String {
var stringMac = "default"
try {
val networkInterfaceList = Collections.list(NetworkInterface.getNetworkInterfaces())
for (it in networkInterfaceList) {
if (it.name.lowercase() == "wlan0") {
Log.d(TAG, "getMacAddress: Yes Equals")
for (i in 0..it.hardwareAddress.size) {
var stringMacByte =
Integer.toHexString((it.hardwareAddress[i] and 0xFF.toByte()).toInt())
if (stringMacByte.length == 1) {
stringMacByte = "0$stringMacByte"
}
stringMac = stringMac + stringMacByte.toUpperCase() + ":"
}
break
}
}
return stringMac
} catch (e: SocketException) {
return stringMac
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改integer.tohexstring for String.format(“%02X”,x),因为当您的mac_addr包含0(零)时,转换为整数将消失,并且您将获得11个数字Mac,那是错误的,它是错误的,应包含12个字符;
change integer.toHexstring for String.format("%02X", x), because when your mac_addr contains 0 (zero), to convert to integer will be dissapear, and you will be got an 11 digits mac, that its wrong, it should be contain 12 chars;