Android Question MAC Address of LAN?

aidymp

Well-Known Member
Licensed User
Longtime User
Hi im looking for the MAC address of the Lan connection, I have seen 2 libs and some java that give you the MAC address of WIFI, but nothing regarding LAN! from experience the LAN address is different to the WIFI address. I was hoping I could modify the below to find either the LAN or WIFI address?

B4X:
Sub Get_Mac As String
Dim R As Reflector
R.Target = R.GetContext
R.Target = R.RunMethod2("getSystemService", "wifi", "java.lang.String")
R.Target = R.RunMethod("getConnectionInfo")
Log(R.RunMethod("getMacAddress"))
Return R.RunMethod("getMacAddress")
End Sub


Thanks

Aidy
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that starting from Android 6 the above code will always return "02:00:00:00:00:00".

You can use this code to get the MAC address:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject
   jo.InitializeContext
   Dim b() As Byte = jo.RunMethod("getMac", Array("eth0"))
   Dim bc As ByteConverter
   Log("Mac: " & bc.HexFromBytes(b))
End Sub


#if JAVA
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static byte[] getMac(String interfaceName) throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     NetworkInterface ni = e.nextElement();
     if (ni.getName().equals(interfaceName)) {
       return ni.getHardwareAddress();
     }
  }
    return new byte[0];
  }
 
#End If
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
I far as I understand the code posted by Erel returns the MAC of the own system. In order to use this MAC as an ID in a network, it would be no difference to any other selected ID. The system would be sending its own MAC, with nobody being able to verify.
But I might have misunderstood something.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Note that starting from Android 6 the above code will always return "02:00:00:00:00:00".

Will there be a work around for this?

Thanks BTW the code is as ever FLAWLESS! ;)

Aidy
 
Last edited:
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
I far as I understand the code posted by Erel returns the MAC of the own system. In order to use this MAC as an ID in a network, it would be no difference to any other selected ID. The system would be sending its own MAC, with nobody being able to verify.
But I might have misunderstood something.

I use the mac as an identifier, my apps are for TV boxes, they are very rarely connected by wifi it works well for me, but i made the mistake of beliveing the mac address would be returned for whatever devices is being used! I was wrong!

Thanks

Aidy
 
Upvote 0
Top