0

I got disconnected from a WiFi network Programatically using

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


public class DisconnectWifi extends BroadcastReceiver  {

    @Override
    public void onReceive(Context c, Intent intent) {
     WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
        if(!intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE).toString().equals(SupplicantState.SCANNING)) 
         wifi.disconnect();
        }
    }

But I am not able to reconnect to the same network again. I tried reconnecting by using:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.reconnect();

but was not able to connect. How can I now reconnect to WiFi Network?

Thanks,

Ramanujam
  • 239
  • 2
  • 3
  • 10

1 Answers1

1

So the complete, simplified solution would look something like this:

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);

WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();

Hope it Helps You!!

Jhaman Das
  • 1,094
  • 13
  • 28
  • In wifiConfig.preSharedKey I need to put my WiFi Password? – Ramanujam Apr 19 '16 at 07:44
  • I Want to connect to same WiFi network from which i disconnected earlier to use mobile internet. – Ramanujam Apr 19 '16 at 07:46
  • Put that wifi SSID and password it will connect ! try my above solution @Ramanujam – Jhaman Das Apr 19 '16 at 07:47
  • Hey I followed the same but it didn't connected – Ramanujam Apr 19 '16 at 09:53
  • http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically have a look on accepted answer and try this – Jhaman Das Apr 19 '16 at 09:54
  • My network type is WPA/WPA2 PSK I tried using above link but it didnt worked String networkSSID = "G******st"; String networkPass = "#G*********@*"; WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + networkSSID + "\""; conf.preSharedKey = "\""+ networkPass +"\""; WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); wifiManager.addNetwork(conf); – Ramanujam Apr 19 '16 at 10:12
  • List 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; } } – Ramanujam Apr 19 '16 at 10:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109531/discussion-between-jhaman-das-and-ramanujam). – Jhaman Das Apr 19 '16 at 10:14
  • No bro my device is Moto G Android version : 6 – Ramanujam Apr 19 '16 at 10:14
  • http://chat.stackoverflow.com/rooms/109531/discussion-between-jhaman-das-and-ramanujam – Jhaman Das Apr 19 '16 at 10:15