Android获取设备有线网卡、无线网卡的MAC地址

发布时间:2024年01月12日

#核心点#

cat /sys/class/net/eth0/address
cat /sys/class/net/wlan0/address

Linux系统是规范化的,需要的东西一定会查找到具体的文件

通过运行shell命令的方式查找设备的MAC地址,通用性强、简洁易懂

    protected String getMac() {

        String mac = "";
        String command1 = "cat /sys/class/net/eth0/address";
        String command2 = "cat /sys/class/net/wlan0/address";
        mac = runExe(command1);
        Log.i("dd",mac);
        if(mac.isEmpty()) {
            mac=runExe(command2);
        }
        return mac;
    }

    public String runExe(String command) {
        Runtime mRuntime = Runtime.getRuntime();
        String result = "";
        try {
            Process mProcess = mRuntime.exec(command);
            BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
            StringBuffer mRespBuff = new StringBuffer();

            char[] buff = new char[1024];
            int ch = 0;
            while ((ch = mReader.read(buff)) != -1) {
                mRespBuff.append(buff, 0, ch);
            }
            mReader.close();
            result = mRespBuff.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

文章来源:https://blog.csdn.net/Mosicol/article/details/135557110
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。