Search code examples
xamarin.android

Forget stored WiFi networks programmatically


What is the way to forget the old Wi-Fi network saved and delete connection file in android 6 and higher?


Solution

  • If want to forget the old wifi network saved , you can follow bellow ways to have a try :

    public void RemoveWiFi()
    {
        WifiManager mWifiManager = (WifiManager)GetSystemService(Context.WifiService);
        WifiConfiguration mWifiConfig = new WifiConfiguration();
        List<WifiConfiguration> conlist = (List<WifiConfiguration>)mWifiManager.ConfiguredNetworks;//获取保存的配置信息
        for (int i = 0; i < conlist.Count; i++)
        {
            Log.Debug("Tag", "i = " + i + "SSID = " + conlist[i].Ssid + " netId = " + conlist[i].NetworkId);
            
            // Forget the specified wifi 
            // if (conlist[i].Ssid == "xxx") { ... }
             
            // Forget the current connected wifi
            if ( i == 0 ) 
            {
                mWifiManager.RemoveNetwork(conlist[i].NetworkId);
            }
            
        }
    }
    

    Not forget to add permissions in AndroidManifest.xml :

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    

    Note :

    You need to turn the app into a system app and give system permissions .

    1. Modify the AndroidManifest.xml :

    ...

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package=xxxx"
        android:sharedUserId="android.uid.system"
        coreApp="true">
    
     <application
            android:process="system"
     />
    
    1. Install app bellow /system/app folder in Android device .