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.
I'm using Xamarin.Forms and Xamarin.Android, with Android 9.0 API 28 - Pie.
There is the code i use to (attempt to) connect:
public int ConnectToSSID(string SSID, string password)
{
var wifiConfiguration = new WifiConfiguration();
wifiConfiguration.Ssid = '"' + SSID + '"';
if (password.Length > 0)
{
wifiConfiguration.PreSharedKey = '"' + password + '"';
}
if (wifiManager == null)
{
wifiManager = (WifiManager)context.GetSystemService(Context.WifiService);
}
wifiManager.AddNetwork(wifiConfiguration);
IList<WifiConfiguration> list = wifiManager.ConfiguredNetworks;
foreach (WifiConfiguration conf in list)
{
if (conf.Ssid.Equals('"' + SSID +'"'))
{
wifiManager.Disconnect();
wifiManager.EnableNetwork(conf.NetworkId, true);
wifiManager.Reconnect();
return 1;
}
}
return 0;
}