如何获取通过手机热点连接的设备数量?

4

众所周知,Android设备具有将手机变成访问点/热点的功能。

在程序上,是否可以获取连接到手机Wifi访问点的设备数量?

1个回答

5
您可以在以下链接中查看如何在 Android 上计算连接设备数量并获取硬件 MAC 地址: http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/ 以上链接中的代码:
/**
 * Try to extract a hardware MAC address from a given IP address using the
 * ARP cache (/proc/net/arp).<br>
 * <br>
 * We assume that the file has this structure:<br>
 * <br>
 * IP address       HW type     Flags       HW address            Mask     Device
 * 192.168.18.11    0x1         0x2         00:04:20:06:55:1a     *        eth0
 * 192.168.18.36    0x1         0x2         00:22:43:ab:2a:5b     *        eth0
 *
 * @param ip
 * @return the MAC from the ARP cache
 */
public static String getMacFromArpCache(String ip) {
    if (ip == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) {
                // Basic sanity check
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    return mac;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

或者如果您在代码上遇到问题,请尝试以下代码:

 public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
        ArrayList<InetAddress> ret = new ArrayList<InetAddress>();

        LoopCurrentIP = 0;

        String IPAddress = "";
        String[] myIPArray = YourPhoneIPAddress.split("\\.");
        InetAddress currentPingAddr;


        for (int i = 0; i <= 255; i++) {
            try {

                // build the next IP address
                currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
                        myIPArray[1] + "." +
                        myIPArray[2] + "." +
                        Integer.toString(LoopCurrentIP));
                ad = currentPingAddr.toString();   /////////////////
                Log.d("MyApp",ad);                 //////////////

                // 50ms Timeout for the "ping"
                if (currentPingAddr.isReachable(50)) {

                    ret.add(currentPingAddr);
                    ad = currentPingAddr.toString();        /////////////////
                    Log.d("MyApp",ad);                     //////////////
                }
            } catch (UnknownHostException ex) {
            } catch (IOException ex) {
            }

            LoopCurrentIP++;
        }
        return ret;
    }

这个不起作用。我设置了手机的AP并将笔记本电脑连接到AP。当我执行“cat /proc/net/arp”时,我看到一个包含我的笔记本电脑的MAC和IP的记录,这是正确的。问题在于,当我从AP断开笔记本电脑时,ARP缓存不会刷新,执行先前的命令仍然会产生一条记录。 - Mridang Agarwalla
虽然你的解决方案听起来是合理的,而且我也在考虑这些方面,但它确实存在一种边缘情况,即设备可能不会响应 ICMP 请求,即 ping。虽然这种方法大多数情况下都能工作,但不能完全被认为是健壮的。我很感激你的建议。点赞。 - Mridang Agarwalla
@StackOverflowUser 我使用了相同的代码,但是一旦设备连接上,它会一直保留在列表中,直到我关闭热点。因此,我得到了错误的计数。 是否有其他方法可以在同一文件中获取更新后的值,或者文件在多长时间后会更新(/proc/net/arp)? 如何刷新同一文件以读取最新连接的设备列表。 - Rahul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接