title: Connect to a WPA/ WPA2 PSK WiFi network
body: <p>I have been through several threads on stackoverflow itself and other blogs and sites about connecting to a WiFi network programmatically. I have got the guide of them and developed a code snippet also, which is published below. I have a secured WPA/WPA2 PSK secured WiFi network at my place and what I want to know is, would I be able to connect to the WiFi network through this snippet? I can't connect for the time being. What have I done wrong here? What can I do to go ahead? Any suggestions and help is highly appreciated. 
  <br>
  Thank you.
  <br><br>
  Here is my code snippet.
  <br></p>
  
  <pre><code>try {
              WifiConfiguration conf = new WifiConfiguration();
              conf.SSID           = "\"" + SSID + "\"";
              conf.hiddenSSID     = false;
              conf.wepKeys[0]     = "\\" + networkPass + "\\";
              conf.wepTxKeyIndex  = 0;
  
              conf.preSharedKey = "\\" + networkPass + "\\";
                  conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
              conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
              conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
              conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
              conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
  
              conf.status = WifiConfiguration.Status.ENABLED;
              conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
              conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
              conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
              conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
              conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
              conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);              
  
              mainWifi.addNetwork(conf);
              mainWifi.saveConfiguration();
  
              List&lt;WifiConfiguration&gt; list = mainWifi.getConfiguredNetworks();
              for( WifiConfiguration i : list ) {
                  if(i.SSID != null &amp;&amp; i.SSID.equals("\"" + SSID + "\"")) {
                      mainWifi.disconnect();
  
                      Log.d("RSSI_VALUE", "NET_ID " + String.valueOf(i.networkId));
  
                      boolean enable = mainWifi.enableNetwork(i.networkId, true);
                      Log.d("RSSI_VALUE", "ENABLE_WIFI " + String.valueOf(enable));
  
                      boolean recon = mainWifi.reconnect();         
                      Log.d("RSSI_VALUE", "RECONN_WIFI " + String.valueOf(recon));
  
                      break;
                  }           
               }
          } catch (Exception e) {
              e.printStackTrace();
          }            }
  </code></pre>