获取android以太网MAC地址(不是wifi接口)

时间:2011-09-07 11:17:27

标签: java android ethernet mac-address

我正在使用Api级别为8的Android,我想获得以太网接口的地址(eth0)。

在API级别8上,NetworkInterface类没有getHardwareAddress()函数。 WifiManager也不起作用,因为它不是无线接口。

提前致谢!

8 个答案:

答案 0 :(得分:26)

这是基于Joel F答案的解决方案。希望它可以帮助别人!

/*
 * 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;
    }
}

答案 1 :(得分:18)

假设您的以太网接口是eth0,请尝试打开并读取文件/sys/class/net/eth0/address

答案 2 :(得分:2)

这种方式使用java修复它;也许可以帮到你

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);

答案 3 :(得分:1)

至少在Amlogic平台上检查/ sys / class / efuse / mac。

答案 4 :(得分:0)

目前(2014年3月)Google未提供有关Ethernet

的API

这是rason,因为我们没有办法像wifi一样获得以太网mac。

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

另一种方法是读取eth0文件。如果有人不知道,请告诉我!

答案 5 :(得分:0)

也许我的答案会对你们中的一些人有所帮助,至少对你们其他人来说很有趣。

我试图获取Android TV设备的以太网MAC地址,试图找到注册MAC地址的实际制造商。我用adb连接到设备,并使用了上面的Joel F的优秀答案,效果很好。

然后我把盒子翻过来,它就在设备底部的贴纸上。

因此,如果您不是以编程方式需要它,请尝试首先翻转设备。MAC address sticker on bottom of device.

P.S。我联系了制造商注册的地址,他们说他们没有制作这个模型,其他制造商也复制了他们的MAC地址。

答案 6 :(得分:0)

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;
}

答案 7 :(得分:0)

AndroidTV的许多实现可能会将其填充到属性中,您可以使用 getprop 命令进行检查以找到正确的属性名称广告,然后使用 SystemProperties.get()进行阅读

要在Java程序中读取MAC,您可以使用类似以下内容的

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