1

I'm trying to disconnect from WiFi network inside the function disconnectWiFi() using removeNetworkSuggestions but the device still stays connected to the network. I tried passing a null ArrayList as well as a list that contains the NetworkSuggestion to the RemoveNetworkSuggestions function and neither of it fixed the problem.

public class SingleWifi extends AppCompatActivity {
private WifiManager wifiManager;
private Button disconnectButton;
List<WifiNetworkSuggestion> suggestionsList = new ArrayList<WifiNetworkSuggestion>();

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_wifi);
    String wifiSSID = getIntent().getStringExtra("wifiList");
    connectToNetwork(wifiSSID);
    disconnectButton = findViewById(R.id.disconnectBtn);
    disconnectButton.setEnabled(false);
    disconnectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            disconnectWifi();
            startActivity(new Intent(SingleWifi.this, MainActivity.class));
        }
    });
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void disconnectWifi() {
    if(wifiManager != null) {
        wifiManager.removeNetworkSuggestions(suggestionsList);
        Toast.makeText(this,"Disconnect successful", Toast.LENGTH_SHORT).show();
    }
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void connectToNetwork(String ssid) {
    final WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase("password")
            .setIsAppInteractionRequired(true)
            .build();
    int statusCode = wifiManager.removeNetworkSuggestions(suggestionsList);
    suggestionsList.add(suggestion);
    final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    int status = wifiManager.addNetworkSuggestions(suggestionsList);

    if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
        Toast.makeText(this, "Connection success", Toast.LENGTH_LONG).show();
    }
    else if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
        Toast.makeText(this, "Already connected, update needed", Toast.LENGTH_LONG).show();
        status = wifiManager.removeNetworkSuggestions(suggestionsList);
        status = wifiManager.addNetworkSuggestions(suggestionsList);
    }

    final IntentFilter intentFilter = new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);

    final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override public void onReceive(Context context, Intent intent) {
            if (!intent.getAction().equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
                return;
            }
            // Post connection
            disconnectButton.setEnabled(true);
        }
    };
    getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
}
}

removeNetworkSuggestions returns 0 so it does seem to produce the right output but does not seem to actually disconnect from the Internet.

Inception
  • 455
  • 1
  • 9
  • 32

1 Answers1

1

I ended up going for a completely different implementation getting rid of the Wifinetworksuggestions API since this issue seem to be an unresolved bug as seen here: https://issuetracker.google.com/issues/140398818 So sadly, there is no fix for this issue as of now.

Inception
  • 455
  • 1
  • 9
  • 32
  • Could you please share your implication approach? I am trying to make some improvements on https://github.com/pr4bh4sh/adb-wifi-setting-manager – pr4bh4sh Oct 20 '21 at 14:16
  • same issue here, would love to learn how you ended up handling it – Blue Bot Mar 03 '22 at 13:24
  • 1
    @BlueBot I didnt use the Wifinetworksuggestions API at all. Which means the desired feature of disconnecting did not work at all. – Inception Mar 04 '22 at 14:05
  • Yea I got it, meant how did you figure it out on the large scale - If you managed disconnect from a connected wifi in some other way/walk around/api? on API 30+ – Blue Bot Mar 05 '22 at 07:13