可以通过编程方式获取运行Android 6.0 +的设备的MAC地址吗?
根据this,
为用户提供更好的数据保护,从此开始 发布时,Android会删除对设备本地的编程访问权限 使用Wi-Fi和蓝牙API的应用的硬件标识符。该 WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法 现在返回一个恒定值02:00:00:00:00:00。
这是否意味着无法在Android 6.0+中获取设备的MAC地址?如果有可能,您能告诉我如何在Android Studio中执行此操作吗?
此外,this answer仅适用于Android版本低于6.0的设备
答案 0 :(得分:8)
您可以使用其他方法在Android 6.0设备上获取MAC地址。
首先将Internet用户权限添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
其次,
try {
// get all the interfaces
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
//find network interface wlan0
for (NetworkInterface networkInterface : all) {
if (!networkInterface.getName().equalsIgnoreCase("wlan0")) continue;
//get the hardware address (MAC) of the interface
byte[] macBytes = networkInterface.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
//gets the last byte of b
res1.append(Integer.toHexString(b & 0xFF) + ":");
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}