在Xamarin的棉花糖上获取Mac地址

时间:2016-04-08 16:13:12

标签: android xamarin android-6.0-marshmallow mac-address

我一直在尝试使用Android 6.0来获取MAC的几件事情但没有成功):

我已拥有此权限:

public string GetMacAdress(Context context)
{
    string mac = GetMacAddressLegacy(context);

    if (mac == "02:00:00:00:00:00")
    {
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface nif in interfaces)
        {
            if (!nif.Name.ToLower().Contains("wlan")) continue;

            var physicalAddress = nif.GetPhysicalAddress();
            byte[] macBytes = physicalAddress.GetAddressBytes();
            if (macBytes == null) continue;

            string macString = BitConverter.ToString(macBytes);
            if (!string.IsNullOrWhiteSpace(macString)) mac = macString.Trim().ToUpper().Replace("-", ":");
        }
    }

    return mac;
}

[Obsolete]
public string GetMacAddressLegacy(Context context)
{
    string toReturn =  "02:00:00:00:00:00";
    if (DetectWifiNetwork())
    {
        isConected = true;
        var telephonyMgr = (WifiManager)context.GetSystemService(Context.WifiService);
        toReturn = telephonyMgr.ConnectionInfo.MacAddress;
        if (!string.IsNullOrWhiteSpace(toReturn)) toReturn = toReturn.Trim().ToUpper();
    }
    else
    {
        isConected = false;
    }
    return toReturn;
}

这是我的代码:

byte[] macBytes = physicalAddress.GetAddressBytes();

但是这一行:div { width: 200px; height: 200px; border: 2px solid red; } div img { object-fit: cover; }返回一个空数组。

任何人都可以解决这个问题吗?

3 个答案:

答案 0 :(得分:3)

尝试使用此方法

public static string getMacAddress()
{
    string macAddress = string.Empty;

    var all = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);

    foreach (var interfaces in all)
    {
        if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("wlan0")) continue;

        var macBytes = (interfaces as 
        Java.Net.NetworkInterface).GetHardwareAddress();
        if (macBytes == null) continue;

        var sb = new System.Text.StringBuilder();
        foreach (var b in macBytes)
        {
            string convertedByte = string.Empty;
            convertedByte = (b & 0xFF).ToString("X2") + ":";

            if(convertedByte.Length == 1)
            {
                convertedByte.Insert(0, "0");
            }
            sb.Append(convertedByte);
        }

        macAddress = sb.ToString().Remove(sb.Length - 1);

        return macAddress;
    }
    return "02:00:00:00:00:00";
}

答案 1 :(得分:1)

尝试使用此代码:

public string GetMacAdress(Context context)
{
    string mac = GetMacAddressLegacy(context);

    if (mac == "02:00:00:00:00:00")
    {
        var interfaces = Java.Net.NetworkInterface.NetworkInterfaces;

        foreach (var nif in interfaces)
        {
            if (!nif.Name.ToLower().Contains("wlan")) continue;

            byte[] macBytes = nif.GetHardwareAddress();

            string macString = BitConverter.ToString(macBytes);
            if (!string.IsNullOrWhiteSpace(macString))   
                mac = macString.Trim().ToUpper().Replace("-", ":");
        }
    }

    return mac;
}

答案 2 :(得分:-1)

使用它足以使它工作:

权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

代码:

WifiManager wManager = (WifiManager)GetSystemService(Context.WifiService);
WifiInfo wInfo = wManager.ConnectionInfo;
button.Text =  wInfo.MacAddress;

您是在真实设备上还是在模拟器上进行测试?