.NET MAUI设备的MAC地址

huangapple go评论76阅读模式
英文:

.NET MAUI device MAC address

问题

I am trying to get the MAC address of the current device in .NET MAUI. This is on Android/Windows and iOS.

It looks like NetworkInterfaces is not working as it was.

Consider this code:

public string get_mac()
{
    int c = 0;

    try
    {
        NetworkInterface[] ni = NetworkInterface.GetAllNetworkInterfaces();
        for (c = 0; c < ni.Length; c++)
        {
            if (ni[c].NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            {
                PhysicalAddress pa = ni[c].GetPhysicalAddress();
                return pa.ToString();
            }
        }
        return "!Ethernet Not Found!"
    }
    catch(Exception ex)
    {
        Globals.log.add(ex);
        return "!" + ex.Message;
    }
}

It looks like all of the interfaces are set to a type of "Unknown" and the physical address (MAC) of each is set to "". It looks like it is properly getting the network name.

Is NetworkInterface even valid in .NET MAUI?

英文:

I am trying to get the MAC address of the current device in .NET MAUI. This is on Android/Windows and iOS.

It looks like NetworkInterfaces is not working as it was.

Consider this code:

public string get_mac()
{
    int c = 0;

    try
    {
        NetworkInterface[] ni = NetworkInterface.GetAllNetworkInterfaces();
        for (c = 0; c < ni.Length; c++)
        {
            if (ni[c].NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            {
                PhysicalAddress pa = ni[c].GetPhysicalAddress();
                return pa.ToString();
            }
        }
        return "!Ethernet Not Found!";
    }
    catch(Exception ex)
    {
        Globals.log.add(ex);
        return "!" + ex.Message;
    }
}

It looks like all of the interfaces are set to a type of "Unknown" and the physical address (MAC) of each is set to "". It looks like it is properly getting the network name.

Is NetworkInterface even valid in .NET MAUI?

答案1

得分: 1

我的回答是否定的。

对于iOS,iOS沙盒阻止应用程序获取设备的MAC地址。您可以参考*iPhone的MAC地址Xamarin.iOS中的文件系统访问*。

对于Android,现在我们无法获取MAC地址。您可以参考此问题:如何在Android 11上获取Android设备的MAC地址?以及官方文档不使用MAC地址

英文:

My answer is no.

For iOS, the iOS sandbox prevents an app from getting the device's MAC address. You could refer to MAC Address of the iPhone and File system access in Xamarin.iOS.

For Android, now we cannot. You could refer to this issue: How can I get the MAC address from an Android device with OS 11? and the official documentation Don't work with MAC addresses.

答案2

得分: 0

对于 Windows 平台,这段代码可以正常工作;

对于 Android 和 iOS,这会有一些问题,如Liqun Shen-MSFT所说

英文:

For the Windows platform, this code works properly;

string macAddress = (from nic in NetworkInterface.GetAllNetworkInterfaces()
                         where nic.OperationalStatus == OperationalStatus.Up
                         select nic.GetPhysicalAddress().ToString()
                    ).FirstOrDefault();

It's a bit problematic for Android and iOS, as Liqun Shen-MSFT said.

huangapple
  • 本文由 发表于 2023年4月20日 03:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定