【问题标题】:Get the list of all the devices connected within same Network Android获取同一网络 Android 中连接的所有设备的列表
【发布时间】:2020-06-09 23:05:47
【问题描述】:

在 Android 中,

  1. 如何获取同一WiFi网络中连接的设备列表?

  2. 如何从连接到同一 WiFi 网络的智能手机中获取最近的设备?

  3. 频率检查是否能够找到最近的设备?

【问题讨论】:

  • 你必须做些什么来实现这个目标?
  • 获取设备信息并连接到设备。
  • 我现在处于研发阶段,如何展示代码?
  • 如果你有任何建议,欢迎
  • 检查thisthis答案

标签: android


【解决方案1】:

我找到了连接在同一网络中的设备列表的解决方案,并将代码放在下面

在onCreate中调用该方法-> getClientList(true, 200);

在我们的类中编写这个方法

  public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
    BufferedReader br = null;
    ArrayList<ClientScanResult> result = null;
    try {
        result = new ArrayList<ClientScanResult>();
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        int i = 0;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if ((splitted != null) && (splitted.length >= 4)) {
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);

                    if (!onlyReachables || isReachable) {
                        result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
                        i++;
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.e(this.getClass().toString(), e.getMessage());
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            Log.e(this.getClass().toString(), e.getMessage());
        }
    }
    return result;
}

如有任何澄清,请回复此帖子的消息, 谢谢

【讨论】:

  • 文件 /proc/net/arp 不包含连接到同一网络的所有设备。它只是最近连接设备的缓存。见stackoverflow.com/questions/16035636/…
  • @Narendran 获取连接列表后如何连接?
  • 是否可以获得所有连接设备的mac ID和LocalIP?
猜你喜欢
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2022-07-16
  • 2013-05-04
相关资源
最近更新 更多