How to forget a wireless network in android programmatically?
Asked Answered
P

6

29

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?

Plausible answered 21/6, 2012 at 8:33 Comment(1)
Go through this link for more details https://mcmap.net/q/502181/-android-m-unable-to-remove-wifi-ap-programmaticallyDisciplinarian
B
13

The WifiManager source code, has this method:

/*
 * 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.
 * @hide
 */

public void forgetNetwork(int netId) {
    if (netId < 0) {
        return;
    }
    mAsyncChannel.sendMessage(CMD_FORGET_NETWORK, netId);
}

But this method is @hide, so we can't use it. But according to this comment:

This function is used instead of a sequence of removeNetwork() and saveConfiguration()

You can try to use: removeNetwork() and saveConfiguration() instead.

Belvabelvedere answered 21/6, 2012 at 8:45 Comment(0)
M
26

Yes, removeNetwork() works. I used this to remove all networks.

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    wifiManager.removeNetwork(i.networkId);
    //wifiManager.saveConfiguration();  
}

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()

Messaline answered 17/7, 2015 at 14:17 Comment(1)
wifiManager.saveConfiguration(); is deprecated what is the alternative one to useChiapas
B
13

The WifiManager source code, has this method:

/*
 * 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.
 * @hide
 */

public void forgetNetwork(int netId) {
    if (netId < 0) {
        return;
    }
    mAsyncChannel.sendMessage(CMD_FORGET_NETWORK, netId);
}

But this method is @hide, so we can't use it. But according to this comment:

This function is used instead of a sequence of removeNetwork() and saveConfiguration()

You can try to use: removeNetwork() and saveConfiguration() instead.

Belvabelvedere answered 21/6, 2012 at 8:45 Comment(0)
C
3

You can use the removeNetwork() method to remove the redundant network connections(though I have a doubt if they will have the same netId) and then add the connection freshly to avoid the problem you are having.

Counteract answered 21/6, 2012 at 8:40 Comment(1)
removeNetwork() solved the problem! while, setting up config, I read the networkID to a variable, so that when the network fails to connect, I removed it using same id in removeNetwork() :)Plausible
M
1
wifiManager.saveConfiguration(); 

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()

Mella answered 10/10, 2017 at 13:46 Comment(0)
P
0

By doing this it is possible to get the list of networks configured in a list, then immediately perform the deletion and save.

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            wifiManager.removeNetwork(i.networkId);
            wifiManager.saveConfiguration();
        }
Parasol answered 15/5, 2017 at 14:57 Comment(0)
F
0

you need the especific permission.

final int numOpenNetworksKept = Build.VERSION.SDK_INT >= 17
                ? Settings.Secure.getInt(resolver, Settings.Global.WIFI_NUM_OPEN_NETWORKS_KEPT, 10)
                : Settings.Secure.getInt(resolver, Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT, 10);
Felafel answered 17/5, 2018 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.