Java android热点只获取连接客户端列表

时间:2017-09-08 05:44:03

标签: java android

我想得到当前连接到我的热点的客户列表我这样做了:

public List<WifiClient> getClients() {
List<WifiClient> result = new ArrayList<>();
result.clear();

// Basic sanity checks
Pattern macPattern = Pattern.compile("..:..:..:..:..:..");

BufferedReader br = null;
try {
    br = new BufferedReader(new FileReader("/proc/net/arp"));
    String line;
    while ((line = br.readLine()) != null) {
        String[] parts = line.split(" +");
        if (parts.length < 6) {
            continue;
        }

        String ipAddr = parts[0];
        String hwAddr = parts[3];
        String device = parts[5];

        if (!device.equals(deviceName)) {
            continue;
        }
        if (!macPattern.matcher(parts[3]).find()) {
            continue;
        }
        boolean isReachable = InetAddress.getByName(ipAddr).isReachable(1000);  // this is network call so we cant do that on UI thread, so i take background thread.
        if (isReachable) {
            result.add(new WifiClient(ipAddr, hwAddr));
        }
    }
} catch (IOException e) {
    Log.e(TAG, "", e);
} finally {
    try {
        if (br != null) {
            br.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "", e);
    }
}
return result;
}

但我在这里暂停:

            `boolean isReachable = InetAddress.getByName(ipAddr).isReachable(1000);  // this is network call so we cant do that on UI thread, so i take background thread.`

当我不这样做时,我列出了所有已连接的用户,但现在没有。

2 个答案:

答案 0 :(得分:0)

由于您的-F'...'在循环之外,如果捕获到异常,循环将不会继续。

try-catch

答案 1 :(得分:0)

您确定需要/proc/net/arp才能获得连接的客户端吗?它是一个ARP表,将您的本地网络地址映射到硬件地址。

请考虑使用/proc/net/tcp - 它列出所有打开的TCP套接字。见http://www.linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html