开启未连接
ClientModeImpl
service/java/com/android/server/wifi/ClientModeImpl.java
setupClientMode
3637 if (isConnectedMacRandomizationEnabled()) {
3638 mWifiNative.setMacAddress(mInterfaceName, MacAddressUtils.createRandomUnicastAddress());
3639 }
打开WLAN,但不连接,关闭再打开WiFi MAC地址会变。
连接
更新MAC地址
updateWifiConfigOnStartConnection
连接时根据设置里的配置更新MAC地址,“网络详情"-》"高级”-》“隐私”,使用随机MAC(默认),使用设备MAC。
Network & internet -》WiFi -》Netowrk details -》Privacy:Use randomized MAC (default)、Use device MAC
6378 /**
6379 * Update the wifi configuration before sending connect to
6380 * supplicant/driver.
6381 *
6382 * @param config wifi configuration object.
6383 * @param bssid BSSID to assocaite with.
6384 */
6385 void updateWifiConfigOnStartConnection(WifiConfiguration config, String bssid) {
6470 if (isConnectedMacRandomizationEnabled()) {
6471 if (config.macRandomizationSetting == WifiConfiguration.RANDOMIZATION_PERSISTENT) {
6472 configureRandomizedMacAddress(config);
6473 } else {
6474 setCurrentMacToFactoryMac(config);
6475 }
6476 }
使用随机MAC(默认)
configureRandomizedMacAddress
配置随机MAC地址。
3301 * Dynamically change the MAC address to use the locally randomized
3302 * MAC address generated for each network.
3303 * @param config WifiConfiguration with mRandomizedMacAddress to change into. If the address
3304 * is masked out or not set, it will generate a new random MAC address.
3305 */
3306 private void configureRandomizedMacAddress(WifiConfiguration config) {
3307 if (config == null) {
3308 Log.e(TAG, "No config to change MAC address to");
3309 return;
3310 }
3311 String currentMacString = mWifiNative.getMacAddress(mInterfaceName);
3312 MacAddress currentMac = currentMacString == null ? null :
3313 MacAddress.fromString(currentMacString);
3314 MacAddress newMac = mWifiConfigManager.getRandomizedMacAndUpdateIfNeeded(config);
3315 if (!WifiConfiguration.isValidMacAddressForRandomization(newMac)) {
3316 Log.wtf(TAG, "Config generated an invalid MAC address");
3317 } else if (newMac.equals(currentMac)) {
3318 Log.d(TAG, "No changes in MAC address");
3319 } else {
3320 mWifiMetrics.logStaEvent(StaEvent.TYPE_MAC_CHANGE, config);
3321 boolean setMacSuccess =
3322 mWifiNative.setMacAddress(mInterfaceName, newMac);
3323 if (setMacSuccess) {
3324 mWifiNative.removeNetworkCachedDataIfNeeded(config.networkId, newMac);
3325 }
3326 Log.d(TAG, "ConnectedMacRandomization SSID(" + config.getPrintableSsid()
3327 + "). setMacAddress(" + newMac.toString() + ") from "
3328 + currentMacString + " = " + setMacSuccess);
3329 }
3330 }
获取随机MAC地址
getRandomizedMacAndUpdateIfNeeded
enhanced_mac_randomization_force_enabled=1按需更新MAC地址。
573 /**
574 * Returns the randomized MAC address that should be used for this WifiCo