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


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

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

    1. FileInputStream fis_name = null;
    2. FileInputStream fis = null;
    3. try {
    4. //interfaceName 可以直接填写 eth0
    5. String path = "sys/class/net/"+interfaceName+"/address";
    6. fis_name = new FileInputStream(path);
    7. byte[] buffer_name = new byte[1024 * 8];
    8. int byteCount_name = fis_name.read(buffer_name);
    9. if (byteCount_name > 0) {
    10. mac = new String(buffer_name, 0, byteCount_name, "utf-8");
    11. }
    12. if (mac.length() == 0) {
    13. path = "sys/class/net/eth0/wlan0";
    14. fis = new FileInputStream(path);
    15. byte[] buffer = new byte[1024 * 8];
    16. int byteCount = fis.read(buffer);
    17. if (byteCount > 0) {
    18. mac = new String(buffer, 0, byteCount, "utf-8");
    19. }
    20. }
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }finally {
    24. if(fis_name != null){
    25. try {
    26. fis_name.close();
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. }
    31. if(fis != null){
    32. try {
    33. fis.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }

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

    现在介绍另外一种方法:

    方法2:采用ConnectivityManager

    1.获取连接的网络信息

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

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

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

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

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

     String extraInfo = activeNetworkInfo.getExtraInfo();

    3.获取ip信息

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

    4.获取网关地址信息:

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

    5.获取dns信息:

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

    6:获取子网掩码地址

    1. /**
    2. * 获取子网掩码
    3. * @param interfaceName
    4. * @return
    5. */
    6. public static String getIpAddressMaskForInterfaces(String interfaceName) {
    7. //"eth0"
    8. try {
    9. //获取本机所有的网络接口
    10. Enumeration networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
    11. //判断 Enumeration 对象中是否还有数据
    12. while (networkInterfaceEnumeration.hasMoreElements()) {
    13. //获取 Enumeration 对象中的下一个数据
    14. NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
    15. if (!networkInterface.isUp() && !interfaceName.equals(networkInterface.getDisplayName())) {
    16. //判断网口是否在使用,判断是否时我们获取的网口
    17. continue;
    18. }
    19. for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
    20. if (interfaceAddress.getAddress() instanceof Inet4Address) {
    21. //仅仅处理ipv4
    22. //获取掩码位数,通过 calcMaskByPrefixLength 转换为字符串
    23. return calcMaskByPrefixLength(interfaceAddress.getNetworkPrefixLength());
    24. }
    25. }
    26. }
    27. } catch (SocketException e) {
    28. e.printStackTrace();
    29. }
    30. return "0.0.0.0";
    31. }

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

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

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

    晚风过花庭

  • 相关阅读:
    编辑照片时,出现闪退
    SLAM中的李群和李代数
    C++面试八股文:了解sizeof操作符吗?
    go结构体、集合和高阶函数
    【Python学习笔记】字符编码
    转载——比较器的原理
    STM32开发(三十一)STM32F103 片内资源 —— 模拟/数字转换 DAC 正弦波 编程详解
    题解《子数整数》、《欢乐地跳》、《开灯》
    因误删文件导致CentOS7开机卡死无法进入图形登录界面
    JavaScript 函数
  • 原文地址:https://blog.csdn.net/qq_33796069/article/details/127401634