Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取通过电话接入点连接的设备数量?_Java_Android - Fatal编程技术网

Java 如何获取通过电话接入点连接的设备数量?

Java 如何获取通过电话接入点连接的设备数量?,java,android,Java,Android,众所周知,安卓设备具有将手机变成接入点/热点的功能 通过编程是否可以获取连接到手机wifi接入点的设备数量?您可以计算接入点上连接的设备数量,并在android上的以下链接上获取硬件mac地址: 以上链接的代码: /** * Try to extract a hardware MAC address from a given IP address using the * ARP cache (/proc/net/arp).<br> * <br> * We ass

众所周知,安卓设备具有将手机变成接入点/热点的功能


通过编程是否可以获取连接到手机wifi接入点的设备数量?

您可以计算接入点上连接的设备数量,并在android上的以下链接上获取硬件mac地址:

以上链接的代码:

/**
 * 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;
}
/**
*尝试使用从给定IP地址提取硬件MAC地址
*ARP缓存(/proc/net/ARP)。
*
*我们假设该文件具有以下结构:
*
*IP地址HW类型标志HW地址掩码设备 *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 *@从ARP缓存返回MAC */ 公共静态字符串getMacFromArpCache(字符串ip){ 如果(ip==null) 返回null; BufferedReader br=null; 试一试{ br=新的BufferedReader(新的文件读取器(“/proc/net/arp”); 弦线; 而((line=br.readLine())!=null){ String[]splitted=line.split(“+”); 如果(拆分!=null&&splitted.length>=4&&ip.equals(拆分[0])){ //基本健康检查 字符串mac=splitted[3]; 如果(mac.matches(“…:”)){ 返回mac; }否则{ 返回null; } } } }捕获(例外e){ e、 printStackTrace(); }最后{ 试一试{ br.close(); }捕获(IOE异常){ e、 printStackTrace(); } } 返回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;
    }
public ArrayList getConnectedDevices(字符串YourPhoneIPAddress){
ArrayList ret=新的ArrayList();
LoopCurrentIP=0;
字符串IPAddress=“”;
字符串[]myIPArray=YourPhoneIPAddress.split(“\\”);
InetAddress currentPingAddr;

对于(int i=0;i这不起作用。我设置了手机的AP并将我的笔记本电脑连接到AP。当我执行,
cat/proc/net/arp
时,我看到一条记录,其中包含我笔记本电脑的MAC和IP,这是正确的。问题是,当我断开笔记本电脑与AP的连接时,arp缓存不会刷新并仍在执行前一个命令ilds一条记录。虽然您的解决方案听起来合法,我也在考虑这些问题,但它确实存在一个边缘情况,即设备可能不会响应ICMP请求,即ping。虽然这种方法在大多数情况下都有效,但不能认为它是完全可靠的。我感谢您的输入。Upvote。@StackOverflowUser我使用了相同的代码,但在ce设备已连接在列表中,直到我关闭热点,因为我得到错误计数是否有其他方法可以在同一文件中获得更新值,或者在文件更新多少时间后(/proc/net/arp)如何刷新同一文件以读取最新连接的设备列表