欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WiFi关闭流程

程序员文章站 2022-06-17 10:06:53
/android/packages/apps/Settings/src/com/android/settings/wifi/WifiEnabler.java:public boolean onSwitchToggled(boolean isChecked) {mWifiManager.setWifiEnabled(isChecked)}/android/frameworks/base/wifi/java/android/net/wifi/WifiManager.java:public bool...

/android/packages/apps/Settings/src/com/android/settings/wifi/WifiEnabler.java:

public boolean onSwitchToggled(boolean isChecked) {
	mWifiManager.setWifiEnabled(isChecked)
}

/android/frameworks/base/wifi/java/android/net/wifi/WifiManager.java:

public boolean setWifiEnabled(boolean enabled) {
	return mService.setWifiEnabled(mContext.getOpPackageName(), enabled);
}

通过AIDL调用到:
/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java:

public synchronized boolean setWifiEnabled2(String packageName, int staId, boolean enable) {
	if (staId == STA_PRIMARY) {
           mWifiController.sendMessage(CMD_WIFI_TOGGLED);//发送CDM_WIFI_TOGGLED消息
       } 
}

/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiController.java:
/WifiController收到广播,在Wifi开启的时候,WifiController停留在mStaEnabledState/

class StaEnabledState extends State {
	case CMD_WIFI_TOGGLED:
		//isWifiToggleEnabled()为false,表示要关闭WiFi
		if (! mSettingsStore.isWifiToggleEnabled()) {
			transitionTo(mStaDisabledState);//切换至StaDisabledState
		}

/进入StaDisabledState状态后,首先进入enter()方法/

class StaDisabledState extends State {
	public void enter() {
		mActiveModeWarden.disableWifi();
	}
}

到ActiveModeWarden类的内部类ModeStateMachine(状态机)。
/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/ActiveModeWarden.java:

public void disableWifi() {
       changeMode(ModeStateMachine.CMD_DISABLE_WIFI);
}
private void changeMode(int newMode) {
       mModeStateMachine.sendMessage(newMode);
}//最后发送了CMD_DISABLE_WIFI这个消息。

在这个方法内处理消息

private boolean checkForAndHandleModeChange(Message message) {
	case ModeStateMachine.CMD_DISABLE_WIFI:
		/ModeStateMachine在Wifi开启的时候,处于mClientModeActiveState状态
		mModeStateMachine.transitionTo(mWifiDisabledState);
}

在切换状态时,调用mClientModeActiveState的exit()方法,在exit()方法中调用的ClientModeManager的stop()方法,更新wifi状态从Enabled->Disabling。
/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeManager.java:

public void stop() {
	if (mIfaceIsUp) {
		updateWifiState(WifiManager.WIFI_STATE_DISABLING,WifiManager.WIFI_STATE_ENABLED);
	}
	mStateMachine.quitNow();
}

//在WIFI开启的时候,ClientModeStateMachine处于StartedState状态

在状态机退出的时候会执行其exit()方法。

private class StartedState extends State {
	public void exit() {
		mClientModeImpl.setOperationalMode(ClientModeImpl.DISABLED_MODE, null);//使切换状态
		mWifiNative.teardownInterface(mClientInterfaceName);//底层进行关闭处理
		//更新wifi状态从Disabling->Disabled
		updateWifiState(WifiManager.WIFI_STATE_DISABLED,WifiManager.WIFI_STATE_DISABLING);
		 mStateMachine.quitNow();
	}
}

/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeImpl.java:

public void setOperationalMode(int mode, String ifaceName) {
	 if (mode != CONNECT_MODE) {
		// we are disabling client mode...   need to exit connect mode now
		transitionTo(mDefaultState);
	}
}

切换状态至初始状态mDefaultState。这个状态机在已连接中,处于mConnectModeState状态,所以当切换状态之前需要执行mConnectModeState的exit()方法。

class ConnectModeState extends State {
	public void exit() {
		 // Inform WifiConnectivityManager that Wifi is disabled
		mWifiConnectivityManager.setWifiEnabled(false);
		mWifiNative.removeAllNetworks(mInterfaceName);//关闭后是直接关闭或者进入仅扫描的状态,分两种。
		stopClientMode();//清理状态
	}
}

WiFi状态发生变化会调用/android/frameworks/opt/net/wifi/service/java/com/android/server/wifi/ActiveModeWarden.java的

class ClientModeActiveState extends ModeActiveState {
	public void onStateChanged(int state) {
		else if (state == WifiManager.WIFI_STATE_DISABLED) {
			 // client mode stopped
			mModeStateMachine.sendMessage(CMD_CLIENT_MODE_STOPPED, this);
		}
	}
}
case CMD_CLIENT_MODE_STOPPED:
	// notify WifiController that ClientMode stopped
	 mClientModeCallback.onStateChanged(WifiManager.WIFI_STATE_DISABLED);
	 mModeStateMachine.transitionTo(mWifiDisabledState);

本文地址:https://blog.csdn.net/weixin_43159275/article/details/110493471

相关标签: Android