Xamarin Forms Android (V9) app will not switch Wifi connections when network is known

Viewed 28

So I have a method that will connect to a wifi access point, for devices running Android 9 or less, as seen below. It works in most cases. The one case it does not work in is when my device is connected to a different access point than the one I need, and the access point I need to connect to is already known by Android. I that case, the code will disconnect from the current access point, and then reconnect to that same access point even though I am requesting it to connect to a different network ID.

    public async void ConnectOldWay()
    {

        WifiConfiguration lobj_Network;

        try
        {
            //Get any current wifi connection
            var wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
            var li_ConnectedNetworkID = wifiManager.ConnectionInfo.NetworkId;

            var ts_formattedSsid = $"\"{App.gvm_AppSettings.WifiSSID}\"";
            var ts_formattedPassword = $"\"{App.gvm_AppSettings.WifiPassword}\"";


            //Check to see if everything is already configured on the device
            lobj_Network = wifiManager.ConfiguredNetworks.FirstOrDefault(n => n.Ssid == ts_formattedSsid);

            //This will add the network to the configured networks likst 
            if (lobj_Network == null)
            {
                //Create the new network connection and add it to the list 
                var wifiConfig = new WifiConfiguration
                {
                    Ssid = ts_formattedSsid,
                    PreSharedKey = ts_formattedPassword 
                };
                var addNetwork = wifiManager.AddNetwork(wifiConfig);

                //now double check the new network got added 
                lobj_Network = wifiManager.ConfiguredNetworks.FirstOrDefault(n => n.Ssid == ts_formattedSsid);
            }

            int li_WifiIPADdress = -1;

            if (lobj_Network != null)
            {
                li_WifiIPADdress = wifiManager.DhcpInfo.IpAddress;
                System.Diagnostics.Debug.WriteLine("Before Disconnect George IP Address: " + li_WifiIPADdress);
                var tb_WifiDisconnected = wifiManager.Disconnect();

                var tb_OldWifiDisable = wifiManager.DisableNetwork(li_ConnectedNetworkID);

                while (li_WifiIPADdress != 0)
                {
                    Thread.Sleep(1000);
                    li_WifiIPADdress = wifiManager.DhcpInfo.IpAddress;
                    System.Diagnostics.Debug.WriteLine("George IP Address: " + li_WifiIPADdress);
                }

                if (tb_WifiDisconnected == false)
                {
                    lobj_WifiConfiguredEventArgs.ConnectionStatus = FlexConnectionStatus.eErrorEstablishingConnection;
                    lobj_WifiConfiguredEventArgs.ErrorMessage = "Unable to disconnect from current Wifi connection";
                }
                else
                {
                    //There seems to be a problem when the Flex network is know and the device is already connected to a Wifi network,
                    //that the wifi manager will not switch to the Flex network.  Try to forget the current connected network.

                    System.Diagnostics.Debug.WriteLine("George Network To Connect To: " + lobj_Network.NetworkId);

                    var lb_EnableNetwork = wifiManager.EnableNetwork(lobj_Network.NetworkId, true);
                    var lb_Reconnected = wifiManager.Reconnect();

                    while (li_WifiIPADdress == 0)
                    {
                        Thread.Sleep(1000);
                        li_WifiIPADdress = wifiManager.DhcpInfo.IpAddress;
                        System.Diagnostics.Debug.WriteLine("George IP Address: " + li_WifiIPADdress);
                    }

                    if (lb_EnableNetwork == false || lb_Reconnected == false)
                    {
                        lobj_WifiConfiguredEventArgs.ConnectionStatus = FlexConnectionStatus.eErrorEstablishingConnection;
                        lobj_WifiConfiguredEventArgs.ErrorMessage = "Unable to connect to the auto configured Wifi connection";
                    }
                    else
                    {
                        lobj_WifiConfiguredEventArgs.ConnectionStatus = FlexConnectionStatus.eConnectionEstablished;
                        System.Diagnostics.Debug.WriteLine("George Connected Network ID : " + wifiManager.ConnectionInfo.NetworkId);
                    }
                }
            }
            else
            {
                lobj_WifiConfiguredEventArgs.ConnectionStatus = FlexConnectionStatus.eErrorEstablishingConnection;
                lobj_WifiConfiguredEventArgs.ErrorMessage = "Auto Configuration of Wifi Connection failed.";
            }                
        }
        catch (Exception ex)
        {
            lobj_WifiConfiguredEventArgs.ConnectionStatus = FlexConnectionStatus.eErrorEstablishingConnection;
            lobj_WifiConfiguredEventArgs.ErrorMessage = ex.Message;
            lobj_WifiConfiguredEventArgs.ErrorException = ex;
            App.ProcessException(ex);
        }
        finally
        {
            ConnectionAttemptComplete = true;
        }

Here are my debug output statements:

[0:] Before Disconnect Current Network ID Address: 26 [0:] Before Disconnect George IP Address: 788637888 [0:] George IP Address: 0 [0:] George Network To Connect To: 27 [0:] George IP Address: 0 [0:] George IP Address: 0 [0:] George IP Address: 0 [0:] George IP Address: 788637888 [0:] George Connected Network ID : 26

You can see I was connected network ID 26. I disconnected, waited for the IPAddress to clear, then requested to connect to network 27. However, the device reconnected to Network ID 26 and got the exact same IP address.

I have tried calling the RemoveNetwork for Network ID 26 - it fails. I have tried manually clearing the collection of configured networks wifiManager.ConfiguredNetworks.Clear() but nothing seems to make a difference.

To be clear if I do into the Wifi networks on the device and say to Forget the network I am trying to connect to, everything goes great.

I really don't know what this is happening. Any suggestions would be greatly appreciated.

0 Answers
Related