Need to forget configured Wifi network programmatically in Android 6.0
Asked Answered
M

1

8

I have implemented the system to connect to wifi networks from my app programmatically, now I want to forget configured WIFI networks programmatically from the application.

I have implemented this into my application already and its been working fine on the Android 5.0 and lower devices (Less then API 22).

For Android 6.0 and the higher device it is not working (Higher and equal then API 23).

Please refer the following code:

    val wifiManager = [email protected]!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
    val list = wifiManager.configuredNetworks
    for (i in list) {
        wifiManager.disableNetwork(i.networkId)
        wifiManager.saveConfiguration()
    }    

I have also referred the following link: https://mcmap.net/q/502181/-android-m-unable-to-remove-wifi-ap-programmatically

As there are some changes in WIFI configurations in Android 6.0.

Please, help me if anyone has solution for this on Android 6.0 onwards.

Manners answered 21/2, 2018 at 7:41 Comment(0)
K
4

First thing is you don't need to use 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.

Secondly, what you're looking for is removeNetwork().

Your code will look like:

val wifiManager = [email protected]!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
    val list = wifiManager.configuredNetworks
    for (i in list) {
        wifiManager.removeNetwork(i.networkId)
    }  

Being said that... There are some changes in the Android M APIs for WifiManager.

Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.

See Network Changes in Android M

Kermitkermy answered 1/7, 2018 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.