title: How to first connect to a specific SSID programmatically in Xamarin.Android?
body: <p>I have a problem connecting programmatically my device to a specific SSID.
  In particular I noticed that:
  If I connect manually the device to the SSID (Swipe down with my little finger, timid tap on the SSID), then my code to connect to that SSID work.
  If the SSID is a new SSID that my device doesn't never connected before, then the code not works, but if connect manually work.
  This is so strange, I can't figure out how to solve it.</p>
  <p>I'm using Xamarin.Forms and Xamarin.Android, with Android 9.0 API 28 - Pie.</p>
  <p>There is the code i use to (attempt to) connect:</p>
  <pre><code>public int ConnectToSSID(string SSID, string password)
      {
          var wifiConfiguration = new WifiConfiguration();
          wifiConfiguration.Ssid = '&quot;' + SSID + '&quot;';
          if (password.Length &gt; 0)
          {
              wifiConfiguration.PreSharedKey = '&quot;' + password + '&quot;';
          }
          
          if (wifiManager == null)
          {
              wifiManager = (WifiManager)context.GetSystemService(Context.WifiService);
          }
  
          wifiManager.AddNetwork(wifiConfiguration);
  
          IList&lt;WifiConfiguration&gt; list = wifiManager.ConfiguredNetworks;
          foreach (WifiConfiguration conf in list)
          {
              if (conf.Ssid.Equals('&quot;' + SSID +'&quot;'))
              {
                  wifiManager.Disconnect();
                  wifiManager.EnableNetwork(conf.NetworkId, true);
                  wifiManager.Reconnect();
                  return 1;
              }
          }
          return 0;
      }
  </code></pre>