Get android Ethernet MAC Address (not wifi interface)

后端 未结 8 1754
南旧
南旧 2021-02-05 12:12

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 ge

相关标签:
8条回答
  • 2021-02-05 12:42

    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");
    
    0 讨论(0)
  • 2021-02-05 12:44

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题