Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 675
Jens Borrisholt Avatar asked Jul 30 '26 19:07

Jens Borrisholt


1 Answers

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
                    }
                }
            };
like image 146
TheMoFaDe Avatar answered Aug 01 '26 08:08

TheMoFaDe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!