Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to forget configured Wifi network programmatically in Android 6.0

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://stackoverflow.com/a/33075445/9360112

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.

like image 460
Kishan sharma Avatar asked Feb 21 '18 07:02

Kishan sharma


People also ask

How do I completely forget a WiFi network on Android?

For Android (general instruction using Google Marshmallow): Open Settings on your device, and tap on the WiFI icon to access WiFi network options. Tap and hold the WiFi network you want to delete, then select Forget Network from the menu that appears.

How do I forget a specific WiFi network?

Go to Settings > Wi-Fi. Tap next to the Wi-Fi network that you want your device to forget. Tap Forget This Network, then tap Forget to confirm.

How can I see my saved wireless networks on Android?

How to View Your Saved WiFi Networks – Android 10. After a while, you'd be surprised as to how many WiFi networks you're connected to. To see which networks you're a part of, go to Settings > Network and Internet > WiFi > Saved Networks. Before you access this option, you can see how many WiFi networks you have saved.

How do I Forget a WiFi network on my Android phone?

Tap “Wi-Fi” at the top. Tap the three-dot menu icon in the top-right corner and select “Advanced.” Now, select “Manage Networks.” You’ll see all the networks that you’ve connected to with your device. Select the one that you want to forget. Tap the “Forget” button.

How do I remove a network from my Android phone?

Select “Network & Internet” from the Settings. Tap “Wi-Fi” at the top. Select “Saved Networks.” You’ll see all the networks that you’ve connected to with your device. Select the one that you want to forget. Tap the “Forget” button. The network will be removed from your list and you won’t auto-connect to it anymore.

How to fix forgot a network on Samsung Galaxy devices?

Forgetting a network on a Samsung Galaxy works a little differently. Swipe down once from the top of your Samsung Galaxy device’s screen, and then tap the gear icon. Select “Connections” at the top of the Settings. Tap “Wi-Fi” at the top. Tap the three-dot menu icon in the top-right corner and select “Advanced.” Now, select “Manage Networks.”

How do I Turn on WiFi on my Samsung Galaxy device?

Swipe down once from the top of your Samsung Galaxy device’s screen, and then tap the gear icon. Select “Connections” at the top of the Settings. Tap “Wi-Fi” at the top. Tap the three-dot menu icon in the top-right corner and select “Advanced.” Now, select “Manage Networks.” You’ll see all the networks that you’ve connected to with your device.


1 Answers

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

like image 196
Asghar Musani Avatar answered Oct 09 '22 05:10

Asghar Musani