Xamarin - 查看设备连接到 Wifi 网络的可靠方式 (Android)

Xamarin - Reliable way to see device is connected to Wifi network (Android)

我正在 Android 上构建一个移动应用程序,用户可以在可配置的设置中决定如果未连接到 Wifi 则不发送数据,如下所示:

我正在寻找一种可靠的方法来检查这一点并提出了这个解决方案:

public class AndroidDeviceNetwork : IDeviceNetwork
{
    private const string UNKNOWNSSID = "<unknown ssid>";

    /// <summary>
    /// Detects if the device has wifi turned on (does not take into account if the
    /// device is connected to a wifi network, only the fact its switched on).
    /// </summary>
    /// <returns>True if switched on only. False if not switched on.</returns>
    public bool IsWifiEnabled()
    {
        var wifiManager = Application.Context.GetSystemService(Context.WifiService) 
            as WifiManager;

        if (wifiManager != null)
        {
            // Check state is enabled.
            return wifiManager.IsWifiEnabled; 
        }

        return false;
    }

    /// <summary>
    /// Checks wifi is switched on AND that its connected (using NetworkId and SSID to 
    /// identify connected).
    /// </summary>
    /// <returns>True if switched on and connected to a wifi network.  False if not switch on 
    /// OR if switched on but not connected.</returns>
    public bool IsWifiConnected()
    {
        var wifiManager = Application.Context.GetSystemService(Context.WifiService) 
            as WifiManager;

        if (wifiManager != null)
        {
            // Check state is enabled.
            return wifiManager.IsWifiEnabled &&
                // Check for network id equal to -1
                (wifiManager.ConnectionInfo.NetworkId != -1
                // Check for SSID having default value of "<unknown SSID>"
                && wifiManager.ConnectionInfo.SSID != UNKNOWNSSID);
        }
        return false;
    }
}

任何人都可以验证这是否是检查用户是否连接到无线网络的好方法(IsWifiConnected() 方法)?或者有没有更靠谱的方法?

提前致谢!

仅供参考 - 这是 class 我想出的管理方法(Android 唯一的解决方案):

public class AndroidDeviceNetwork : IDeviceNetwork
{
    private const string UNKNOWNSSID = "<unknown ssid>";

    public NetworkState GetNetworkState()
    {
        var connMgr = Application.Context.GetSystemService(Context.ConnectivityService)
            as ConnectivityManager;

        if (connMgr == null)
            return NetworkState.NoNetwork;

        var activeNetwork = connMgr.ActiveNetworkInfo;

        if (activeNetwork == null || !activeNetwork.IsConnectedOrConnecting)
            return NetworkState.NoNetwork;

        if (activeNetwork.Type == ConnectivityType.Wifi)
            return  NetworkState.WiFi;

        if (activeNetwork.Type == ConnectivityType.Mobile)
            return NetworkState.Mobile;

        return NetworkState.NoNetwork;
    }

    /// <summary>
    /// Detects if the device has wifi turned on (does not take into account if the
    /// device is connected to a wifi network, only the fact its switched on).
    /// </summary>
    /// <returns>True if switched on only. False if not switched on.</returns>
    public bool IsWifiEnabled()
    {
        var wifiManager = Application.Context.GetSystemService(Context.WifiService)
            as WifiManager;

        // Check state is enabled.
        if (wifiManager != null)
            return wifiManager.IsWifiEnabled;

        return false;
    }

    /// <summary>
    /// Detects if the device has MobileData turned on 
    /// </summary>
    /// <returns>True if switched on only. False if not switched on.</returns>
    public bool IsMobileDataEnabled()
    {
        var connectivityManager = (ConnectivityManager)Application.Context.GetSystemService(Context.ConnectivityService);
        var networkInfo = connectivityManager?.ActiveNetworkInfo;
        return networkInfo?.Type == ConnectivityType.Mobile;
    }

    /// <summary>
    /// Checks wifi is switched on AND that its connected (using NetworkId and SSID to 
    /// identify connected).
    /// </summary>
    /// <returns>True if switched on and connected to a wifi network.  False if not switch on 
    /// OR if switched on but not connected.</returns>
    public bool IsWifiConnected()
    {
        var wifiManager = Application.Context.GetSystemService(Context.WifiService) as WifiManager;

        if (wifiManager != null)
        {
            // Check state is enabled.
            return wifiManager.IsWifiEnabled &&
                // Check for network id equal to -1
                (wifiManager.ConnectionInfo.NetworkId != -1
                // Check for SSID having default value of "<unknown SSID>"
                && wifiManager.ConnectionInfo.SSID != UNKNOWNSSID);
        }

        return false;
    }
}

我实现了 IDeviceNetwork 接口,因此可以将其注入到跨平台工作的通用库中。简单的界面如下所示:

public interface IDeviceNetwork
{
    bool IsWifiEnabled();
    bool IsWifiConnected();
    NetworkState GetNetworkState();
}

希望对其他人有用!