wifi enable状态切换的上层调用流程(wifi ap enable == true时,此时手机充当的是热点)
set ap disable == success : set wifi enable
set ap disable与set ap enable 过程差不多,这里分析set ap enable的过程。
set ap enable - > setupDriverForSoftAp ? transitionTo(mSoftApState) : transitionTo(mInitialState);
mSoftApState - > enter()
checkAndSetConnectivityInstance();
mSoftApManager = mFacade.makeSoftApManager(
mContext, getHandler().getLooper(), mWifiNative, mNwService,
mCm, mCountryCode.getCurrentCountryCode(),
mWifiApConfigStore.getAllowed2GChannel(),
new SoftApListener());
mSoftApManager.start(config);
softApmanager -> IdleState -> (CMD_START)
更新AP状态为:WifiManager.WIFI_AP_STATE_ENABLING
updateApState(WifiManager.WIFI_AP_STATE_ENABLING, 0);
开始启动AP
int result = startSoftAp((WifiConfiguration) message.obj);
result == SUCCESS ?
如果启动成功则更新状态为WifiManager.WIFI_AP_STATE_ENABLED,并且状态机切换为mStartedState。
updateApState(WifiManager.WIFI_AP_STATE_ENABLED, 0);
transitionTo(mStartedState);
:
如果启动失败,则状态机不变,更新AP状态为WifiManager.WIFI_AP_STATE_FAILED
int reason = WifiManager.SAP_START_FAILURE_GENERAL;
if (result == ERROR_NO_CHANNEL) {
reason = WifiManager.SAP_START_FAILURE_NO_CHANNEL;
}
updateApState(WifiManager.WIFI_AP_STATE_FAILED, reason);
WifiStateMachine当前的状态机SoftApState通过SoftApListener回调接口,接受到启动AP失败,
根据失败原因:
state == WIFI_AP_STATE_DISABLED:
sendMessage(CMD_AP_STOPPED);
case CMD_AP_STOPPED:
transitionTo(mInitialState);
------------------------------------------
state == WIFI_AP_STATE_FAILED
sendMessage(CMD_START_AP_FAILURE);
case CMD_START_AP_FAILURE:
transitionTo(mInitialState);
------------------------------------------
最终调用setWifiApState修改状态:
setWifiApState(state, reason);
//更新状态信息
// Update state
mWifiApState.set(wifiApState);
setWifiApState方法将会发送一个粘性广播WIFI_AP_STATE_CHANGED_ACTION通知其他组件AP状态发生了变化。
如果set Ap disable == success的话,紧接着wifienabler 会调用setWifiEnabled
setWifiEnabled :
mWifiController发送CMD_WIFI_TOGGLED事件 -> 承接上处,mWifiController当前状态机应该处于StaEnabledState
StaEnabledState enter:
mWifiStateMachine.setSupplicantRunning(true);
设置wifi状态为setWifiState(WIFI_STATE_ENABLING);
WifiStateMachine状态机InitialState切换为mSupplicantStartingState
加入底层启动supplicant成功:mSupplicantStartingState将接受到来自底层的事件WifiMonitor.SUP_CONNECTION_EVENT
此时更新wifi 状态为setWifiState(WIFI_STATE_ENABLED);
发送广播通知supplicant状态更新了:
sendSupplicantConnectionChangedBroadcast(true);
并且将状态机切换为mDriverStartedState :
transitionTo(mDriverStartedState); 此时状态机也可能直接进入到mScanModeState
-------------------------------------------------------------------------------------------------------
/* Wifi state machine modes of operation */
/* CONNECT_MODE - connect to any 'known' AP when it becomes available */
public static final int CONNECT_MODE = 1; //此状态说明sta可以扫描AP,也可以链接AP
/* SCAN_ONLY_MODE - don't connect to any APs; scan, but only while apps hold lock */
public static final int SCAN_ONLY_MODE = 2; //此状态说明sta只能扫描AP
/* SCAN_ONLY_WITH_WIFI_OFF - scan, but don't connect to any APs */
public static final int SCAN_ONLY_WITH_WIFI_OFF_MODE = 3; //此状态说明sta 在off的时候也能扫描AP
set ap disable == success : set wifi enable
set ap disable与set ap enable 过程差不多,这里分析set ap enable的过程。
set ap enable - > setupDriverForSoftAp ? transitionTo(mSoftApState) : transitionTo(mInitialState);
mSoftApState - > enter()
checkAndSetConnectivityInstance();
mSoftApManager = mFacade.makeSoftApManager(
mContext, getHandler().getLooper(), mWifiNative, mNwService,
mCm, mCountryCode.getCurrentCountryCode(),
mWifiApConfigStore.getAllowed2GChannel(),
new SoftApListener());
mSoftApManager.start(config);
softApmanager -> IdleState -> (CMD_START)
更新AP状态为:WifiManager.WIFI_AP_STATE_ENABLING
updateApState(WifiManager.WIFI_AP_STATE_ENABLING, 0);
开始启动AP
int result = startSoftAp((WifiConfiguration) message.obj);
result == SUCCESS ?
如果启动成功则更新状态为WifiManager.WIFI_AP_STATE_ENABLED,并且状态机切换为mStartedState。
updateApState(WifiManager.WIFI_AP_STATE_ENABLED, 0);
transitionTo(mStartedState);
:
如果启动失败,则状态机不变,更新AP状态为WifiManager.WIFI_AP_STATE_FAILED
int reason = WifiManager.SAP_START_FAILURE_GENERAL;
if (result == ERROR_NO_CHANNEL) {
reason = WifiManager.SAP_START_FAILURE_NO_CHANNEL;
}
updateApState(WifiManager.WIFI_AP_STATE_FAILED, reason);
WifiStateMachine当前的状态机SoftApState通过SoftApListener回调接口,接受到启动AP失败,
根据失败原因:
state == WIFI_AP_STATE_DISABLED:
sendMessage(CMD_AP_STOPPED);
case CMD_AP_STOPPED:
transitionTo(mInitialState);
------------------------------------------
state == WIFI_AP_STATE_FAILED
sendMessage(CMD_START_AP_FAILURE);
case CMD_START_AP_FAILURE:
transitionTo(mInitialState);
------------------------------------------
最终调用setWifiApState修改状态:
setWifiApState(state, reason);
//更新状态信息
// Update state
mWifiApState.set(wifiApState);
setWifiApState方法将会发送一个粘性广播WIFI_AP_STATE_CHANGED_ACTION通知其他组件AP状态发生了变化。
如果set Ap disable == success的话,紧接着wifienabler 会调用setWifiEnabled
setWifiEnabled :
mWifiController发送CMD_WIFI_TOGGLED事件 -> 承接上处,mWifiController当前状态机应该处于StaEnabledState
StaEnabledState enter:
mWifiStateMachine.setSupplicantRunning(true);
设置wifi状态为setWifiState(WIFI_STATE_ENABLING);
WifiStateMachine状态机InitialState切换为mSupplicantStartingState
加入底层启动supplicant成功:mSupplicantStartingState将接受到来自底层的事件WifiMonitor.SUP_CONNECTION_EVENT
此时更新wifi 状态为setWifiState(WIFI_STATE_ENABLED);
发送广播通知supplicant状态更新了:
sendSupplicantConnectionChangedBroadcast(true);
并且将状态机切换为mDriverStartedState :
transitionTo(mDriverStartedState); 此时状态机也可能直接进入到mScanModeState
-------------------------------------------------------------------------------------------------------
/* Wifi state machine modes of operation */
/* CONNECT_MODE - connect to any 'known' AP when it becomes available */
public static final int CONNECT_MODE = 1; //此状态说明sta可以扫描AP,也可以链接AP
/* SCAN_ONLY_MODE - don't connect to any APs; scan, but only while apps hold lock */
public static final int SCAN_ONLY_MODE = 2; //此状态说明sta只能扫描AP
/* SCAN_ONLY_WITH_WIFI_OFF - scan, but don't connect to any APs */
public static final int SCAN_ONLY_WITH_WIFI_OFF_MODE = 3; //此状态说明sta 在off的时候也能扫描AP