当前位置:首页 > 操作系统 > MacOs

java – 以编程方式检索MAC地址 – Android

我遇到了以编程方式检索设备的MAC地址的问题,在任何人提及我已经阅读过的其他帖子之前,例如:
How to find MAC address of an Android device programmatically

但是我尝试在我自己的应用程序中使用代码并使用简单的log.d对其进行测试,结果却发现它没有返回任何内容. “看看这是否有效”的信息,但没有别的.所以我假设mac地址为空.

<code>Log.d("seeing if this works", macAddress2);
</code>

我所做的代码如下所示:

<code>//Set onclick listener for the Get Mac Address button
        getMac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                WifiInfo wInfo = wifiManager.getConnectionInfo();
                String macAddress2 = wInfo.getMacAddress();

                macAddress.setText(macAddress2);
            }
        });
</code>

解决方法:

您正在测试哪个Android版本?最新的(2015年10月)Android M preview阻止了应用程序获取Wifi和蓝牙的硬件标识符.

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

通过从/ sys / class / net / wlan0 / address读取Wifi MAC有一个解决方法,但是在Android N中也会阻止它作为claimed by Google.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐