Android P wifi和p2p启动流程

wifisettings部分就不说了,从wificontroller开始:

1.WifiController: 接收到CMD_WIFI_TOGGLED,调用transitionTo(mDeviceActiveState)切换状态->DeviceActiveState::mWifiStateMachinePrime.enterClientMode()

2.WifiStateMachinePrime: changeMode(ModeStateMachine.CMD_START_CLIENT_MODE)->ModeStateMachine::ClientModeActiveState::enter::mManager.start()

3.ClientModeManager: mStateMachine.sendMessage(ClientModeStateMachine.CMD_START)->ClientModeStateMachine::IdleState::mWifiNative.setupInterfaceForClientMode(这里做了①startHal②startSupplicant③createStaIface创建wlan0接口④mWifiMonitor.startMonitoring)->transitionTo(mStartedState)->StartedState::processMessage::CMD_INTERFACE_STATUS_CHANGED::onUpChanged(isUp)::mWifiStateMachine.setOperationalMode(WifiStateMachine.CONNECT_MODE, mClientInterfaceName)->updateWifiState(WifiManager.WIFI_STATE_ENABLED, WifiManager.WIFI_STATE_ENABLING)在这里发送wifi已经enabled的广播

4.WifiStateMachine: transitionTo(mDisconnectedState)->先进入父状态ConnectModeState::enter::setupClientMode::p2pSendMessage(WifiStateMachine.CMD_ENABLE_P2P)

接下来是p2p部分
wifip2pservice跟wifiservice一样是随系统服务启动的SystemServer: mSystemServiceManager.startService(WIFI_P2P_SERVICE_CLASS)
在WifiP2pServiceImpl的构造函数中会启动一个P2pStateMachine,在P2pStateMachine构造函数里初始状态setInitialState(mP2pDisabledState),同时注册了监听wifi开关状态的广播,当获取到wifi状态为WifiManager.WIFI_STATE_ENABLED时做了两件事:
1.checkAndReEnableP2p->sendMessage(ENABLE_P2P)->P2pStateMachine::P2pDisabledState::mWifiNative.setupInterface(这里调用mIWifiP2pIface = mHalDeviceManager.createP2pIface(mInterfaceDestroyedListener, handler)创建p2p0接口)->mNwService.setInterfaceUp(mInterfaceName)将这个接口拉起来->registerForWifiMonitorEvents这里是调用mWifiMonitor.startMonitoring(mInterfaceName)->transitionTo(mInactiveState)然后切换到InactiveState等待后续消息处理
2.checkAndSendP2pStateChangedBroadcast->sendP2pStateChangedBroadcast(mIsWifiEnabled && mIsInterfaceAvailable),第一步里p2p使能接口可用后发送WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION广播并携带WifiP2pManager.WIFI_P2P_STATE_ENABLED状态。

你可能感兴趣的:(wifi)