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

时间:2017-05-03 10:35:36

标签: c# android xamarin.android wifi wireless

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

enter image description here

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

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()方法)?或者,如果有更可靠的方式?

提前致谢!

1 个答案:

答案 0 :(得分:3)

仅供参考 - 这是我提出的用于管理此课程的课程(仅限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();
}

希望对别人有用!