SoFunction
Updated on 2025-04-07

Android   Detailed explanation of the forget operation example of Wifi

Detailed explanation of the forget() operation example of Android Wifi

When we are dealing with a Wifi connection, we sometimes need to forget the password information of the current connection. To do this, 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(); 
  (FORGET_NETWORK, netId, putListener(listener)); 
} 

From the function introduction, we can see that when calling the forget() function, the configuration information of the current network connection will be deleted from wpa_supplicant.conf; after that, the network will not have automatic reconnection, because the configuration information of the network is no longer in the conf file.

Tracking FORGET_NETWORK messages, WifiServiceImpl::ClientHandler handles:

case WifiManager.FORGET_NETWORK: 
  if (isOwner()) { 
    ((msg)); 
  } else { 
    (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, Wifi is the connection state. 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 
      = (message.arg1); 
  if (toRemove == null) { 
    lastForgetConfigurationAttempt = null; 
  } else { 
    lastForgetConfigurationAttempt = new WifiConfiguration(toRemove); 
  } 
  // check that the caller owns this network 
  netId = message.arg1; 
 
  if (!(, netId, 
      /* onlyAnnotate */ false)) { 
    logw("Not authorized to forget network " 
       + " cn uFailed to forget network"); 
    replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED, 
        ); 
  } 
  break; 

():

/** 
 * 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 = (netId); 
  boolean remove = removeConfigAndSendBroadcastIfNeeded(netId); 
  if (!remove) { 
    //success but we dont want to remove the network from supplicant conf file 
    return true; 
  } 
  if ((netId)) { 
    if (config != null && ()) { 
      writePasspointConfigs(, null); 
    } 
    (); 
    writeKnownNetworkHistory(true); 
    return true; 
  } else { 
    loge("Failed to remove network " + netId); 
    return false; 
  } 
} 

According to the netId of the incoming network, the WifiNative removeNetwork() and saveConfig() methods are called 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 found that executing the forget() function does not cause the state switch in WifiStateMachine.

Thank you for reading, I hope it can help you. Thank you for your support for this site!