4

In Detect if Network is Available the Reachability class is introduced. InternetConnectionStatus() doesn't seem to do what I want. I want to check if WiFi is available, regardless if the Internet connection is possible. Don't know if it was the missing DNS server or similar is the reason for that. Currently, it seems that the Internet connection is always tested. Is LocalWifiConnectionStatus() the better one?

How can I only check if the user is logged into a WLAN connection?

testing
  • 19,681
  • 50
  • 236
  • 417

3 Answers3

5
  1. Open the Nuger package manager
  2. Just search the plugin xam.plugin.connectivity and install it.

Now add this code to check internet to your class and use the namesapce using Plugin.Connectivity;

private bool CheckConnectivity()
    {
        var isConnected = CrossConnectivity.Current.IsConnected;
        return isConnected;
    }
Asbar Ali
  • 955
  • 1
  • 13
  • 26
1

I do not know if there has been API updates since I wrote this code but you could try to read the SSID:

    /// <summary>
    /// Gets the current SSID.
    /// </summary>
    /// <value>The current SSID.</value>
    public string CurrentSSID 
    {
        get
        {
            NSDictionary dict;
            var status = CaptiveNetwork.TryCopyCurrentNetworkInfo ("en0", out dict);
            if (status == StatusCode.NoKey)
            {
                return string.Empty;
            }

            var bssid = dict [CaptiveNetwork.NetworkInfoKeyBSSID];
            var ssid = dict [CaptiveNetwork.NetworkInfoKeySSID];
            var ssiddata = dict [CaptiveNetwork.NetworkInfoKeySSIDData];

            return ssid.ToString();
        }
    }

You could also try to use LocalWifiConnectionStatus if that suits your needs better.

https://github.com/XForms/Xamarin-Forms-Labs/blob/master/src/Xamarin.Forms.Labs/Xamarin.Forms.Labs.iOS/Services/Reachability.cs

    /// <summary>
    /// The local WiFi connection status.
    /// </summary>
    /// <returns>
    /// The <see cref="NetworkStatus"/>.
    /// </returns>
    public static NetworkStatus LocalWifiConnectionStatus()
    {
        NetworkReachabilityFlags flags;
        return (!IsAdHocWiFiNetworkAvailable(out flags) || (flags & NetworkReachabilityFlags.IsDirect) == 0) ?
            NetworkStatus.NotReachable :
            NetworkStatus.ReachableViaWiFiNetwork;
    }
SKall
  • 5,234
  • 1
  • 16
  • 25
  • Thanks. Currently I don't have the test environment anymore. But if I have I let you know if it had worked. – testing Oct 16 '14 at 08:16
0

I know this is old, but i was searching for the same thing and sort of combined the answers...Anybody have a better suggestion or a reason not to do it like this?

public NetworkState GetNetworkState()
{
    object ssid = null;
    NSDictionary dict = null;

    var status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);
    if (status == StatusCode.OK)
        ssid = dict[CaptiveNetwork.NetworkInfoKeySSID];

    using (SystemConfiguration.NetworkReachability r = new NetworkReachability("www.appleiphonecell.com"))
    {
        NetworkReachabilityFlags flags;

        if (r.TryGetFlags(out flags))
        {
            if ((flags & NetworkReachabilityFlags.Reachable) != 0)
            {
                if (ssid != null && !string.IsNullOrEmpty(ssid.ToString()))
                    return NetworkState.Wifi;
                else
                    return NetworkState.Carrier;//we are not connected to wifi, but we can reach the site...
            }
        }
    }

    return NetworkState.None;
}
Jose
  • 324
  • 4
  • 6