最近在研究 Android 设备的唯一标识符,需要能对用户设备进行唯一标识,并且在被刷机后还能识别出身份,目的是为了防止渠道作弊。研究过程发现一个关于 MAC
地址比较实用的点,这里记录并分享以下。
Android 系统中提供了一些设备识别符,如 IMEI
、ANDROID_ID
等,但这些标识符的稳定性和唯一性并不理想。在进一步研究手机 wifi MAC
地址的过程中发现,系统路径 /sys/class/net/wlan0/
下的 address
文件保存有手机的 MAC
地址,并且在 root 权限下编辑后虽然提示保存成功但实际并没有生效。
所以考虑通过 cat
命令读取该文件,方法如下:
public String getMacAddress() {
String macAddress = null;
String str = "";
try {
Process process = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
InputStreamReader isr = new InputStreamReader(process.getInputStream());
LineNumberReader lnr = new LineNumberReader(isr);
while (str != null) {
str = lnr.readLine();
if (str != null) {
macAddress = str.trim();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return macAddress;
}
测试此方法可以成功提取到 MAC
地址后,还需要进一步验证其可靠性。在有 root 权限的手机上安装 xposed 框架,并通过程序“应用变量”修改设备的 MAC
地址。此时,再对比通过 WifiManager
获取到的 MAC
地址,该方法如下:
WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
String mac = wm.getConnectionInfo().getMacAddress();
两者结果如下:
可以发现,通过 address
文件读取到的 MAC
地址并没有被篡改,而通过 WifiManager
获取到的 MAC
地址被修改了。
为了进一步验证其准确性,在手头现有的十几台设备上进行了测试,测试结果均符合上述结论。但是在 Android 7.0 及以上版本的设备上可能无法读取到 address
文件,即返回 null
,这是因为系统对 shell 命令权限收紧的原因导致的。
这里还需要注意的是,在 Android 6.0 及以上版本的设备上,getMacAddress()
方法获取到结果会是 02:00:00:00:00:00
这一常量,这是由于官方不再对此方法进行支持导致的,可以参见 Android Developers Blog 中的说明:
Most notably, Local WiFi and Bluetooth MAC addresses are no longer available. The
getMacAddress()
method of a WifiInfo object and theBluetoothAdapter.getDefaultAdapter().getAddress()
method will both return02:00:00:00:00:00
from now on.