最新连接的设备列表android热点

时间:2017-05-10 12:11:02

标签: android arp hotspot

我使用以下代码读取连接的设备IP地址How can I get the number of devices connected through the phones 但我得到错误的列表,即列表显示连接到热点并且当前已断开连接的早期设备。 有没有其他方法来获得更新列表。或者如何刷新/ proc / net / arp文件以获取最新列表

还阅读了this,它与linux上的arp有关,但是找不到出路。

1 个答案:

答案 0 :(得分:0)

这对我有用,它将显示未连接的设备及其Mac地址

 public int getClientList() {
    int macCount = 0;
    BufferedReader br = null;
    String flushCmd = "sh ip -s -s neigh flush all";
    Runtime runtime = Runtime.getRuntime();
    try {
        runtime.exec(flushCmd, null, new File("/proc/net"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null) {
                // Basic sanity check
                String mac = splitted[3];
                System.out.println("Mac : Outside If " + mac);
                if (mac.matches("..:..:..:..:..:..")) {
                    macCount++;
               /* ClientList.add("Client(" + macCount + ")");
                IpAddr.add(splitted[0]);
                HWAddr.add(splitted[3]);
                Device.add(splitted[5]);*/
                    System.out.println("Mac : " + mac + " IP Address : " + splitted[0]);
                    System.out.println("Mac_Count  " + macCount + " MAC_ADDRESS  " + mac);
                    Toast.makeText(
                            getApplicationContext(),
                            "Mac_Count  " + macCount + "   MAC_ADDRESS  "
                                    + mac, Toast.LENGTH_SHORT).show();

                }
           /* for (int i = 0; i < splitted.length; i++)
                System.out.println("Addressssssss     "+ splitted[i]);*/

            }
        }
    } catch (Exception e) {

    }
    return macCount;
}

尝试一下