javaandroidandroid-wifiandroid-internetandroid-wireless

How to forget a wireless network in android programmatically?


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?


Solution

  • 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.