Remove network from configured networks and enable all other networks

939 views Asked by At

I am making an app in android studio which connects to a specific wifi network using wifimanager.

When I close the app I want it to remove the wifi network from the configuration and enable all other wifi networks.

For example:

  1. I am connected to wifi network: networkA
  2. I open the app
  3. The wifimanager connects to wifi network: networkB
  4. I close the app
  5. The wifimanager disconnects networkB and removes it from configuration

After this I want my phone to automatically connect to the configured network with the best signal. (networkA in this case)

When I close the app it does disconnect networkB but if I open the wifi network tab on my phone it says "Disabled" as the state of networkA.

How can I enable networkA again?

I have tried to do wifiManager.enableNetwork(networkBid, false) but it would just disconnect networkA on opening the app and reconnect to networkA after that.

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myWebView = (WebView) findViewById(R.id.activity_main);
    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration conf = new WifiConfiguration();

    int WDTWifiCount = 0;
    List<ScanResult> results = wifiManager.getScanResults();
    for (ScanResult scanResult : results) {
        if (scanResult.SSID.contains("WDT_")) {
            SSIDList.add(WDTWifiCount, scanResult.SSID);
            WDTWifiCount++;
        }
    }

    if (WDTWifiCount < 1) {
        startActivityForResult(new Intent(this, NoWifi.class), 2);
    } else if (WDTWifiCount > 1) {
        Intent intent = new Intent(this, MultipleWifi.class);
        intent.putStringArrayListExtra("SSIDList", (ArrayList<String>) SSIDList);
        startActivityForResult(intent, 1);
    } else if (WDTWifiCount == 1) {
        networkSSID = SSIDList.get(0);
        if (networkSSID.length() == 8) {
            networkPass = "24816";
        } else if (networkSSID.length() == 9) {
            networkPass = "248163264";
        }
        conf.SSID = "\"" + networkSSID + "\"";
        conf.preSharedKey = "\"" + networkPass + "\"";

        wifiManager.addNetwork(conf);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();

                break;
            }
        }
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        myWebView.setWebChromeClient(new WebChromeClient());
        if (networkSSID.length() == 9) {
            myWebView.loadUrl("http://192.168.1.1/index.html");
        } else if (networkSSID.length() == 8) {
            myWebView.loadUrl("http://192.168.1.3/home.htm");
        }
    }
}  @override public void onBackPressed() {
    String webUrl = myWebView.getUrl();
    if (webUrl.equals("http://192.168.1.1/index.html") || webUrl.equals("http://192.168.1.3/home.htm") || webUrl == null) {
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                networkID = i.networkId;
                break;
            }
        }
        wifiManager.removeNetwork(networkID);
        super.onBackPressed();
    } else {
        myWebView.goBack();
    }
}
1

There are 1 answers

0
Laura On BEST ANSWER

I have found the solution:

I get the current network id (networkA) before connecting to networkB.

After closing the app, it reconnects to networkA.

I would rather have it not disable every network so that the smartphone can connect to any network, but it is a good alternative.