android - 如何监听连接到android热点的设备

标签 android android-wifi

这是我用来在 android 中创建热点的代码,现在我想检测连接到它的设备的 IP

// toggle wifi hotspot on or off
public static boolean configApState(Context context) {

    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiConfiguration wificonfiguration = null;
    try {  

        // if WiFi is on, turn it off
        if(isApOn(context)) {               
            wifimanager.setWifiEnabled(false);
        }               
        Method method = wifimanager.getClass().getMethod("setWifiApEnabled",  WifiConfiguration.class, boolean.class);                   
        method.invoke(wifimanager, wificonfiguration, !isApOn(context));
        return true;
    } 
    catch (Exception e) {

        e.printStackTrace();
    }       
    return false;
}

最佳答案

您可以使用以下代码获取连接的设备:

public void getListOfConnectedDevice() {
Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {
        BufferedReader br = null;
        boolean isFirstLine = true;

        try {
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;

            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }

                String[] splitted = line.split(" +");

                if (splitted != null && splitted.length >= 4) {

                    String ipAddress = splitted[0];
                    String macAddress = splitted[3];

                    boolean isReachable = InetAddress.getByName(
                            splitted[0]).isReachable(500);  // this is network call so we cant do that on UI thread, so i take background thread.
                    if (isReachable) {
                        Log.d("Device Information", ipAddress + " : "
                                + macAddress);
                    }

                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
});
thread.start();
}

来源:link

关于android - 如何监听连接到android热点的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29249441/

相关文章:

android - fragment 中的 RecyclerView 不工作(无法向其添加数据)

java - 使用 WiFi Direct 连接安卓设备

android - 是否可以在不配对的情况下使用蓝牙或 wifi 从一个 android 手机向另一个 android 手机发送消息?

android - 如何通过Wi-Fi访问SD卡上的文件?

android - wifiManager.startScan() 返回 false

java - 如何在 JodConverter 中读取 doc 或 pdf 中的文本

android - Cordova Android 6.2.1 + Crosswalk 23 => SSL 错误:net::ERR_INSECURE_RESPONSE

android - 禁用时 ActionBar 中的半透明图标 : only on my phone?

android - 在 android 中使用 wi-fi 进行点对点通信

javascript - Wikitude SDK for Android 中使用 Javascript 的原因是什么?