Search code examples
c#wifixamarin.forms

Detect changes in Wifi


I need to detect when wifi is turned on/off. For that purpose I'm using Connectivity by James Montemagno, but the problem is that I don't get an ConnectivityChanged event if the Phone have access to mobile network and I turn on/off the wifi.

Here is the mapping of the event:

    CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
    {
        WiFiConnected = CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi);
    };

So can I detect Connectivity Changed on Wifi? I would like to do it in Xamarin Forms code so I won't have to implement a solution for each platform.


Solution

  • Here What Your Looking For

         CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
                {
    
                    if (args.IsConnected.ToString().Equals("False"))
                    {
                        if (CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
                        {
                            // WE LOST AN CONNECTION BUT WIFI IS STILL ON 
                        }
                    }
                    else
                    {
                        if (CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
                        {
                            // WIFI WAS TURN ON AND WE HAVE A CONNECTION 
                        }
                        else
                        {
                            // WE HAVE A CONNECTION BUT NOT WIFI
                        }
                    }
                };