android - 在 Xamarin 中获取 Marshmallow 上的 Mac 地址

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

我一直在尝试多种方法来让 MAC 运行 Android 6.0,但没有成功):

我已经拥有此权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

这是我的代码:

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();返回一个空数组。

有人能解决这个问题吗?

最佳答案

尝试使用这个方法

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

关于android - 在 Xamarin 中获取 Marshmallow 上的 Mac 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36504424/

相关文章:

android - 如何以编程方式在应用程序设置中打开应用程序权限窗口

android - Android App在长时间闲置时不断崩溃(API 24和23)

android - 如何在基于值的圆形大小的android中绘制带有动画的圆形

c# - 确定使用 Azure 移动应用服务拉取了多少更改?

android - RenderScript:无法打开共享库:(空)

c# - 我可以根据条件禁用 ViewCell.ContextActions

c# - 从另一个 View 的中心开始到其左边界的水平展开动画

android - 如何在 Android 中使用 KeyGenerator for FingerPrint API 生成 key

android - 将 JSON 格式的 OpenCV 图像从 Python 发送到 Android

android - Google Maps Android API v2 : Failed to Load Map. 无法联系 Google 服务器