C# Android DhcpInfo.Netmask返回0
我正在尝试使用DhcpInfo.Netmask获取我的网络地址,但获取的网络掩码为0,即使我的设备已连接到wifi网络。这是我正在使用的代码:C# Android DhcpInfo.Netmask返回0,c#,android,xamarin.android,C#,Android,Xamarin.android,我正在尝试使用DhcpInfo.Netmask获取我的网络地址,但获取的网络掩码为0,即使我的设备已连接到wifi网络。这是我正在使用的代码: namespace checks { [Activity(Label = "checks", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { protected override void OnCreate(Bundle b
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);
};
}
}
}
我正在尝试使用DhcpInfo.Netmask获取我的网络地址,但获取的网络掩码为0,即使我的设备已连接到wifi网络
这是一个很好的例子
作为一种解决方法,您可以使用以下代码获取网络前缀长度,并通过以下方式将其转换为网络掩码:
我没有。但在添加权限后,它仍然返回0。
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);
}