#【转帖】一探究竟:Android M 如何获取 Wifi MAC地址(1) @(Android研究)[Android|MAC地址]java
[TOC]android
##引子shell
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地址。app
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); }
输出以下:ide
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地址应该就是咱们要找的。ui
既然NetworkInterface
能够正常获取,那得好好看看它在 Android framework 中的实现源码:code
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"
文件中读取的!orm
这个name是什么呢?server
继续翻源码:
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
?
回到开头,咱们是经过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地址。那么官方推荐的“正道”该如何走呢?且看下回分解~