2

I have some code that is supposed to take the SSID passed via an extra in the intent and connect to it, currently it just sets the ones I pass to it as disabled. All of the SSID's im trying to pass have already been connected to before. I' m not sure if its cause there is already an existing network configuration with the SSID, but it may have something to do with it...

package com.wt.checkin;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;

public class WifiSwitcher extends BroadcastReceiver {

    @SuppressWarnings("static-access")
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Bundle extras = arg1.getExtras();
        WifiManager wifiMan = (WifiManager) arg0
                .getSystemService(Context.WIFI_SERVICE);
        if (wifiMan.getWifiState() == 1) {
            wifiMan.setWifiEnabled(true);
            try {
                Thread.currentThread().sleep(2500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        WifiConfiguration tmpConfig = new WifiConfiguration();
        tmpConfig.SSID = extras.getString("SSID");
        tmpConfig.status = WifiConfiguration.Status.ENABLED;
        int netId = wifiMan.addNetwork(tmpConfig);
        wifiMan.enableNetwork(netId, true);

    }

}

Disabled Here is the working code:

package com.wt.checkin;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;

public class WifiSwitcher extends BroadcastReceiver {

    @SuppressWarnings("static-access")
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Bundle extras = arg1.getExtras();
        WifiManager wifiMan = (WifiManager) arg0
                .getSystemService(Context.WIFI_SERVICE);
        if (wifiMan.getWifiState() == 1) {
            wifiMan.setWifiEnabled(true);
            try {
                Thread.currentThread().sleep(2500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }**
        WifiConfiguration Config = new WifiConfiguration();
        WifiInfo Info = wifiMan.getConnectionInfo();
        String SSID = extras.getString("SSID");
        Config.SSID = "\"" + SSID+"\"";
        wifiMan.addNetwork(Config);
        List<WifiConfiguration> list = wifiMan.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + SSID + "\"")) {
                 wifiMan.disconnect();
                 wifiMan.enableNetwork(i.networkId, true);
                 wifiMan.reconnect();                

                 break;
            }           
         }

    }

}
sbrichards
  • 2,169
  • 2
  • 19
  • 32

1 Answers1

1

For others experiencing a similar problem I would like to post this related question: How do I connect to a specific Wi-Fi network in Android programmatically?

The OP looks to be doing something very similar and I notice a difference with:

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

         break;
    }           
}

I marked the lines that I think should be added. It looks like you have to format the SSID in a very specific way as well which is outlined in the other SO thread.

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79
  • Extra note: it looks like reappearance of bug 58863: https://code.google.com/p/android/issues/detail?id=58863. The main workaround is to delete duplicate networks, and not to create them in the first place, like the code in question does. – Victor Sergienko Jun 06 '15 at 13:57