我在以编程方式检索设备的 MAC 地址时遇到问题,在有人提到我已经阅读过的其他帖子之前,例如: How to find MAC address of an Android device programmatically
但是我尝试在我自己的应用程序中使用代码并使用简单的 log.d 对其进行测试,结果发现它没有返回任何内容。 “看看这是否有效”的消息,但没有别的。所以我假设 mac 地址为空。
Log.d("seeing if this works", macAddress2);
我所做的代码如下所示:
//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);
}
});
最佳答案
您正在测试哪个 Android 版本?最新(10/2015) 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 .
关于java - 以编程方式检索 MAC 地址 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29550896/