【问题标题】:How to scan IP and Mac address of all Device Connected to wifi in android accurately?如何准确扫描android中所有连接到wifi的设备的IP和Mac地址?
【发布时间】:2018-08-04 12:46:51
【问题描述】:

您好,我正在尝试从 android 查找连接到我的 WIFI 路由器的所有设备,我需要每个设备的设备 Mac 地址和本地 IP 地址(包括 iOT 设备), 现在,我正在尝试从 ARP 缓存表中查找。但有时在扫描中缺少一些设备,它不是那么准确。

我的代码:

 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 地址) 安卓准确。


【问题讨论】:

    标签: java android nsd


    【解决方案1】:

    我找到了我的问题的解决方案,大多数设备不在系统 ARP 表中,所以你需要在第一次 Ping 每个设备,一旦你 ping 那个设备,它就会存储在系统 ARP 表中存储在(/proc/net/arp)

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

    代码:

    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" />
    

    【讨论】:

    • 对..添加权限是非常必要的,有时会在专注于代码时留下
    • 使用 ping 有很大帮助。 InetAddress.getByName(host.getHostAddress()).isReachable(3000)。有没有其他方法可以获取mac地址?如果网络计算机不回复 ping/icmp 怎么办?
    【解决方案2】:

    实际上,您只需要 ping 设备网络的广播地址,就可以使 ICMP ECHO 请求到达网络上的每一台机器。无需单独ping。另一方面,作为“安全预防措施”,某些设备不会响应此类 ping。这取决于你。

    然后,执行另一个答案中的代码 - 解析包含 ARP 缓存的 /proc/net/arp。从 procfs 中的那个特殊文件中,您将找到网络中网络设备的 MAC 地址。 (将其作为答案发布,因为我还没有足够的声誉将此作为评论添加到其他答案)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2015-03-22
      • 2015-10-30
      • 2021-02-26
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      相关资源
      最近更新 更多