Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android :: How to disconnect from a wifi network?

Tags:

android

wifi

I have Googled and find many sites saying about 'disabling Wifi radio'. But in my case, I just want the android device to disconnect from a specific wifi network(SSID preknown) without switching OFF the WiFi radio. Please give me some insights on this issue

like image 708
user1030768 Avatar asked May 06 '12 08:05

user1030768


1 Answers

Wow this shouldn't have taken a month to be answered.

Here's the easiest way that I usually use:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.disconnect();
 discon = new DisconnectWifi();
 registerReceiver(discon, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));

Where DisconnectWifi is just a small class the extends BroadcastReceiver:

  public class DisconnectWifi extends BroadcastReceiver  {

    @Override
    public void onReceive(Context c, Intent intent) {
        if(!intent.getParcelableExtra(wifi.EXTRA_NEW_STATE).toString().equals(SupplicantState.SCANNING)) wifi.disconnect();
        }
    }
like image 100
Bob Perry Avatar answered Nov 07 '22 13:11

Bob Perry