[Android][Framework]设置默认WiFi配置

2020-01-20 16:56:55 浏览数 (1)

点击Wifi热点配置选项,会弹出一个对话框,WifiApDialog.java。这个对话框会在onCreate的时候填入一些默认内容:

代码语言:javascript复制
if (mWifiConfig != null) {
    mSsid.setText(mWifiConfig.SSID);
    if (mWifiConfig.apBand == 0) {
        mBandIndex = 0;
    } else {
        mBandIndex = 1;
    }

    mSecurity.setSelection(mSecurityTypeIndex);
    if (mSecurityTypeIndex == WPA2_INDEX) {
        mPassword.setText(mWifiConfig.preSharedKey);
    }
}

内容都是从mWifiConfig获取的,这个mWifiConfig是在构造的时候赋值:

代码语言:javascript复制
// TetherSettings.java

private void initWifiTethering() {
    final Activity activity = getActivity();
    mWifiConfig = mWifiManager.getWifiApConfiguration(); // 这里获取的
    mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);

    mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);

    mRestartWifiApAfterConfigChange = false;

    if (mWifiConfig == null) {
        final String s = activity.getString(
            com.android.internal.R.string.wifi_tether_configure_ssid_default);
        mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
                                                s, mSecurityType[WifiApDialog.OPEN_INDEX]));
    } else {
        int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
        mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT),
                                                mWifiConfig.SSID,
                                                mSecurityType[index]));
    }
}

@Override
public Dialog onCreateDialog(int id) {
    if (id == DIALOG_AP_SETTINGS) {
        final Activity activity = getActivity();
        mDialog = new WifiApDialog(activity, this, mWifiConfig);
        return mDialog;
    }
    return null;
}

继续跟代码

代码语言:javascript复制
// WifiServiceImpl.java
/**
 * see {@link WifiManager#getWifiApConfiguration()}
 * @return soft access point configuration
 */
public WifiConfiguration getWifiApConfiguration() {
    enforceAccessPermission();
    return mWifiStateMachine.syncGetWifiApConfiguration();
}
代码语言:javascript复制
// WifiStateMachine.java
public WifiConfiguration syncGetWifiApConfiguration() {
	return mWifiApConfigStore.getApConfiguration();
}
代码语言:javascript复制
// WifiApConfigStore.java
/**
 * Generate a default WPA2 based configuration with a random password.
 * We are changing the Wifi Ap configuration storage from secure settings to a
 * flat file accessible only by the system. A WPA2 based default configuration
 * will keep the device secure after the update.
 */
private WifiConfiguration getDefaultApConfiguration() {
    WifiConfiguration config = new WifiConfiguration();
    config.SSID = mContext.getResources().getString(
        R.string.wifi_tether_configure_ssid_default);
    config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
    String randomUUID = UUID.randomUUID().toString();
    //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
    config.preSharedKey = randomUUID.substring(0, 8)   randomUUID.substring(9, 13);
    return config;
}

所以,只需要在getDefaultApConfiguration配置默认的热点相关内容。

0 人点赞