【转帖】一探究竟:Android M 如何获取 Wifi MAC地址(1)
2016-08-19 00:00
495 查看
#【转帖】一探究竟:Android M 如何获取 Wifi MAC地址(1)
@(Android研究)[Android|MAC地址]
[TOC]
##引子
早在 Android M 预览版发布时就有人发现,通过
输出如下:
顾名思义,猜想
原来MAC地址是直接从
这个name是什么呢?
继续翻源码:
可以看出
从路由器上在线设备的MAC地址列表,可以印证我这台设备Wifi的
那么读取文件
不出所料!
进而,问题又变成如何获取设备的Wifi的
而
在
顺藤摸瓜,又找到系统服务实现类
打开
如此这般终于找到定义设备的Wifi的
通过adb可以很容易得到这个属性值:
那么在我们应用里可以通过Java的反射获取
通过反射
读取
请注意:
个别设备可能会拒绝你访问
未免厂商实现不一的风险,请先尝试用
本文只是探讨了如何绕过官方设置的障碍,从“黑科技”的角度获取MAC地址。那么官方推荐的“正道”该如何走呢?且看下回分解~
@(Android研究)[Android|MAC地址]
[TOC]
##引子
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地址。那么官方推荐的“正道”该如何走呢?且看下回分解~
相关文章推荐
- Android M 如何获取 Wifi MAC地址
- Android 在wifi下获取IP和MAC地址
- 如何在android 中获取Wifi设备的IP地址
- Android获取Ethernet、WIFI的ip和mac地址
- Android 如何获取连接wifi热点的设备数量
- android 如何获取连接wifi热点的设备数量
- Android下如何获取Mac地址?
- android程序获取WIFI的IP地址和MAC地址
- 如何使用Java代码获取Android移动终端Mac地址
- Android开发之如何获取wifi的名称即SSID
- Android 6.0获取wifi Mac地址为02:00:00:00:00:00的解决方法
- Android如何获取Wifi名称即SSID
- Android如何获取Wifi名称即SSID
- Android获取WIFI状态下的IP地址以及MAC地址
- Android如何获取Wifi名称即SSID
- Android中Wifi未开启情况下获取Mac地址和IP
- Android中Wifi未开启情况下获取Mac地址和IP
- Android中Wifi未开启情况下获取Mac地址和IP
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android 不开WiFi获取Mac地址 夜未央