本文介绍了Xamarin.Android DhcpInfo.Netmask 返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DhcpInfo.Netmask 获取我的网络地址,但我得到的网络掩码为 0,即使我的设备已连接到 wifi 网络.这是我正在使用的代码:

I'm trying to get my network address with DhcpInfo.Netmask but the net mask I get is 0, even though my device is connected to a wifi network. This is the code I'm using:

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



        };

    }
}

}

推荐答案

这是一个已知问题.

作为一种解决方法,您可以使用以下代码获取 NetworkPrefixLength 并通过 这张表:

As a workaround, you can use the following codes to get the NetworkPrefixLength and convert it to NetMask by this table:

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

这篇关于Xamarin.Android DhcpInfo.Netmask 返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 05:50