Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 806
inversus Avatar asked Sep 07 '11 11:09

inversus


2 Answers

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;
    }
}
like image 119
inversus Avatar answered Sep 21 '22 10:09

inversus


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

like image 21
Joel F Avatar answered Sep 20 '22 10:09

Joel F