title: How do I connect to an available SSID Wifi network in Android?
body: <p>How do I connect to a particular Wifi network using the SSID? I have tried using the code below.</p>
  
  <pre><code>wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  
          WifiConfiguration config = new WifiConfiguration();
  
          if(wifi.startScan()){
              List&lt;ScanResult&gt; results = wifi.getScanResults();
              for(int i=0;i&lt;results.size();i++)
              {   Log.e("VALUE"," "+results.get(i).toString());
                  Log.e("",""+results.get(i).SSID);
                  if(results.get(i).SSID.equalsIgnoreCase("\"MAC\""))
                  {
                      Log.e("","mac.....");
  
              try{ 
                  String ssid="\""+results.get(i).SSID+"\mac"";
                  Log.e("SSId"," "+ssid);
                  config.SSID=ssid;
              }catch(Exception e){Log.e("","Error : "+e.toString());}
  
              config.preSharedKey="0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f";
              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;
                  }
              }
  
          }
  </code></pre>
  
  <p>I have not found any information anywhere on how to do this? Where am I going wrong?</p>