无法使用Android版Ethernet Manager检索有线网络的IP和MAC地址

问题描述 投票:0回答:1

我为Android 4.4设备开发了一个APP,在其中尝试使用EthernetManager检索有线网络的IP地址和MAC地址。现在,当我尝试在Android 5.1.1设备上运行此应用程序时,没有获得IP地址和MAC地址。我已经提供了超级用户访问权限。

我在使它正常工作时遇到困难:

public string GetInterfaceName()=>(string)ethernetManager.Class.GetMethod("getEthernetIfaceName").Invoke(ethernetManager);

错误日志:

2020-02-18 16:20:37.8485 [INFO] [4] HttpService-尝试通过“ GetWiredNetworkConfiguration”调用获取有线网络连接数据。2020-02-18 16:20:37.9114 [ERROR] [4] HttpService-getEthernetIfaceName []2020-02-18 16:20:37.9114 [INFO] [4] HttpServer-对请求/有线网络/配置的响应:Java.Lang.NoSuchMethodException:getEthernetIfaceName []在Java.Interop.JniEnvironment + InstanceMethods.CallObjectMethod(Java.Interop.JniObjectReference实例,Java.Interop.JniMethodInfo方法,Java.Interop.JniArgumentValue * args)中[0x0006e]在:0中在Java.Interop.JniPeerMembers + JniInstanceMethods.InvokeAbstractObjectMethod中(System.String编码的成员,Java.Interop.IJavaPeerable自我,Java.Interop.JniArgumentValue *参数)[0x00014],在:0中在Java.Lang.Class.GetMethod(System.String名称,Java.Lang.Class [] parameterTypes)中:[0x00043],其位于:0在<3433561ce2794b26999a934f0ccf7c2a>:0中的PatientPoint.Droid.NetworkConfiguration.Wired.EthernetManagerProxy.GetInterfaceName()[0x00015]处在<3433561ce2794b26999a934f0ccf7c2a>:0中的PatientPoint.Droid.NetworkConfiguration.Wired.WiredNetworkConnectionProvider.GetWiredNetworkConfiguration()[0x00000]处在<33f384394b3e44efac7c863885fc43f1>:0中的Deadpool.Droid.Core.HttpService.GetWiredNetworkConfiguration()[0x00015]处---结束的Java.Lang.NoSuchMethodException堆栈跟踪-java.lang.NoSuchMethodException:getEthernetIfaceName []在java.lang.Class.getMethod(Class.java:664)在java.lang.Class.getMethod(Class.java:643)

获得MAC地址的功能:

private string GetMacAddress(string interfaceName)
{
    var ethernetInterface = NetworkInterface.GetByName(interfaceName);
    if (ethernetInterface != null)
    {
        var bytes = ethernetInterface.GetHardwareAddress();
        if (bytes != null)
        {
            var result = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                result.Append($"{bytes[i]:X2}");
                if (i != bytes.Length - 1)
                {
                    result.Append(":");
                }
            }
            return result.ToString();
        }
    }
    return "Not Available";
}

请告知以上方法是否适用于Android 4.4,或者我是否必须对Android 5.1.1使用其他方法,或者我在这里做错了什么?

java c# android xamarin.android ethernet
1个回答
0
投票

[如果要获取android设备的mac地址,则可以尝试以下代码。我没有Android 4.4和5.1.1,未经允许,我在Android 6.0上进行了测试,效果很好。

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

从代码获取:

enter image description here

从Android设备获取:

enter image description here

我从类似的线程获取代码:https://stackoverflow.com/a/43981078/11850033

© www.soinside.com 2019 - 2024. All rights reserved.