Android  Wifi的forget()操作实例详解

Android  Wifi的forget()操作实例详解

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比青山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式青山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖青山地区。费用合理售后完善,十余年实体公司更值得信赖。

我们在处理某个Wifi连接时,有时会需要忘掉当前连接的密码信息。执行这项操作,我们需要调用WifiManager::forget()函数:

/** 
 * 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)); 
} 

从函数介绍可知,调用forget()函数,当前网络连接的配置信息就会从wpa_supplicant.conf中删掉;之后这个网络就不会有自动重连的动作,因为conf文件中已经没有该网络的配置信息。

跟踪FORGET_NETWORK消息,WifiServiceImpl::ClientHandler处理:

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; 

简单地将该消息转发给WifiStateMachine。此时Wifi是连接状态,WifiStateMachine中当前状态是ConnectedState,它的父状态ConnectModeState处理:

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 dont want to remove the network from 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 { 
    loge("Failed to remove network " + netId); 
    return false; 
  } 
} 

根据传入的当前网络的netId,分别调用WifiNative的removeNetwork()、saveConfig()方法删除conf文件的配置信息并进行保存;执行完成后,forget()函数结束了。通过代码我们发现,执行forget()函数并不会引起WifiStateMachine中状态的切换。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


网页名称:Android  Wifi的forget()操作实例详解
标题路径:http://hbruida.cn/article/ijsgeo.html