使用.NET MAUI从Android和iOS设备获取IP地址

发布于 2025-02-04 11:12:40 字数 1834 浏览 2 评论 0原文

我正在尝试使用.NET MAUI从Android设备获得IP地址。 (当前的Android Framework Net6.0 Android)

使用以下代码,我可以在该应用程序的第一次启动时获得IP。除非我重新启动手机,否则它不适用于启动应用程序。(在Samsung S10+ Android 12上进行了测试)

    private void GetIPAddressClicked(object sender, EventArgs e)
{

    string interfaceDescription = string.Empty;
    var result = new List<IPAddress>();
    try
    {
        var upAndNotLoopbackNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces().Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback
                                                                                                      && n.OperationalStatus == OperationalStatus.Up);

        foreach (var networkInterface in upAndNotLoopbackNetworkInterfaces)
        {
            var iPInterfaceProperties = networkInterface.GetIPProperties();

            var unicastIpAddressInformation = iPInterfaceProperties.UnicastAddresses.FirstOrDefault(u => u.Address.AddressFamily == AddressFamily.InterNetwork);
            if (unicastIpAddressInformation == null) continue;

            result.Add(unicastIpAddressInformation.Address);

            interfaceDescription += networkInterface.Description + "---";
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine($"Unable to find IP: {ex.Message}");
    }
    finally
    {
        string allIpInfo = string.Empty;
        foreach (var item in result)
        {
            allIpInfo += item.ToString() + "---";
        }
        IPAddress.Text = allIpInfo;

        InterfaceName.Text = interfaceDescription;
    }

在工作时,

FetfaceScription =“ -------”

AllipInfo =“ 192.168.50.50.112 ------ 127.0。 0.1 ---“

当它不起作用时,

交织时 - &gt; rmnet_data0

allipinfo ---&gt; 192.0.0.2

我不确定这里有什么问题。我该如何解决?还是有更好的方法来获取IP地址? 谢谢。

I am trying to get IP address from android device using .NET MAUI. (Current android framework net6.0-android)

With the below code, I can get the IP with the first launch of the application. And it does not work with following launch of the application unless I reboot my phone.(Tested on Samsung S10+ Android 12)

    private void GetIPAddressClicked(object sender, EventArgs e)
{

    string interfaceDescription = string.Empty;
    var result = new List<IPAddress>();
    try
    {
        var upAndNotLoopbackNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces().Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback
                                                                                                      && n.OperationalStatus == OperationalStatus.Up);

        foreach (var networkInterface in upAndNotLoopbackNetworkInterfaces)
        {
            var iPInterfaceProperties = networkInterface.GetIPProperties();

            var unicastIpAddressInformation = iPInterfaceProperties.UnicastAddresses.FirstOrDefault(u => u.Address.AddressFamily == AddressFamily.InterNetwork);
            if (unicastIpAddressInformation == null) continue;

            result.Add(unicastIpAddressInformation.Address);

            interfaceDescription += networkInterface.Description + "---";
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(
quot;Unable to find IP: {ex.Message}");
    }
    finally
    {
        string allIpInfo = string.Empty;
        foreach (var item in result)
        {
            allIpInfo += item.ToString() + "---";
        }
        IPAddress.Text = allIpInfo;

        InterfaceName.Text = interfaceDescription;
    }

When it works,

interfaceDescription = "------"

allIpInfo = "192.168.50.112---127.0.0.1---"

When it does not work,

interfaceDescription ---> rmnet_data0

allIpInfo ---> 192.0.0.2

I am not sure what is wrong here. How can I fix this? Or is there a better way to get the IP address?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2025-02-11 11:12:40

在Alternativa中,我可以推荐此方法。它仅返回IP地址,而不像上面代码中的整个列表一样返回。

导入这些库

#if ANDROID
using Android.App;
using Android.Net.Wifi;
#endif

,并将此代码

var result = "";
#if ANDROID
            WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Service.WifiService);
            int ipaddress = wifiManager.ConnectionInfo.IpAddress;
            IPAddress ipAddr = new IPAddress(ipaddress);  
            result = ipAddr.ToString();
#endif

用于Windows您可以使用

var result = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString();

In alternativa, I can recommend this method. It returns only the IP address and not the entire list like in the code above.

Import these libraries

#if ANDROID
using Android.App;
using Android.Net.Wifi;
#endif

And use this code

var result = "";
#if ANDROID
            WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Service.WifiService);
            int ipaddress = wifiManager.ConnectionInfo.IpAddress;
            IPAddress ipAddr = new IPAddress(ipaddress);  
            result = ipAddr.ToString();
#endif

For Windows u can use

var result = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文