?1.此方法只能获取众多Mac中的一个
private static string GetMacAddress()
{
string physicalAddress = "";
NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adaper in nice)
{
Debug.Log(adaper.Description);
if (adaper.Description == "en0")
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
break;
}
else
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
if (physicalAddress != "")
{
break;
};
}
}
return physicalAddress;
}
2.此处获取活跃的Mac:当前wifi使用的mac地址,如果想要以太网的mac? 将Wireless80211 改为Ethernet
public static string GetmacAddress()
{
string strphysicalAddress = "";
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var iface in interfaces)
{
if (iface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && iface.OperationalStatus == OperationalStatus.Up)
{
var physicalAddress = iface.GetPhysicalAddress();
var macAddress = physicalAddress.ToString();
Debug.Log("Current MAC address: " + macAddress);
strphysicalAddress = macAddress;
break;
}
else
{
var physicalAddress = iface.GetPhysicalAddress();
var macAddress = physicalAddress.ToString();
Debug.Log("没网的 MAC address: " + macAddress);
}
}
return strphysicalAddress;
}