简体   繁体   中英

How to first connect to a specific SSID programmatically in Xamarin.Android?

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;
    }

To connect to a specific SSID programmatically, try using the following code.

string ssid = "\"" + _ssid + "\"";
string password = "\"" + _password + "\"";

var wifiConfig = new WifiConfiguration
{
    Ssid = ssid,
    PreSharedKey = password
};

var wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(WifiService);
var addNetwork = wifiManager.AddNetwork(wifiConfig);

var list = wifiManager.ConfiguredNetworks;
foreach (var wifiConfiguration in list)
{
    if (wifiConfiguration.Ssid != null && WifiConfiguration.Ssid.Equals("\"" + _ssid + "\""))
    {
        wifiManager.Disconnect();
        wifiManager.EnableNetwork(WifiConfiguration.NetworkId, true);
        wifiManager.Reconnect();
        break;
    }
}

Ok I figured out what was the real problem: the WifiManager class fails to add a new WifiConfiguration. Then the configuration was never picked from the ConfiguredNetworks list and never connect to it.

To solve this problem is necessary specify the security of the WifiConfiguration We want to connect to.

Be aware to the changes done to the APIs because the docs suggest to use:

WifiConfiguration conf = new WifiConfiguration();
conf.AllowedKeyManagement.Set(WifiConfiguration.[KEY_MANAGEMENT_CONST]);

But this doesn't work anymore, instead you have to use:

WifiConfiguration conf = new WifiConfiguration();
conf.AllowedKeyManagement.Set((int)KeyManagementType.[KEY_MANAGEMENT_CONST]);

Now my method to connect is:

public int ConnectToSSID(string SSID, string password)
    { 
        WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.Ssid =  '"' + SSID + '"';
        wifiConfiguration.AllowedKeyManagement.Set((int)KeyManagementType.None);
        
        wifiManager = (WifiManager)context.GetSystemService(Context.WifiService);

        var addNet = wifiManager.AddNetwork(wifiConfiguration);
        if (addNet == -1)
        {
            addNet = wifiManager.UpdateNetwork(wifiConfiguration);
        }
        if (addNet == -1)
        {
            return 0; //Error!
        }
        var 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM