English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Detailed Explanation of Android Wifi's forget() Operation
When handling a certain Wifi connection, we sometimes need to forget the current connection password information. To perform this operation, we need to call the WifiManager::forget() function:
/** * Delete the network in the supplicant config. * * This function is used instead of a sequence of removeNetwork() * and saveConfiguration(). * * @param config the set of variables that describe the configuration, * contained in a {@link WifiConfiguration} object. * @param listener for callbacks on success or failure. Can be null. * @throws IllegalStateException if the WifiManager instance needs to be * initialized again * @hide */ public void forget(int netId, ActionListener listener) { if (netId < 0) throw new IllegalArgumentException("Network id cannot be negative"); validateChannel(); sAsyncChannel.sendMessage(FORGET_NETWORK, netId, putListener(listener)); }
From the function description, calling the forget() function will delete the configuration information of the current network connection from wpa_supplicant.conf; after that, this network will not have the action of automatic reconnection because there is no configuration information of this network in the conf file.
Track the FORGET_NETWORK message, the ClientHandler in WifiServiceImpl handles:
case WifiManager.FORGET_NETWORK: if (isOwner(msg.sendingUid)) { mWifiStateMachine.sendMessage(Message.obtain(msg)); } else { Slog.e(TAG, "Forget is not authorized for user"); replyFailed(msg, WifiManager.FORGET_NETWORK_FAILED, WifiManager.NOT_AUTHORIZED); } break;
Simply forward the message to WifiStateMachine. At this time, the Wi-Fi is connected, the current state in WifiStateMachine is ConnectedState, and its parent state ConnectModeState handles:
case WifiManager.FORGET_NETWORK: // Debug only, remember last configuration that was forgotten WifiConfiguration toRemove = mWifiConfigStore.getWifiConfiguration(message.arg1); if (toRemove == null) { lastForgetConfigurationAttempt = null; } else { lastForgetConfigurationAttempt = new WifiConfiguration(toRemove); } // check that the caller owns this network netId = message.arg1; if (!mWifiConfigStore.canModifyNetwork(message.sendingUid, netId, /* onlyAnnotate */ false)) { logw("Not authorized to forget network " + " cnid=" + netId + " uid=" + message.sendingUid); replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED, WifiManager.NOT_AUTHORIZED); break; } if (mWifiConfigStore.forgetNetwork(message.arg1)) { replyToMessage(message, WifiManager.FORGET_NETWORK_SUCCEEDED); broadcastWifiCredentialChanged(WifiManager.WIFI_CREDENTIAL_FORGOT, (WifiConfiguration) message.obj); } else { loge("Failed to forget network"); replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED, WifiManager.ERROR); } break;
mWifiConfigStore.forgetNetwork():
/** * Forget the specified network and save config * * @param netId network to forget * @return {@code true} if it succeeds, {@code false} otherwise */ boolean forgetNetwork(int netId) { if (showNetworks) localLog("forgetNetwork", netId); WifiConfiguration config = mConfiguredNetworks.get(netId); boolean remove = removeConfigAndSendBroadcastIfNeeded(netId); if (!remove) { //success but we don't want to remove the network from the supplicant conf file return true; } if (mWifiNative.removeNetwork(netId)) { if (config != null && config.isPasspoint()) { writePasspointConfigs(config.FQDN, null); } mWifiNative.saveConfig(); writeKnownNetworkHistory(true); return true; } else { log("Failed to remove network ") + netId); return false; } }
According to the current network netId passed in, the removeNetwork() and saveConfig() methods of WifiNative are called respectively to delete the configuration information of the conf file and save it; after the execution is completed, the forget() function ends. Through the code, we find that executing the forget() function does not cause a state change in the WifiStateMachine.
Thank you for reading, and I hope it can help everyone. Thank you for your support of this site!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)