Android 查询局域网内所有ip和mac地址

1:通过java运行cmd命令,来通过arp命令获取同一网络下设备信息,对于支持linux 和windows的设备有效,像一些非智能设备,就无力回天了

2:使用android手机通过向子网内所有设备先发送一遍udp包,实现与在线的设备都进行通信一遍,这样对应的路由信息就自动存储在本地手机中,然后在通过读取android 本机的arp缓存表,来获取设备信息

好,下面详细进行第二种方式的描述:
Step1:首先,获取本机所处的子网段,方法详细代码如下:

    /**
     * 获取ip地址
     *
     * @return
     */
    private static String getHostIP() {
        String hostIp = null;
        try {
            //这里以eth0为例
            hostIp = getIpAddress("eth0");
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return hostIp;
    }
    /**
     * Get Ip address 自动获取IP地址
     * 可以传入eth1,eth0,wlan0,等
     *
     * @throws SocketException
     */
    public static String getIpAddress(String ipType) throws SocketException {
        String hostIp = null;
        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();
            InetAddress ia = null;
            while (nis.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nis.nextElement();

                if (ni.getName().equals(ipType)) {


                    Enumeration<InetAddress> ias = ni.getInetAddresses();
                    while (ias.hasMoreElements()) {

                        ia = ias.nextElement();
                        if (ia instanceof Inet6Address) {
                            continue;// skip ipv6
                        }
                        String ip = ia.getHostAddress();

                        // 过滤掉127段的ip地址
                        if (!"127.0.0.1".equals(ip)) {
                            hostIp = ia.getHostAddress();
                            break;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        Log.d(ipType, "get the IpAddress--> " + hostIp + "");
        return hostIp;
    }

Step2:new出一个线程&#x

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值