当前位置: 首页 > 面试题库 >

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

伯英武
2023-03-14
问题内容

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

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


问题答案:

您可以在接入点上计数已连接的设备,并且可以在android的以下链接上获取硬件mac地址:http :
//www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-
远程主机/

上面链接的代码:

/**
 * 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;
    }


 类似资料:
  • 您好,我正在开发一个应用程序,通过无线网络连接到爱普生打印机和打印收据。我的android手机和打印机(TM-T88V-DT)都连接到同一个无线网络,但我的应用程序无法与打印机连接。我已经按照epson技术文档中的说明设置了打印机和无线连接,我正在使用随epson epos sdk for android提供的示例进行测试,但示例应用程序未与打印机连接。我不知道我的设置出了什么问题,请帮忙。

  • 当建立蓝牙连接(BLE)时,外围设备是否有办法获得中央设备的名称?我不确定Bleno是否有必要的工具来得到这个。

  • 我已使用 adb connect 通过 tcpip 连接了Android设备

  • 我想检索连接到智能手机WiFi热点的所有设备的IP地址。在Android 10及以下版本中,我能够通过执行以下代码获得此列表: 然而,在Android 11中,IP命令似乎不再有效(https://developer.android.com/training/articles/user-data-ids#mac-11岁以上) 在Android11中,有没有其他方法可以获取已连接客户端的IP地址?

  • 我正在尝试制作一个应用程序,它使用Android的新蓝牙低能耗API。为此,我从API Level18附带的BLE示例开始。 当我读到Android不能充当外围设备时,我将Android手机置于中央模式,扫描周围的BLE设备。为此,我用一个模拟心脏传感器的北欧平台做了一些测试。一切都以完美的方式运作! 多谢了。 编辑:经过一些艰苦的测试,我在AOSP页面上提出了一个问题。这里可以查

  • 我正在建立一个Android媒体播放器应用程序,我打算使用它在电视上播放媒体(视频,图片等),同时通过HDMI电缆连接。 我想让媒体播放器应用程序暂停时,电视的电源状态关闭,并希望它播放时,电视打开。 当我的Android设备通过HDMI连接到电视时,我如何在我的Android应用程序中检测电视的电源状态? 电视和Android设备都支持HDMI-CEC。该设备是一个ODROID C2。我已经在K