Android WiFi MAC 默認(rèn)選擇設(shè)備MAC (Android 13)

image.png

隨機(jī)mac地址開(kāi)關(guān)

    <!-- True if the firmware supports connected MAC randomization -->
    <bool name="config_wifi_connected_mac_randomization_supported">true</bool>

service/java/com/android/server/wifi/WifiGlobals.java

    /**
     * Helper method to check if Connected MAC Randomization is supported - onDown events are
     * skipped if this feature is enabled (b/72459123).
     *
     * @return boolean true if Connected MAC randomization is supported, false otherwise
     */
    public boolean isConnectedMacRandomizationEnabled() {
        return mContext.getResources().getBoolean(
                R.bool.config_wifi_connected_mac_randomization_supported);
    }

service/java/com/android/server/wifi/ClientModeImpl.java

  /**
     * Update the wifi configuration before sending connect to
     * supplicant/driver.
     *
     * @param config wifi configuration object.
     * @param bssid BSSID to assocaite with.
     */
    void updateWifiConfigOnStartConnection(WifiConfiguration config, String bssid) {
        setTargetBssid(config, bssid);

        // Go through the matching scan results and update wifi config.
        ScanResultMatchInfo key1 = ScanResultMatchInfo.fromWifiConfiguration(config);
        List<ScanResult> scanResults = mScanRequestProxy.getScanResults();
        for (ScanResult scanResult : scanResults) {
            if (!config.SSID.equals(ScanResultUtil.createQuotedSsid(scanResult.SSID))) {
                continue;
            }
            ScanResultMatchInfo key2 = ScanResultMatchInfo.fromScanResult(scanResult);
            if (!key1.equals(key2)) {
                continue;
            }
            updateWifiConfigFromMatchingScanResult(config, scanResult);
        }

        selectCandidateSecurityParamsIfNecessary(config, scanResults);

        if (mWifiGlobals.isConnectedMacRandomizationEnabled()) {
            boolean isMacRandomizationForceDisabled = isMacRandomizationForceDisabledOnSsid(config);
            if (config.macRandomizationSetting == WifiConfiguration.RANDOMIZATION_NONE
                    || isMacRandomizationForceDisabled) {
                setCurrentMacToFactoryMac(config);
            } else {
                configureRandomizedMacAddress(config);
            }
            if (isMacRandomizationForceDisabled
                    && config.macRandomizationSetting != WifiConfiguration.RANDOMIZATION_NONE) {
                // update WifiConfigManager to disable MAC randomization so Settings show the right
                // MAC randomization information.
                config.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE;
                mWifiConfigManager.addOrUpdateNetwork(config, Process.SYSTEM_UID);
            }
        }

        if (config.enterpriseConfig != null
                && config.enterpriseConfig.isAuthenticationSimBased()
                && mWifiCarrierInfoManager.isImsiEncryptionInfoAvailable(
                mWifiCarrierInfoManager.getBestMatchSubscriptionId(config))
                && TextUtils.isEmpty(config.enterpriseConfig.getAnonymousIdentity())) {
            String anonAtRealm = mWifiCarrierInfoManager
                    .getAnonymousIdentityWith3GppRealm(config);
            // Use anonymous@<realm> when pseudonym is not available
            config.enterpriseConfig.setAnonymousIdentity(anonAtRealm);
        }
    }

默認(rèn)選擇設(shè)備MAC
修改方式1:直接切換一次選項(xiàng)
WifiConfigController2在NetworkProviderSettings.java等文件中有關(guān)聯(lián)使用,所以直接切換一次就可以達(dá)到相關(guān)效果;
注意:不過(guò)這種方式可能存在bug 如果用第三方應(yīng)用聯(lián)網(wǎng)的話而非系統(tǒng)設(shè)置,可能不走相關(guān)流程

Index: src/com/android/settings/wifi/WifiConfigController.java
===================================================================
@@ -285,6 +285,7 @@
         if (mWifiManager.isConnectedMacRandomizationSupported()) {
             View privacySettingsLayout = mView.findViewById(R.id.privacy_settings_fields);
             privacySettingsLayout.setVisibility(View.VISIBLE);
+            mPrivacySettingsSpinner.setSelection(1);
         }
         mHiddenSettingsSpinner.setOnItemSelectedListener(this);
         mHiddenWarningView = mView.findViewById(R.id.hidden_settings_warning);
Index: src/com/android/settings/wifi/WifiConfigController2.java
===================================================================
@@ -289,6 +289,7 @@
         if (mWifiManager.isConnectedMacRandomizationSupported()) {
             View privacySettingsLayout = mView.findViewById(R.id.privacy_settings_fields);
             privacySettingsLayout.setVisibility(View.VISIBLE);
+            mPrivacySettingsSpinner.setSelection(1);
         }
         mHiddenSettingsSpinner.setOnItemSelectedListener(this);
         mHiddenWarningView = mView.findViewById(R.id.hidden_settings_warning);

修改方式2:連接wifi時(shí)動(dòng)態(tài)重新設(shè)置

Index: SettingsProvider/res/values/defaults.xml
===================================================================
+   <!-- 將默認(rèn)值改為 0 (默認(rèn)禁用隨機(jī)化) -->
+   <integer name="config_wifi_default_mac_randomization">1</integer>
 </resources>
Index: SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
===================================================================
+            loadIntegerSetting(stmt, "config_wifi_default_mac_randomization", R.integer.config_wifi_default_mac_randomization);
             loadBooleanSetting(stmt, Settings.Global.DEVICE_PROVISIONED,
                     R.bool.def_device_provisioned);
 
Index: service/java/com/android/server/wifi/WifiConfigManager.java
===================================================================
    /**
     * Create a new internal WifiConfiguration object by copying over parameters from the provided
     * external configuration and set defaults for the appropriate parameters.
     *
     * @param externalConfig WifiConfiguration object provided from the external API.
     * @return New WifiConfiguration object with parameters merged from the provided external
     * configuration.
     */
    private WifiConfiguration createNewInternalWifiConfigurationFromExternal(
            WifiConfiguration externalConfig, int uid, @Nullable String packageName) {
        WifiConfiguration newInternalConfig = new WifiConfiguration();

        // First allocate a new network ID for the configuration.
        newInternalConfig.networkId = mNextNetworkId++;

        // First set defaults in the new configuration created.
        setDefaultsInWifiConfiguration(newInternalConfig);

        // Convert legacy fields to new security params
        externalConfig.convertLegacyFieldsToSecurityParamsIfNeeded();

        // Copy over all the public elements from the provided configuration.
        mergeWithInternalWifiConfiguration(newInternalConfig, externalConfig);

        // Copy over the hidden configuration parameters. These are the only parameters used by
        // system apps to indicate some property about the network being added.
        // These are only copied over for network additions and ignored for network updates.
        newInternalConfig.noInternetAccessExpected = externalConfig.noInternetAccessExpected;
        newInternalConfig.ephemeral = externalConfig.ephemeral;
        newInternalConfig.osu = externalConfig.osu;
        newInternalConfig.fromWifiNetworkSuggestion = externalConfig.fromWifiNetworkSuggestion;
        newInternalConfig.fromWifiNetworkSpecifier = externalConfig.fromWifiNetworkSpecifier;
        newInternalConfig.useExternalScores = externalConfig.useExternalScores;
        newInternalConfig.shared = externalConfig.shared;
        newInternalConfig.updateIdentifier = externalConfig.updateIdentifier;
        newInternalConfig.setPasspointUniqueId(externalConfig.getPasspointUniqueId());

        // Add debug information for network addition.
        newInternalConfig.creatorUid = newInternalConfig.lastUpdateUid = uid;
        newInternalConfig.creatorName = newInternalConfig.lastUpdateName =
                packageName != null ? packageName : mContext.getPackageManager().getNameForUid(uid);
        newInternalConfig.lastUpdated = mClock.getWallClockMillis();
        newInternalConfig.numRebootsSinceLastUse = 0;
         initRandomizedMacForInternalConfig(newInternalConfig);
+        boolean defaultMacAddress = Settings.Global.getInt(mContext.getContentResolver(),"config_wifi_default_mac_randomization", 0) == 0;
+       if (defaultMacAddress) {
+          newInternalConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE;
+       }
+        //Log.e(TAG, "createNewInternalWifiConfigurationFromExternal macRandomizationSetting="+newInternalConfig.macRandomizationSetting );
         return newInternalConfig;
     }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容