packages/apps/Settings/src/com/android/settings/wifi/tether/WifiTetherApBandPreferenceController.java
这个类在开启热点的界面负责设置热点的频段。
在解决一个热点的问题时,发现设置6G热点的时候明明设置的值为5 ,在保存到xml文件的时候就变为了7. 导致获取频段值时没有对应上。
在开启热点的界面里,选择频段时。会先将热点的配置保存在一个xml文件里面。开启热点时,会直接拿取配置文件的值进行开启热点。
这个配置文件在手机的 data/misc/apexdata/com.android.wifi/ 下。里面有两个配置文件,一个是wifi的,一个是热点的。
设置的方法为 wifiManager.setSoftApConfiguration()
这个的实现是在packages/modules/Wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
@Override
public boolean setSoftApConfiguration(
@NonNull SoftApConfiguration softApConfig, @NonNull String packageName) {
int uid = Binder.getCallingUid();
mWifiPermissionsUtil.checkPackage(uid, packageName);
boolean privileged = mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)
&& !privileged) {
// random apps should not be allowed to read the user specified config
throw new SecurityException("App not allowed to read or update stored WiFi Ap config "
+ "(uid = " + uid + ")");
}
mLog.info("setSoftApConfiguration uid=%").c(uid).flush();
if (softApConfig == null) return false;
if (WifiApConfigStore.validateApWifiConfiguration(softApConfig, privileged, mContext)) {
mWifiApConfigStore.setApConfiguration(softApConfig);
// Send the message for AP config update after the save is done.
mActiveModeWarden.updateSoftApConfiguration(softApConfig);
return true;
} else {
Log.e(TAG, "Invalid SoftAp Configuration");
return false;
}
}
看这个 mWifiApConfigStore.setApConfiguration(softApConfig);
packages/modules/Wifi/service/java/com/android/server/wifi/WifiApConfigStore.java
public synchronized void setApConfiguration(SoftApConfiguration config) {
SoftApConfiguration newConfig = config == null ? getDefaultApConfiguration()
: new SoftApConfiguration.Builder(sanitizePersistentApConfig(config))
.setUserConfiguration(true).build();
persistConfigAndTriggerBackupManagerProxy(
updatePersistentRandomizedMacAddress(newConfig));
}
关注 sanitizePersistentApConfig(config)
private SoftApConfiguration sanitizePersistentApConfig(SoftApConfiguration config) {
SoftApConfiguration.Builder convertedConfigBuilder =
new SoftApConfiguration.Builder(config);
int[] bands = config.getBands();
SparseIntArray newChannels = new SparseIntArray();
// The bands length should always 1 in R. Adding SdkLevel.isAtLeastS for lint check only.
for (int i = 0; i < bands.length; i++) {
int channel = SdkLevel.isAtLeastS()
? config.getChannels().valueAt(i) : config.getChannel();
int newBand = bands[i];
if (channel == 0 && mIsAutoAppendLowerBandEnabled
&& ApConfigUtil.isBandSupported(newBand, mContext)) {
// some countries are unable to support 5GHz only operation, always allow for 2GHz
// when config doesn't force channel
if ((newBand & SoftApConfiguration.BAND_2GHZ) == 0) {
newBand = ApConfigUtil.append24GToBandIf24GSupported(newBand, mContext);
}
// If the 6G configuration doesn't includes 5G band (2.4G have appended because
// countries reason), it will cause that driver can't switch channel from 6G to
// 5G/2.4G when coexistence happened (For instance: wifi connected to 2.4G or 5G
// channel). Always append 5G into band configuration when configured band includes
// 6G.
if ((newBand & SoftApConfiguration.BAND_6GHZ) != 0
&& (newBand & SoftApConfiguration.BAND_5GHZ) == 0) {
newBand = ApConfigUtil.append5GToBandIf5GSupported(newBand, mContext);
}
}
newChannels.put(newBand, channel);
}
if (SdkLevel.isAtLeastS()) {
convertedConfigBuilder.setChannels(newChannels);
} else if (bands.length > 0 && newChannels.valueAt(0) == 0) {
convertedConfigBuilder.setBand(newChannels.keyAt(0));
}
return convertedConfigBuilder.build();
看这个
if ((newBand & SoftApConfiguration.BAND_6GHZ) != 0
&& (newBand & SoftApConfiguration.BAND_5GHZ) == 0) {
newBand = ApConfigUtil.append5GToBandIf5GSupported(newBand, mContext);
}
packages/modules/Wifi/service/java/com/android/server/wifi/util/ApConfigUtil.java
public static @BandType int append5GToBandIf5GSupported(@BandType int targetBand,
Context context) {
if (isBandSupported(SoftApConfiguration.BAND_5GHZ, context)) {
return targetBand | SoftApConfiguration.BAND_5GHZ;
}
return targetBand;
}
这个方法里面会将传入的5 加上 2 就变为了 7
至于原因上面也做出了解释。
如果6G配置不包括5G频段(由于国家原因,附加了2.4G),则会导致共存时驱动器无法将信道从6G切换到5G/2.4G(例如:连接到2.4G或5G信道的wifi)。当配置的频段包括6G时,始终将5G附加到频段配置中。
觉得有用的话就点个赞吧!!谢谢大家。