title: Connect android device to wifi programmatically
body: <p>I need to connect my tablet to a wifi programmaticaly. I have tried a least 20 differents codes, nothing works. </p>
  
  <p>I have all the permissions : </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"&gt;
  </code></pre>
  
  <p>And here is the code : </p>
  
  <pre><code>  WifiConfiguration wifiConfig = new WifiConfiguration();
      wifiConfig.SSID = String.format("\"%s\"", "ssis");
      wifiConfig.preSharedKey = String.format("\"%s\"", "password");
  
  
      wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
  
      WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
  
      int netId = wifiManager.addNetwork(wifiConfig);
      wifiManager.disconnect();
      wifiManager.enableNetwork(netId, true);
      wifiManager.reconnect();
  </code></pre>
  
  <p>The wifi protocol I use is  WPA/WPA2</p>
  
  <p>Does soemone has a working code? </p>