Get android Ethernet MAC Address (not wifi interface)
Solution 1
Assuming your ethernet interface is eth0, try opening and reading the file /sys/class/net/eth0/address
.
Solution 2
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;
}
}
Solution 3
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);
Solution 4
Check also /sys/class/efuse/mac at least on Amlogic platforms.
Solution 5
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.
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.
inversus
Updated on December 03, 2020Comments
-
inversus over 3 years
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!