iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android通用方法获取mac地址和以太网信息ip地址、网关、dns等
  • 497
分享到

Android通用方法获取mac地址和以太网信息ip地址、网关、dns等

tcp/ip网络协议Android开发 2023-09-01 11:09:40 497人浏览 泡泡鱼
摘要

1.关于获取Mac地址的一些方法 第一种方法:读取sys/class/net/路径下的文件 FileInputStream fis_name = null; FileInputStream fis = null;

1.关于获取Mac地址的一些方法

第一种方法:读取sys/class/net/路径下的文件

FileInputStream fis_name = null;        FileInputStream fis = null;        try {//interfaceName 可以直接填写 eth0            String path = "sys/class/net/"+interfaceName+"/address";            fis_name = new FileInputStream(path);            byte[] buffer_name = new byte[1024 * 8];            int byteCount_name = fis_name.read(buffer_name);            if (byteCount_name > 0) {                mac = new String(buffer_name, 0, byteCount_name, "utf-8");            }            if (mac.length() == 0) {                path = "sys/class/net/eth0/wlan0";                fis = new FileInputStream(path);                byte[] buffer = new byte[1024 * 8];                int byteCount = fis.read(buffer);                if (byteCount > 0) {                    mac = new String(buffer, 0, byteCount, "utf-8");                }            }        } catch (Exception e) {            e.printStackTrace();        }finally {            if(fis_name != null){                try {                    fis_name.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(fis != null){                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }

可是上面的方法如果没有放开该文件的读写权限,是读取不到的;

现在介绍另外一种方法:

方法2:采用ConnectivityManager

获取连接的网络信息

ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext().getSystemService(Service.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();

上面是获取当前连接的网络信息对象,如果要使用它,一定要对该对象进行判空和连接的状态判断

可以将当前的网络连接信息全部打印出来

if(activeNetworkInfo != null && activeNetworkInfo.isConnected()){    Log.i("NetworkInfo info  : " +cm.getLinkProperties(cm.getActiveNetwork()).toString() );}

获取mac地址(仅以太网连接的情况下,wifi获取的是当前的wifi名称)

 String extrainfo = activeNetworkInfo.getExtraInfo();

获取ip信息

 List linkAddresses = cm.getLinkProperties(cm.getActiveNetwork()).getLinkAddresses();//获取当前连接的网络ip地址信息if(linkAddresses != null && !linkAddresses.isEmpty()){//注意:同时可以查看到两个网口的信息,但是ip地址不是固定的位置(即下标)//所以遍历的时候需要判断一下当前获取的ip地址是否符合ip地址的正则表达式 for (int i = 0; i < linkAddresses.size(); i++) {     InetAddress address = linkAddresses.get(i).getAddress();//判断ip地址的正则表达     if(isCorrectIp(address.getHostAddress())){       Log.i("ip地址"+address.getHostAddress())         }     } }       

获取网关地址信息:

List routes = cm.getLinkProperties(cm.getActiveNetwork()).getRoutes();  if(routes != null && !routes.isEmpty()){  for (int i = 0; i < routes.size(); i++) {    //和ip地址一样,需要判断获取的网址符不符合正则表达式     String hostAddress = routes.get(i).getGateway().getHostAddress();     if(isCorrectIp(hostAddress)){     Log.i("网关信息:" + hostAddress);        }     }}

获取dns信息:

List dnsServers = cm.getLinkProperties(cm.getActiveNetwork()).getDnsServers();                if(dnsServers != null && dnsServers.size() >= 2){                       Log.i("dns1 " + dnsServers.get(0).toString());                    Log.i("dns2 " + dnsServers.get(1).toString());                }

获取子网掩码地址

     public static String getIpAddreSSMaskForInterfaces(String interfaceName) {        //"eth0"        try {            //获取本机所有的网络接口            Enumeration networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();            //判断 Enumeration 对象中是否还有数据            while (networkInterfaceEnumeration.hasMoreElements()) {                //获取 Enumeration 对象中的下一个数据                NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();                if (!networkInterface.isUp() && !interfaceName.equals(networkInterface.getDisplayName())) {                    //判断网口是否在使用,判断是否时我们获取的网口                    continue;                }                for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {                    if (interfaceAddress.getAddress() instanceof Inet4Address) {                        //仅仅处理ipv4                        //获取掩码位数,通过 calcMaskByPrefixLength 转换为字符串                        return calcMaskByPrefixLength(interfaceAddress.getNetworkPrefixLength());                    }                }            }        } catch (SocketException e) {            e.printStackTrace();               }        return "0.0.0.0";    }

通过获取网口名调用该方法

getIpAddressMaskForInterfaces(cm.getLinkProperties(cm.getActiveNetwork()).getInterfaceName())

以上则是通用获取网络信息的方式;wifi和以太网都可以用,可以试一下;

晚风过花庭

来源地址:https://blog.csdn.net/qq_33796069/article/details/127401634

--结束END--

本文标题: Android通用方法获取mac地址和以太网信息ip地址、网关、dns等

本文链接: https://www.lsjlt.com/news/388340.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作