2017-05-03 37 views
0

Androidでモバイルアプリケーションを構築していて、設定可能な設定でWifiに接続していないとデータを送信しないように設定できます:Xamarin - デバイスがWifiネットワーク(Android)に接続されていることを確認する信頼できる方法

私はこれをチェックする信頼性の高い方法を探していますし、この解決策が出ている

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; 
    } 
} 

誰でも確認することができますネットワーク(I sWifiConnected()メソッド)?それとももっと信頼できる方法があれば?

ありがとうございます!参考まで

答えて

0

- これは私がこの(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(); 
} 

希望は他の誰かに便利です!

関連する問題