inversus
inversus

Reputation: 1317

Get android Ethernet MAC Address (not wifi interface)

I'm using Android with Api level 8 and I want to get the Address of my Ethernet interface (eth0).

On API level 8, the NetworkInterface class don't have the function getHardwareAddress(). The WifiManager also does not work since this is not an Wireless interface.

Thanks in advance!

Upvotes: 18

Views: 27736

Answers (9)

alizeyn
alizeyn

Reputation: 2420

Kotlin version of 'a.prateek' solution:

fun getEthernetMacAddress(): String? {
    try {
        val allNetworkInterfaces: List<NetworkInterface> = Collections.list(NetworkInterface.getNetworkInterfaces())
        for (networkInterface in allNetworkInterfaces) {
            if (!networkInterface.name.contains("eth0", ignoreCase = true)) continue
            val macBytes = networkInterface.hardwareAddress ?: continue
            val macStringBuilder = StringBuilder()
            for (byte in macBytes) {
                macStringBuilder.append(String.format("%02X:", byte))
            }
            if (macStringBuilder.isNotEmpty()) {
                macStringBuilder.deleteCharAt(macStringBuilder.length - 1)
            }
            return macStringBuilder.toString()
        }
    } catch (e: Exception) {
        Timber.e(e)
    }
    return null
}

Upvotes: 0

Kunal Vaidya
Kunal Vaidya

Reputation: 1

Many implementations of AndroidTV may have it populated in property, you could check with getprop command to find the correct property name ad then read it using SystemProperties.get()

for reading the MAC in the program in java you could use something like following

SystemProperties.get("ro.boot.ethernet-mac");

Upvotes: 0

a.prateek
a.prateek

Reputation: 135

public static String getEthernetMacAddress() {
    String macAddress = "Not able to read";
    try {
        List<NetworkInterface> allNetworkInterfaces = Collections.list(NetworkInterface
                .getNetworkInterfaces());
        for (NetworkInterface nif : allNetworkInterfaces) {
            if (!nif.getName().equalsIgnoreCase("eth0")) 
                continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return macAddress;
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(String.format("%02X:", b));
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            macAddress = res1.toString();
        }
    } catch (Exception ex) {
        log(LogLevel.ERROR, "getEthernetMacAddress e :" + ex.getMessage());
        ex.printStackTrace();
    }
    return macAddress;
}

Upvotes: 3

Shane E.
Shane E.

Reputation: 85

Maybe my answer will be helpful to a few of you out there, and at least funny for the rest of you.

I was trying to get the ethernet MAC address for an Android TV device, to try to find the actual manufacturer the MAC address is registered to. I connected to the device with adb and used Joel F's great answer above and it worked great.

Then I flipped the box over and there it is on a sticker on the bottom of the device.

So if you don't need it programatically, try flipping the device over first.MAC address sticker on bottom of device.

P.S. I contacted the manufacturer the address is registered to, and they said they don't make this model and that other manufacturers copy their MAC addresses.

Upvotes: 1

聂培申
聂培申

Reputation: 31

this way to use java fix it; maybe can help you

NetworkInterface netf = NetworkInterface.getByName("eth0");
byte[] array = netf.getHardwareAddress();
StringBuilder stringBuilder = new StringBuilder("");
String str = "";
for (int i = 0; i < array.length; i++) {
    int v = array[i] & 0xFF;
    String hv = Integer.toHexString(v).toUpperCase();
    if (hv.length() < 2) {
        stringBuilder.append(0);
    }
    stringBuilder.append(hv).append("-");                   
}
str = stringBuilder.substring(0, stringBuilder.length()- 1);

Upvotes: 2

villanuevaquelin
villanuevaquelin

Reputation: 944

Right now (March 2014) Google doesn't privides an API about Ethernet

This is the rason because we don't have a way to get the ethernet mac like in wifi case.

private String getWifiMACAddress(Context ctx) {
    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
   return info.getMacAddress();
}

One alternative is reading the eth0 file. Please let me know if anyone knows otherwise!

Upvotes: 0

Ubaldo Porcheddu
Ubaldo Porcheddu

Reputation: 21

Check also /sys/class/efuse/mac at least on Amlogic platforms.

Upvotes: 1

inversus
inversus

Reputation: 1317

This is my solution based on the Joel F answer. Hope it helps someone!

/*
 * Load file content to String
 */
public static String loadFileAsString(String filePath) throws java.io.IOException{
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead=0;
    while((numRead=reader.read(buf)) != -1){
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
    }
    reader.close();
    return fileData.toString();
}

/*
 * Get the STB MacAddress
 */
public String getMacAddress(){
    try {
        return loadFileAsString("/sys/class/net/eth0/address")
            .toUpperCase().substring(0, 17);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Upvotes: 29

Joel F
Joel F

Reputation: 2631

Assuming your ethernet interface is eth0, try opening and reading the file /sys/class/net/eth0/address.

Upvotes: 25

Related Questions