##引子
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String macAddress = wifiManager.getConnectionInfo().getMacAddress();
早在 Android M 预览版发布时就有人发现,通过WifiInfo.getMacAddress()
获取的MAC地址是一个“假”的固定值,其值为 “02:00:00:00:00:00”。对于这个,官方的说法当然不外乎“保护用户隐私数据”。
大胆假设
不知是有意还是一时不查,Google却忘了Java获取设备网络设备信息的API——NetworkInterface.getNetworkInterfaces()
——仍然可以间接地获取到MAC地址。
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iF = interfaces.nextElement();
byte[] addr = iF.getHardwareAddress();
if (addr == null || addr.length == 0) {
continue;
}
StringBuilder buf = new StringBuilder();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
String mac = buf.toString();
Log.d("mac", "interfaceName="+iF.getName()+", mac="+mac);
}
输出如下:
interfaceName=dummy0, mac=e6:f9:44:3c:ee:da
interfaceName=p2p0, mac=ae:22:0b:3e:d4
interfaceName=wlan0, mac=ac:22:0b:3e:d4
...
顾名思义,猜想wlan0
对应的mac地址应该就是我们要找的。
小心求证
既然NetworkInterface
可以正常获取,那得好好看看它在 Android framework 中的实现源码:
public byte[] getHardwareAddress() throws SocketException {
try {
// Parse colon-separated bytes with a trailing newline: "aa:bb:cc:dd:ee:ff\n".
String s = IoUtils.readFileAsString("/sys/class/net/" + name + "/address");
byte[] result = new byte[s.length()/3];
for (int i = 0; i < result.length; ++i) {
result[i] = (byte) Integer.parseInt(s.substring(3*i, 3*i + 2), 16);
}
// We only want to return non-zero hardware addresses.
for (int i = 0; i < result.length; ++i) {
if (result[i] != 0) {
return result;
}
}
return null;
} catch (Exception ex) {
throw rethrowAsSocketException(ex);
}
}
原来MAC地址是直接从"/sys/class/net/" + name + "/address"
文件中读取的!
这个name是什么呢?
继续翻源码:
private static final File SYS_CLASS_NET = new File("/sys/class/net");
...
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException {
return Collections.enumeration(getNetworkInterfacesList());
}
private static List<NetworkInterface> getNetworkInterfacesList() throws SocketException {
String[] interfaceNames = SYS_CLASS_NET.list();
NetworkInterface[] interfaces = new NetworkInterface[interfaceNames.length];
String[] ifInet6Lines = readIfInet6Lines();
for (int i = 0; i < interfaceNames.length; ++i) {
interfaces[i] = NetworkInterface.getByNameInternal(interfaceNames[i], ifInet6Lines);
...
}
List<NetworkInterface> result = new ArrayList<NetworkInterface>();
for (int counter = 0; counter < interfaces.length; counter++) {
...
result.add(interfaces[counter]);
}
return result;
}
可以看出/sys/class/net
目录下的一个文件夹即对应一个NetworkInterface
的name
。
user@android:/$ ls /sys/class/net/
dummy0
lo
p2p0
rev_rmnet0
rev_rmnet1
rev_rmnet2
rev_rmnet3
rmnet0
rmnet1
rmnet2
rmnet3
rmnet_smux0
sit0
wlan0
从路由器上在线设备的MAC地址列表,可以印证我这台设备Wifi的name
是wlan0
那么读取文件/sys/class/net/wlan0/address
就轻松得到了这台设备的MAC地址
user@android:/$ cat /sys/class/net/wlan0/address
ac:22:0b:3e:d4
不出所料! 进而,问题又变成如何获取设备的Wifi的interface name
?
探寻 Wifi interface name
回到开头,我们是通过context.getSystemService(Context.WIFI_SERVICE)
获取的WifiManager
。
而WifiManager
肯定是与远程系统服务的IBinder
在交互,而系统服务都是在SystemServer.run()
中被启动的。
在SystemServer.java
中搜索关键字”WIFI_SERVICE”,很容易便找到mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
顺藤摸瓜,又找到系统服务实现类com.android.server.wifi.WifiService
,WifiService
中的逻辑很简单,构造真正的实现类com.android.server.wifi.WifiServiceImpl
对象并注册到系统服务中:
public final class WifiService extends SystemService {
private static final String TAG = "WifiService";
final WifiServiceImpl mImpl;
public WifiService(Context context) {
super(context);
mImpl = new WifiServiceImpl(context);
}
@Override
public void onStart() {
Log.i(TAG, "Registering " + Context.WIFI_SERVICE);
publishBinderService(Context.WIFI_SERVICE, mImpl);
}
@Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
mImpl.checkAndStartWifi();
}
}
}
打开WifiServiceImpl.java
,从构造方法处,一眼就看到了关键代码:mInterfaceName = SystemProperties.get("wifi.interface", "wlan0");
如此这般终于找到定义设备的Wifi的interface name
的地方:SystemProperties
通过adb可以很容易得到这个属性值:adb shell getprop wifi.interface
那么在我们应用里可以通过Java的反射获取SystemProperties
,进而调用静态方法get
即可拿到Wifi的interface name
。
结论
纵然Google掩耳盗铃似的把API返回值篡改了,但终究是没有改底层的实现,通过阅读源码的方式还是很容易找到解决办法的。
- 通过反射
SystemProperties
获取属性wifi.interface
的值 - 读取
/sys/class/net/+interfaceName+/address
文件的第一行即是Wifi MAC地址
请注意:
- 个别设备可能会拒绝你访问
/sys
目录 -
/sys/class/net/+interfaceName+/address
文件不一定存在,请在读取之前确认设备的Wifi已经打开或已连接 - 未免厂商实现不一的风险,请先尝试用
NetworkInterface.getByName(interfaceName)
来间接获取Wifi MAC地址
结束了?
这么早下结论其实不是我的风格。
本文只是探讨了如何绕过官方设置的障碍,从“黑科技”的角度获取MAC地址。那么官方推荐的“正道”该如何走呢?且看下回分解~