一、概述
最近在看android wifi mac地址相关的一些内容,发现网上对android 13的 wifi framework 层代码分析较少,因此写下了这篇关于wifi mac地址分析的文章。
文章主要包括以下几部分:
- 如何禁用wifi的随机mac地址,使用wifi自带的固定mac地址。
- 讲解wifi mac地址相关的代码,弄明白函数之间的调用关系。
- 简单画了关于wifi mac地址的函数调用关系。
二、禁用随机mac地址的方法
关于mac地址,android 13有三种模式:
- 使用出厂自带的mac地址。
- 使用持久的随机mac地址。
- 使用随机的mac地址,也是android 13默认的方式。
下面将以AP功能进行讲解。
1、 禁用随机mac地址,使用出厂自带的mac地址方法
这个比较简单,直接修改xml配置就可以了。
配置文件所在位置:packages/modules/Wifi/service/ServiceWifiResources/res/values/config.xml
<!-- Indicates that AP mode MAC randomization is supported on this device -->
<bool translatable="false" name="config_wifi_ap_mac_randomization_supported">true</bool>
直接将上面的true改为false,wifi 就会使用wifi自带的固定地址了。如下:
<!-- Indicates that AP mode MAC randomization is supported on this device -->
<bool translatable="false" name="config_wifi_ap_mac_randomization_supported">false</bool>
具体原理,后面会讲到。
2、使用持久化的随机mac地址方法
在WifiApConfigStore.java类中修改代码,修改的函数和内容如下:
/**
¦* Return the current soft access point configuration.
¦*/
public synchronized SoftApConfiguration getApConfiguration() {
¦ if (mPersistentWifiApConfig == null) {
¦ ¦ /* Use default configuration. */
¦ ¦ Log.d(TAG, "Fallback to use default AP configuration");
¦ ¦ persistConfigAndTriggerBackupManagerProxy(
¦ ¦ ¦ ¦ updatePersistentRandomizedMacAddress(getDefaultApConfiguration()));
¦ }
¦ SoftApConfiguration sanitizedPersistentconfig =
¦ ¦ ¦ sanitizePersistentApConfig(mPersistentWifiApConfig);
//追加的代码 开始
¦ SoftApConfiguration.Builder convertedConfigBuilder =
¦ ¦ ¦ new SoftApConfiguration.Builder(sanitizedPersistentconfig);
convertedConfigBuilder.setMacRandomizationSetting(SoftApConfiguration.RANDOMIZATION_PERSISTENT);
sanitizedPersistentconfig = sanitizedPersistentconfig.build();
//追加的代码 结束
¦ if (!Objects.equals(mPersistentWifiApConfig, sanitizedPersistentconfig)) {
¦ ¦ Log.d(TAG, "persisted config was converted, need to resave it");
¦ ¦ persistConfigAndTriggerBackupManagerProxy(sanitizedPersistentconfig);
¦ }
¦ if (mForceApChannel) {
¦ ¦ Log.d(TAG, "getApConfiguration: Band force to " + mForcedApBand
¦ ¦ ¦ ¦ + ", and channel force to " + mForcedApChannel);
¦ ¦ return mForcedApChannel == 0
¦ ¦ ¦ ¦ ? new SoftApConfiguration.Builder(mPersistentWifiApConfig)
¦ ¦ ¦ ¦ ¦ ¦ .setBand(mForcedApBand).build()
¦ ¦ ¦ ¦ : new SoftApConfiguration.Builder(mPersistentWifiApConfig)
¦ ¦ ¦ ¦ ¦ ¦ .setChannel(mForcedApChannel, mForcedApBand).build();
¦ }