I have developed an APP for an Android 4.4 device in which I am trying to retrieve the IP Address and MAC Address using EthernetManager for the wired network. Now when I am trying to run this application on the Android 5.1.1 device, I am not getting the IP Address and MAC Address. I have already provided the superuser access.
I'm having trouble getting this to work:
public string GetInterfaceName()=>(string)ethernetManager.Class.GetMethod("getEthernetIfaceName").Invoke(ethernetManager);
The error log:
2020-02-18 16:20:37.8485 [INFO] [4] HttpService - Attempting to get wired network connection data via 'GetWiredNetworkConfiguration' call. 2020-02-18 16:20:37.9114 [ERROR] [4] HttpService - getEthernetIfaceName [] 2020-02-18 16:20:37.9114 [INFO] [4] HttpServer - Response to request /wired-network/configuration: Java.Lang.NoSuchMethodException: getEthernetIfaceName [] at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in :0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in :0 at Java.Lang.Class.GetMethod (System.String name, Java.Lang.Class[] parameterTypes) [0x00043] in :0 at PatientPoint.Droid.NetworkConfiguration.Wired.EthernetManagerProxy.GetInterfaceName () [0x00015] in <3433561ce2794b26999a934f0ccf7c2a>:0 at PatientPoint.Droid.NetworkConfiguration.Wired.WiredNetworkConnectionProvider.GetWiredNetworkConfiguration () [0x00000] in <3433561ce 2794b26999a934f0ccf7c2a>:0 at Deadpool.Droid.Core.HttpService.GetWiredNetworkConfiguration () [0x00015] in <33f384394b3e44efac7c863885fc43f1>:0 --- End of managed Java.Lang.NoSuchMethodException stack trace --- java.lang.NoSuchMethodException: getEthernetIfaceName [] at java.lang.Class.getMethod(Class.java:664) at java.lang.Class.getMethod(Class.java:643)
The function to get the MAC Address:
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";
}
Please advise if the above method works for Android 4.4 or do I have to use some other method for Android 5.1.1 or am I doing something wrong here?
If you want to get the mac address of android device, you could try the code below. I do not have Android 4.4 and 5.1.1, i test on Android 6.0 with no permission, it works well.
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";
}
Get from the code:
Get from Android device:
I get the code from the similar thread: https://stackoverflow.com/a/43981078/11850033
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.