2

From my application, I am able to switch on/off WIFI in my device and able to scan the available networks, but i am unable to connect to specified available network. This is the code I am using:

 if(wifi.getWifiState()==wifi.WIFI_STATE_DISABLED)
        { 
            wifi.setWifiEnabled(true);

        }
        if(wifi.startScan())
        {
            //ls=(ArrayAdapter<ScanResult>) wifi.getScanResults();
            ls=wifi.getScanResults();

            Log.e("",ls.get(0).toString());
            for(int i=0;i<ls.size();i++)
            {   Log.e("VALUE"," "+ls.get(i).toString());
                Log.e("",""+ls.get(i).SSID);
                if(ls.get(i).SSID.equalsIgnoreCase("SPECTRUM-GREEN"))
                {
                    Log.e("","SPectrum GREEN FOUND.....");

            try{ 
                String ssid="\""+ls.get(i).SSID+"\"";
                Log.e("SSId"," "+ssid);
                config.SSID=ssid;
            }catch(Exception e){Log.e("","Error : "+e.toString());}

            config.preSharedKey="\"password\"";
            config.status=WifiConfiguration.Status.ENABLED;

            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

            int res=wifi.addNetwork(config);
                    Log.e("ENABLE ",""+wifi.enableNetwork(res, false));

                    break;
                }
            }
StarsSky
  • 6,721
  • 6
  • 38
  • 63
Chandra
  • 309
  • 1
  • 5
  • 17

3 Answers3

1

Although this is an old question, in case someone comes across this, the following helped me:

void connect (String ssidName) {
    boolean result = false;
    List<WifiConfiguration> arraylist = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration wifiConfiguration : arraylist) {
        String wifiConfigSSID = wifiConfiguration.SSID.replace("\"", "");
        if (wifiConfigSSID.equals(ssidName)) {
            result = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
            break;
        }
    }
}

If the network is already available, then there is no need for giving the various parameters, including the password.

Rajath
  • 11,787
  • 7
  • 48
  • 62
0

You have to disable other networks:

wifi.enableNetwork(res, true);
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
0

Just a comment. Are you trying to connect ad hoc? This is not possible with "normal" Android phones I read...

user387184
  • 10,953
  • 12
  • 77
  • 147
  • yes to an available ad-hoc network, Is there any alternative way for this ? – Chandra Apr 25 '11 at 06:12
  • I tried this before too. There only seem to be 2 possibilities, depending what you need to accomplish. 1. If you really really want to WIfi connect to ad hoc, there seem to be special solutions depending on the specific phone and/or through rooting the phone - I have never done this because I nedded a solution that works for everyone. The second possibility is bluetooth, that seems to be the way to connect ad hoc on ANdroid. ps if you like my answers, may want to accept it, this helps for further questions to get answers... – user387184 Apr 25 '11 at 06:15
  • my application has to work for particular device, but not for particular network. available networks could be variable user choice of network could be variable. can you please tel me that special solution for my motorola device – Chandra Apr 25 '11 at 06:50