Android获取路由网关(上联设备)MAC地址

这篇博客分享了如何在Android系统中通过代码获取路由网关(上联设备)的MAC地址,提供了包含注释的代码示例,确保在有线和无线连接下都能准确获取。对于代码中的类和文件含义,作者建议读者自行搜索了解。
摘要由CSDN通过智能技术生成

 

我这边直接贴代码吧,里面有一些注释:

具体的我就不解释了,但是这种方式我本地测试过,是准确的,包括有线和无线的连接方式,具体里面的一些之类和文件的含义,可以自己百度下。

    /**
     * 获取路由器MAC地址
     * @return
     */
    public String getGatewayMac() {
        String str ="";
        try {
            str = getMacFromFile( getGateway());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     *
     * @param str 根据ip获取到对应mac地址信息
     * @return
     */
    public String getMacFromFile(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        List a = readFile("/proc/net/arp");
        if (a != null && a.size() > 1) {
            for (int i = 1; i < a.size(); i++) {
                List arrayList = new ArrayList();
                String[] split = ((String) a.get(i)).split(" ");
                int i2 = 0;
                while (i2 < split.length) {
                    if (split[i2] != null && split[i2].length() > 0) {
                        arrayList.add(split[i2]);
                    }
                    i2++;
                }
                if (arrayList.size() > 4 && ((String) arrayList.get(0)).equalsIgnoreCase(str)) {
                    return ((String) arrayList.get(3)).toUpperCase();
                }
            }
        }
        return "";
    }

    private static List<String> readFile(String str) {
        IOException e;
        Throwable th;
        File file = new File(str);
        List<String> arrayList = new ArrayList();
        BufferedReader bufferedReader;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            while (true) {
                try {
                    String readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        break;
                    }
                    arrayList.add(readLine);
                } catch (IOException e2) {
                    e = e2;
                }
            }
            bufferedReader.close();
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        } catch (IOException e3) 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值