如何获取连接到wifi热点的客户端设备详细信息?

时间:2014-02-03 08:48:21

标签: android android-wifi

我在我的Android应用程序中以编程方式连接不同的设备和wifi热点AP,如何以编程方式检测连接和断开连接的客户端以及wifi热点AP? Android API中是否有任何回调事件可以提供有关各个设备的连接或断开连接事件的信息?提前谢谢。

5 个答案:

答案 0 :(得分:10)

此方法适用于我,但这仅检测版本4.0及更高版本;它无法找到与热点连接的版本为2.2或2.3的设备。

public void getClientList() {
    int macCount = 0;
    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 ) {
                // 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) {

    }               
}

答案 1 :(得分:10)

不幸的是,没有公共API提供有关此信息的信息...... 但您可以读取/ proc / net / arp文件并查看连接到接入点的客户端。

/ proc / net / arp文件有6个字段: IP地址硬件类型标志硬件地址面具设备

问题是当客户端断开连接时,因为它不会从文件中消失。解决方案可能是对每个客户端进行 ping 并等待响应,但对我来说这不是一个好的解决方案,因为有些客户端不响应 ping 。如果你喜欢这个解决方案,请在GitHub上检查这个项目 - &gt; https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class/tree/master/src/com/whitebyte

我所做的是:读取/ proc / net / arp并检查 FLAGS 字段,当值为0x2时,工作站已连接且0x0已断开连接,但要刷新此字段,我需要我不时清除ARP缓存,我用这个命令做了: ip neigh flush all

我希望我帮助你

答案 2 :(得分:1)

@SuppressWarnings("ConstantConditions")
public static String getClientMacByIP(String ip)
{
    String res = "";
    if (ip == null)
        return res;

    String flushCmd = "sh ip -s -s neigh flush all";
    Runtime runtime = Runtime.getRuntime();
    try
    {
        runtime.exec(flushCmd,null,new File("/proc/net"));
    }

    BufferedReader br;
    try
    {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null)
        {
            String[] sp = line.split(" +");
            if (sp.length >= 4 && ip.equals(sp[0]))
            {Assistance.Log(sp[0]+sp[2]+sp[3],ALERT_STATES.ALERT_STATE_LOG);
                String mac = sp[3];
                if (mac.matches("..:..:..:..:..:..") && sp[2].equals("0x2"))
                {
                    res = mac;
                    break;
                }
            }
        }

        br.close();
    }
    catch (Exception e)
    {}

    return res;
}

// --------------------------------------------- -----------

@SuppressWarnings("ConstantConditions")
public static String getClientIPByMac(String mac)
{
    String res = "";
    if (mac == null)
        return res;

    String flushCmd = "sh ip -s -s neigh flush all";
    Runtime runtime = Runtime.getRuntime();
    try
    {
        runtime.exec(flushCmd,null,new File("/proc/net"));
    }

    BufferedReader br;
    try
    {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null)
        {
            String[] sp = line.split(" +");
            if (sp.length >= 4 && mac.equals(sp[3]))
            {
                String ip = sp[0];
                if (ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}") && sp[2].equals("0x2"))
                {
                    res = ip;
                    break;
                }
            }
        }

        br.close();
    }
    catch (Exception e)
    {}

    return res;
}

答案 3 :(得分:0)

你可以使用BroadcastReciever&#34; android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED&#34;检测客户端连接。 在你的AndroidManifest中:

scope

并在您的活动中

<receiver
            android:name=".WiFiConnectionReciever"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED" />
            </intent-filter>
        </receiver>

答案 4 :(得分:0)

Android 10限制了对/ proc / net目录的访问权限,因此上述某些解决方案不再可行,但是'ip'命令仍然可用

    this.cartItems.forEach(item => {
        const indexEmptyItem = !item.restaurant || !item.spectacle || !item.placesListContent || item.placesListContent < 1;
        const giftItemIndex = !item.type;
        this.disableBookBtn = (indexEmptyItem || giftItemIndex) ? true : false;
        // do other operations based on each item
      })
相关问题