修改热点配置会回调WifiTetherSettings.java里的
onTetherConfigUpdated()方法,在这个方法里会调用stopTether().
耗时主要体现在关闭热点之前调用了getWifiApState方法
211 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
212 Log.d("TetheringSettings",
213 "Wifi AP config changed while enabled, stop and restart");
214 mRestartWifiApAfterConfigChange = true;
215 mSwitchBarController.stopTether();
216 }
关闭Hotspot耗时的原因在WifiserviceImpl中,Q上getWifiApEnabledState函数会用1000ms,P上只用20ms。
P的代码
/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
mClientHandler.runWithScissors(() -> {
Q的代码
/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
mWifiInjector.getClientModeImplHandler().runWithScissors(() -> {
经过本地验证,在Q上使用P的写法可以解决关闭热点耗时的问题。
P 和 Q的差异点 :
P上mClientHandler 是工作在 wifiServiceHandlerThread 线程中
Q上 此命令处理是在 ClientModeImpl 线程中(mWifiCoreHandlerThread) 相当于P上的 mWifiStateMachineHandlerThread
由于 在热点开启/关闭过程中,ClientModeImpl 线程(mWifiCoreHandlerThread)比较忙,从而导致getWifiApEnabledState 处理比较靠后。
比较忙的原因是 SoftAP 正在启动/关闭
同时,开关热点和wifi均会调用getWifiApEnabledState导致它们开关缓慢,但是这也要考虑手机性能,一般低性能情况下会出现这个问题。