如何以编程方式访问最新的Android设备上的WiFi MacAddress?

发布于 2025-01-24 14:31:54 字数 1420 浏览 4 评论 0原文

我正在寻找使其他属性与其他属性独特的属性,因此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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-01-31 14:31:54

更改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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文