WiFi scanning scenario WiFi scanning scenarios use different scanning strategies depending on the device's screen status, the user's current activity, and the network environment. 1. "Screen is on and in the WiFi settings interface": When the user enters the WiFi settings interface with the screen on, the device will perform a fixed scan, which usually takes 10 seconds. This scan is to quickly obtain a list of currently available WiFi networks for the user to choose to connect to. 2. "Screen is on and not in WiFi settings interface": In this case, the Android device will use a binary exponential backoff scanning strategy. The scanning interval will gradually increase according to a certain algorithm, with a minimum interval of 20 seconds and a maximum interval of up to 160 seconds. This strategy helps balance the energy consumption of the device and the efficiency of WiFi scanning. 3. "Screen off and saved networks": If the device is in the screen off state, but the WiFi network has been saved before, the device will scan according to the current network connection status. If it is already connected to a network, it will not scan; otherwise, the device will perform a PNO (Preferred Network Offload) scan, that is, only scan the saved networks. The minimum interval for this scan is 20 seconds and the maximum interval is 60 seconds. 4. No saved network: If no WiFi networks are saved, the device will perform a fixed scan, usually every 5 minutes. The purpose of this scan is to notify the user of the available open networks around. There are also some special scenarios of WiFi scanning, such as: 「Location change」: When a user moves their device to a new location, the device may automatically trigger a WiFi scan to find and connect to a new available network. 「Application request」: Some applications may need to request WiFi scanning in the background to obtain surrounding network information or perform other related operations. In this case, the Android system will decide whether to allow scanning based on the application's permissions and policies. The screen is on and in the WiFi settings interface When the device is in the on-screen state and in the WiFi settings interface, the WiFi scanning behavior will be more active and accurate. This is to provide a real-time, accurate list of available WiFi networks so that you can easily select and connect to the appropriate network. - 「Real-time Scan」: When you open the WiFi settings interface, the system will immediately start a WiFi scan. This scan is real-time and will immediately search and list the currently available WiFi networks.
- 「Continuous Refresh」: Once scanning starts, the system will continuously refresh the WiFi network list to ensure that the listed networks are up to date. This is achieved by periodically rescanning or listening to network change events.
- 「Network signal strength display」: In the WiFi settings interface, in addition to listing the network name (SSID), the system will also display the signal strength of each network. This helps to determine which network has a better signal, so as to make a more informed connection choice.
- 「Security Identification」: For each listed WiFi network, the system will also display its security information (such as open network, WPA2-PSK, etc.). This helps you understand the security of the network and decide whether to trust and connect to it.
- 「User interaction」: In the WiFi settings interface, you can perform various operations, such as clicking on a network to connect, entering a password, forgetting a network, etc. These operations will trigger corresponding system responses, such as starting the connection process, verifying the password, etc.
When the screen is on and the WiFi settings interface is displayed, the device will perform a fixed scan, which usually takes 10 seconds. This scan is to quickly obtain a list of currently available WiFi networks for the user to choose to connect to. WIFI_RESCAN_INTERVAL_MS is 10 * 1000 (10 seconds). //WifiTracker.java public void handleMessage(Message message) { if (message.what != MSG_SCAN) return; if (mWifiManager.startScan()) { mRetry = 0; } else if (++mRetry >= 3) { mRetry = 0; if (mContext != null) { Toast.makeText(mContext, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show(); } return; } sendEmptyMessageDelayed(MSG_SCAN, WIFI_RESCAN_INTERVAL_MS); } The screen is on but not in the WiFi settings interface When the device's screen is on but not in the WiFi settings interface, WiFi scanning behaves differently. In this case, the system manages WiFi scanning based on a series of policies and algorithms to balance device energy consumption, performance, and user experience. - Reduce Scan Frequency: When you are not in the WiFi settings interface, the system will generally reduce the frequency of WiFi scans compared to when you are in this interface. This is to reduce the device's energy consumption and avoid excessive network searches when they are not necessary.
- 「Binary exponential backoff scanning」: To further optimize scanning behavior, the Android system may adopt a binary exponential backoff scanning strategy. The interval between each scan will gradually increase, based on a backoff algorithm. The initial interval may be short, but as the number of consecutive scans increases, the interval will gradually increase. This strategy helps reduce the frequency of scanning while maintaining the ability to detect available networks.
- 「Application layer request」: Although the system reduces the frequency of automatic scanning, applications can still request WiFi scans in the background. For example, some applications may need to obtain surrounding network information to perform their functions. In this case, the system will evaluate the application's permissions and requests and perform scans as needed.
- 「Location and network change trigger」: If the device's location changes or a change in network status is detected (for example, switching from mobile data to WiFi), the system may automatically trigger a WiFi scan to ensure that the device can discover and connect to new available networks in a timely manner.
- 「User manually triggered」: Although the system reduces automatic scanning, WiFi scanning can still be triggered manually. For example, you can rescan available networks by pulling down the notification bar and clicking the "WiFi" icon.
When the screen is on and not in the WiFi settings interface, the scanning interval will gradually increase according to a certain algorithm. The minimum interval is 20 seconds and the maximum interval may reach 160 seconds. startConnectivityScan --> startPeriodicScan --> startPeriodicSingleScan //WifiConnectivityManager.java //扫描间隔定义// Periodic scan interval in milli-seconds. This is the scan // performed when screen is on. public static final int PERIODIC_SCAN_INTERVAL_MS = 20 * 1000; // 20 seconds // When screen is on and WiFi traffic is heavy, exponential backoff // connectivity scans are scheduled. This constant defines the maximum // scan interval in this scenario. @VisibleForTesting public static final int MAX_PERIODIC_SCAN_INTERVAL_MS = 160 * 1000; // 160 seconds private void startConnectivityScan(boolean scanImmediately) { // Always stop outstanding connecivity scan if there is any stopConnectivityScan(); if (mWifiState != WIFI_STATE_CONNECTED && mWifiState != WIFI_STATE_DISCONNECTED) { return; } if (mScreenOn) { startPeriodicScan(scanImmediately); } else { if (mWifiState == WIFI_STATE_DISCONNECTED && !mPnoScanStarted) { startDisconnectedPnoScan(); } } } private void startPeriodicScan(boolean scanImmediately) { mPnoScanListener.resetLowRssiNetworkRetryDelay(); if (scanImmediately) { resetLastPeriodicSingleScanTimeStamp(); } mPeriodicSingleScanInterval = PERIODIC_SCAN_INTERVAL_MS; startPeriodicSingleScan(); } // Start a single scan and set up the interval for next single scan. private void startPeriodicSingleScan() { long currentTimeStamp = mClock.getElapsedSinceBootMillis(); if (mLastPeriodicSingleScanTimeStamp != RESET_TIME_STAMP) { long msSinceLastScan = currentTimeStamp - mLastPeriodicSingleScanTimeStamp; if (msSinceLastScan < PERIODIC_SCAN_INTERVAL_MS) { localLog("Last periodic single scan started " + msSinceLastScan + "ms ago, defer this new scan request."); schedulePeriodicScanTimer(PERIODIC_SCAN_INTERVAL_MS - (int) msSinceLastScan); return; } if (isScanNeeded) { mLastPeriodicSingleScanTimeStamp = currentTimeStamp; startSingleScan(isFullBandScan, WIFI_WORK_SOURCE); schedulePeriodicScanTimer(mPeriodicSingleScanInterval); // Set up the next scan interval in an exponential backoff fashion. mPeriodicSingleScanInterval *= 2; if (mPeriodicSingleScanInterval > MAX_PERIODIC_SCAN_INTERVAL_MS) { mPeriodicSingleScanInterval = MAX_PERIODIC_SCAN_INTERVAL_MS; } } else { // Since we already skipped this scan, keep the same scan interval for next scan. schedulePeriodicScanTimer(mPeriodicSingleScanInterval); } } } Screen off and saved network When the device is in the off state and there are saved WiFi networks, the WiFi scanning behavior will show a specific pattern. The system will manage WiFi connections in an efficient and energy-saving way based on previous connection records and network status. - "Continuous monitoring of connected networks": If the device is currently connected to a saved WiFi network, the system will continue to monitor the connection status of the network. As long as the connection is stable and the signal is good, the device will generally not perform additional WiFi scans.
- 「PNO scan for unconnected networks」: If the device is not connected to any saved WiFi network, a PNO (Preferred Network Offload) scan will be performed. The scan is only performed on the saved network list instead of scanning the entire available network. It helps save device power consumption while ensuring that you can quickly connect to known, trusted networks.
- "Scan interval optimization": In order to balance the accuracy of the scan and the energy consumption of the device, the PNO scan interval will be optimized. Typically, the minimum scan interval may be set to 20 seconds, while the maximum interval may be adjusted according to network conditions and device policies, usually not more than a few minutes. This ensures that the device can promptly discover and connect to the saved network when needed, while not over-consuming battery power when not needed.
- "Response to user location and network changes": If you move to a new location while the screen is off, or the network status changes (for example, other saved networks become available), the system may adjust the scanning strategy based on these changes. This includes shortening the scanning interval or triggering additional scans to ensure that the device can respond to these changes in a timely manner.
- 「System notifications and reminders」: When the device finds that a saved WiFi network has become available, it may inform the user through a system notification or reminder. The user can choose to manually connect to the network or set the device to automatically connect the next time the screen turns on.
For networks with the screen off and saved, the minimum interval min=20s, the maximum interval max=20s*3=60s, which may vary for different Android versions. No saved network When the device is in a state of no saved network, the WiFi scanning behavior will show a specific pattern. In this case, the device has no pre-saved WiFi network information and needs to scan to find an available network. - 「Periodic Scan」: Since there is no saved network information, the device will periodically perform WiFi scans to search and discover available networks. Scanning is usually performed when the device is in the screen-on state, and the frequency of scanning may be affected by system policies and device settings.
- Scan interval: When there is no saved network, the scan interval may be relatively long to reduce the energy consumption of the device. The specific scan interval varies depending on the device and system version, and is usually set to several minutes or longer.
- 「User interaction trigger」: In addition to regular scanning, WiFi scanning can also be triggered by some interactive operations. For example, you can manually turn on the WiFi switch in the settings menu, or trigger a scan by pulling down the notification bar and clicking the WiFi icon.
- 「Network selection prompt」: When the device finds an available WiFi network, the system usually displays a notification or prompt to inform that a new network is available. You can click the notification to view the network list and select the network you want to connect to.
- "Security and signal strength considerations": When choosing a network to connect to, the security and signal strength of the network are usually considered. The system will display the security information of each network (such as open network, WPA2-PSK, etc.) and the signal strength indicator to help users make more informed connection decisions.
//WifiStateMachine.java class DisconnectedState extends State { @Override public void enter() { Log.i(TAG, "disconnectedstate enter"); // We dont scan frequently if this is a temporary disconnect // due to p2p if (mTemporarilyDisconnectWifi) { p2pSendMessage(WifiP2pServiceImpl.DISCONNECT_WIFI_RESPONSE); return; } /** clear the roaming state, if we were roaming, we failed */ mIsAutoRoaming = false; mWifiConnectivityManager.handleConnectionStateChanged( WifiConnectivityManager.WIFI_STATE_DISCONNECTED); /** * If we have no networks saved, the supplicant stops doing the periodic scan. * The scans are useful to notify the user of the presence of an open network. * Note that these are not wake up scans. */ if (mNoNetworksPeriodicScan != 0 && !mP2pConnected.get() && mWifiConfigManager.getSavedNetworks().size() == 0) { sendMessageDelayed(obtainMessage(CMD_NO_NETWORKS_PERIODIC_SCAN, ++mPeriodicScanToken, 0), mNoNetworksPeriodicScan); } mDisconnectedTimeStamp = mClock.getWallClockMillis(); mWifiStateTracker.updateState(WifiStateTracker.DISCONNECTED); } } //其中扫描周期mNoNetworksPeriodicScan = mContext.getResources().getInteger(R.integer.config_wifi_no_network_periodic_scan_interval); frameworks/base/core/res/res/values/config.xmlmNoNetworksPeriodicScan is registered in config.xml with a period of 5 minutes <!-- Integer indicating the framework no networks periodic scan interval in milliseconds. --> <integer translatable="false" name="config_wifi_no_network_periodic_scan_interval">300000</integer> To optimize energy consumption and user experience, the Android system may use some energy-saving strategies to limit the frequency and duration of WiFi scans. For example, when the device is in the off state, the system may reduce or suspend WiFi scans to extend battery life. When the device is low on power or in energy-saving mode, the system may also limit WiFi scan behavior. |