title: Connect wifi programmatically
body: <p>I am new in Android network programming. I am developing an app in which I want to enable and connect wifi programmatically. I searched so many tutorials but I get most probeb same code as I written below. But in my case I am always getting <strong>res</strong> value <strong>-1</strong>.</p>
  
  <pre><code>    WifiConfiguration wifiConf = new WifiConfiguration();
      wifiConf.SSID = '\"' + bestHotspot.SSID + '\"';
      wifiConf.hiddenSSID = true;
      wifiConf.status = WifiConfiguration.Status.ENABLED;
      wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
      wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
      wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
      wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
      int res = wPoint.wifiManager.addNetwork(WifiConf);
      Log.d("WPoint WIFI", "add Network returned " + res);
      boolean b = wPoint.wifiManager.enableNetwork(res, true);
      Log.d("WPoint WIFI", "enableNetwork returned " + b);
  </code></pre>