Android -- Wifi的forget()操作

Android -- Wifi的forget()操作


我们在处理某个Wifi连接时,有时会需要忘掉当前连接的密码信息。执行这项操作,我们需要调用WifiManager::forget()函数:
  1. /** 
  2.  * Delete the network in the supplicant config. 
  3.  * 
  4.  * This function is used instead of a sequence of removeNetwork() 
  5.  * and saveConfiguration(). 
  6.  * 
  7.  * @param config the set of variables that describe the configuration, 
  8.  *            contained in a {@link WifiConfiguration} object. 
  9.  * @param listener for callbacks on success or failure. Can be null. 
  10.  * @throws IllegalStateException if the WifiManager instance needs to be 
  11.  * initialized again 
  12.  * @hide 
  13.  */  
  14. public void forget(int netId, ActionListener listener) {  
  15.     if (netId < 0throw new IllegalArgumentException("Network id cannot be negative");  
  16.     validateChannel();  
  17.     sAsyncChannel.sendMessage(FORGET_NETWORK, netId, putListener(listener));  
  18. }  
从函数介绍可知,调用forget()函数,当前网络连接的配置信息就会从wpa_supplicant.conf中删掉;之后这个网络就不会有自动重连的动作,因为conf文件中已经没有该网络的配置信息。
跟踪FORGET_NETWORK消息,WifiServiceImpl::ClientHandler处理:
  1. case WifiManager.FORGET_NETWORK:  
  2.     if (isOwner(msg.sendingUid)) {  
  3.         mWifiStateMachine.sendMessage(Message.obtain(msg));  
  4.     } else {  
  5.         Slog.e(TAG, "Forget is not authorized for user");  
  6.         replyFailed(msg, WifiManager.FORGET_NETWORK_FAILED,  
  7.                 WifiManager.NOT_AUTHORIZED);  
  8.     }  
  9.     break;  
简单地将该消息转发给WifiStateMachine。此时Wifi是连接状态,WifiStateMachine中当前状态是ConnectedState,它的父状态ConnectModeState处理:
  1. case WifiManager.FORGET_NETWORK:  
  2.     // Debug only, remember last configuration that was forgotten  
  3.     WifiConfiguration toRemove  
  4.             = mWifiConfigStore.getWifiConfiguration(message.arg1);  
  5.     if (toRemove == null) {  
  6.         lastForgetConfigurationAttempt = null;  
  7.     } else {  
  8.         lastForgetConfigurationAttempt = new WifiConfiguration(toRemove);  
  9.     }  
  10.     // check that the caller owns this network  
  11.     netId = message.arg1;  
  12.   
  13.     if (!mWifiConfigStore.canModifyNetwork(message.sendingUid, netId,  
  14.             /* onlyAnnotate */ false)) {  
  15.         logw("Not authorized to forget network "  
  16.              + " cnid=" + netId  
  17.              + " uid=" + message.sendingUid);  
  18.         replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED,  
  19.                 WifiManager.NOT_AUTHORIZED);  
  20.         break;  
  21.     }  
  22.   
  23.     if (mWifiConfigStore.forgetNetwork(message.arg1)) {  
  24.         replyToMessage(message, WifiManager.FORGET_NETWORK_SUCCEEDED);  
  25.         broadcastWifiCredentialChanged(WifiManager.WIFI_CREDENTIAL_FORGOT,  
  26.                 (WifiConfiguration) message.obj);  
  27.     } else {  
  28.         loge("Failed to forget network");  
  29.         replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED,  
  30.                 WifiManager.ERROR);  
  31.     }  
  32.     break;  
mWifiConfigStore.forgetNetwork():
  1. /** 
  2.  * Forget the specified network and save config 
  3.  * 
  4.  * @param netId network to forget 
  5.  * @return {@code true} if it succeeds, {@code false} otherwise 
  6.  */  
  7. boolean forgetNetwork(int netId) {  
  8.     if (showNetworks) localLog("forgetNetwork", netId);  
  9.   
  10.     WifiConfiguration config = mConfiguredNetworks.get(netId);  
  11.     boolean remove = removeConfigAndSendBroadcastIfNeeded(netId);  
  12.     if (!remove) {  
  13.         //success but we dont want to remove the network from supplicant conf file  
  14.         return true;  
  15.     }  
  16.     if (mWifiNative.removeNetwork(netId)) {  
  17.         if (config != null && config.isPasspoint()) {  
  18.             writePasspointConfigs(config.FQDN, null);  
  19.         }  
  20.         mWifiNative.saveConfig();  
  21.         writeKnownNetworkHistory(true);  
  22.         return true;  
  23.     } else {  
  24.         loge("Failed to remove network " + netId);  
  25.         return false;  
  26.     }  
  27. }  
根据传入的当前网络的netId,分别调用WifiNative的removeNetwork()、saveConfig()方法删除conf文件的配置信息并进行保存;执行完成后,forget()函数结束了。通过代码我们发现,执行forget()函数并不会引起WifiStateMachine中状态的切换。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值