如何准确扫描Android中连接到wifi的所有设备的IP和Mac地址? [英] How to scan IP and Mac address of all Device Connected to wifi in android accurately?

查看:176
本文介绍了如何准确扫描Android中连接到wifi的所有设备的IP和Mac地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找所有从android连接到我的WIFI路由器"的设备,我需要每个设备(包括iOT设备)的设备Mac地址和本地IP地址,现在,我正在尝试从ARP缓存表中查找.但是有时在扫描中某些设备丢失了,并不是很准确.

Hi i am trying to find all devices connected to My WIFI Router from android and ,i need to device Mac address and local ip address of each device (Including iOT Devices) , right now , i am trying to find from ARP cache table . but sometime in the scan some devices are missing , it is not so accurate .

我的代码:

 List<LocalDeviceInfo> devicesInfos = new ArrayList<>();


        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] splitted = line.split(" +");
                if (splitted != null && splitted.length >= 4) {
                    String ip = splitted[0];
                    String mac = splitted[3];
                    if (mac.matches("..:..:..:..:..:..")) {
                        LocalDeviceInfo thisNode = new LocalDeviceInfo(ip, mac);
                        devicesInfos.add(thisNode);
                    }
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Print Description
        for (LocalDeviceInfo devicesInfo :devicesInfos)
        {
            System.out.print("✅");
            System.out.println("IP : "+devicesInfo.getIp());
            System.out.println("Mac : "+devicesInfo.getMacAddress());
        }


如何扫描以下设备中的所有设备(IP地址和Mac地址)android准确.

How can i scan all devices (IP address and Mac address) in android accurately .


推荐答案

我找到了解决我的问题的方法,大多数设备不在系统arp表中,因此一旦您每次都必须Ping通每个设备ping该设备将其存储在系统ARP表中,该表存储在(/proc/net/arp)

I found the solution of my problem , most of devices was not in the system arp table , so you need to Ping each of device at first time , once you ping that device its will be stored in System ARP Table Which is stored at (/proc/net/arp)

使用ip对所有设备执行ping操作:(首先,您需要找到设备的IP地址,然后才能确定子网掩码,然后可以从(0-255)开始固定

Pinging All devices with ip: ( First you need to find your device's IP Address , than you can determine the subnet mask and you can start pining from (0-255)

代码:

public  void startPingService(Context context)
{
  List<LocalDeviceInfo> deviceInfoList  = new ArrayList<LocalDeviceInfo>();
    try {

        WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
        String subnet = getSubnetAddress(mWifiManager.getDhcpInfo().gateway);


        for (int i=1;i<255;i++){

            String host = subnet + "." + i;

            if (InetAddress.getByName(host).isReachable(timeout)){

                String strMacAddress = getMacAddressFromIP(host);

                Log.w("DeviceDiscovery", "Reachable Host: " + String.valueOf(host) +" and Mac : "+strMacAddress+" is reachable!");

                    LocalDeviceInfo localDeviceInfo = new LocalDeviceInfo(host,strMacAddress);
                    deviceInfoList.add(localDeviceInfo);
             }
            else
            {
                Log.e("DeviceDiscovery", "❌ Not Reachable Host: " + String.valueOf(host));

            }
        }


    }
    catch(Exception e){
        //System.out.println(e);
    }


}


private String getSubnetAddress(int address)
{
    String ipString = String.format(
            "%d.%d.%d",
            (address & 0xff),
            (address >> 8 & 0xff),
            (address >> 16 & 0xff));

    return ipString;
}

从ARP缓存表中获取Mac地址

public String getMacAddressFromIP(@NonNull String ipFinding)
{

    Log.i("IPScanning","Scan was started!");
    List<LocalDeviceInfo> antarDevicesInfos = new ArrayList<>();


    BufferedReader bufferedReader = null;

    try {
        bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                String ip = splitted[0];
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {

                    if (ip.equalsIgnoreCase(ipFinding))
                    {
                        return mac;
                    }
                }
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return "00:00:00:00";
}

您也需要这些权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

这篇关于如何准确扫描Android中连接到wifi的所有设备的IP和Mac地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆