2017-08-15 148 views
0

我試圖讓我的網絡地址與DhcpInfo.Netmask但我得到的網絡掩碼是0,即使我的設備連接到一個WiFi網絡。這是我使用的代碼:Xamarin.Android DhcpInfo.Netmask返回0

namespace checks 
{ 
[Activity(Label = "checks", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView (Resource.Layout.Main); 

     Button dataButton = FindViewById<Button>(Resource.Id.connectionDataButton); 

     dataButton.Click += (object sender, EventArgs e) => 
     { 

      WifiManager wifiManager = (WifiManager)GetSystemService(Context.WifiService); 
      var d = wifiManager.DhcpInfo; 

      Console.WriteLine("My Net mask: {0}", d.Netmask); 



     }; 

    } 
} 

}

+0

我沒有。但是在添加權限後,它仍然返回0。 –

回答

0

我試圖用DhcpInfo.Netmask讓我的網絡地址,但網絡掩碼我得到的是0,即使我的設備是連接到一個wifi網絡。

這是一個Known Issue

作爲一種變通方法,您可以使用下面的代碼通過this table得到NetworkPrefixLength並將其轉換爲網絡掩碼:

WifiManager wifiManager = (WifiManager)GetSystemService(Context.WifiService); 
DhcpInfo dhcpInfo = wifiManager.DhcpInfo; 
try 
{ 
    byte[] ipAddress = BitConverter.GetBytes(dhcpInfo.IpAddress); 
    InetAddress inetAddress = InetAddress.GetByAddress(ipAddress); 
    NetworkInterface networkInterface = NetworkInterface.GetByInetAddress(inetAddress); 
    foreach (InterfaceAddress address in networkInterface.InterfaceAddresses) 
    { 
     //short netPrefix = address.getNetworkPrefixLength(); 
     //Log.d(TAG, address.toString()); 
     var prefix=address.NetworkPrefixLength; 
    } 
} 
catch (IOException exception) 
{ 
    Log.Debug("Exception:", exception.Message); 
}