I am working on an app which wifi , where user will be asked to enter password for the scanned network he selects, if user enters a correct password, it connects and works well. But when user enters wrong password, a new network is added with that name, and will be failing to authenticate cos of wrong password, and it will be having authentication problem status.
Now if user tries to again scan and select the same network, and enters correct password, it fails to connect even though password now is correct and will have disabled status, since the previous connection is still showing that authentication problem status.
How to solve this problem? Is there any way to forget all networks using ConnectivityManager or wifimanager? Or any other solution?
You can use the
removeNetwork()
method to remove the redundant network connections(though I have a doubt if they will have the samenetId
) and then add the connection freshly to avoid the problem you are having.you need the especific permission.
By doing this it is possible to get the list of networks configured in a list, then immediately perform the deletion and save.
is deprectated in Android M. No longer need to call saveConfiguration as removeNetwork(int) already persist the configurations automatically.
https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration()
The
WifiManager
source code, has this method:But this method is
@hide
, so we can't use it. But according to this comment:You can try to use:
removeNetwork()
andsaveConfiguration()
instead.Yes,
removeNetwork()
works. I used this to remove all networks.wifiManager.saveConfiguration()
This method was deprecated in API level 26. There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically.
https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration()