当前位置: 首页 > 知识库问答 >
问题:

使用Java获取WiFi网络上的所有设备

司马奇希
2023-03-14

我试图发现我的设备连接到的WiFi网络上的所有设备。这是普通的Java,不是Android。我需要搜索每个设备,看看它是否有一个特定的端口打开。我将通过Socket连接到我发现的第一个符合此条件的设备,所以我需要它的IP地址。我基本上是在尝试编写以下代码:

for (WiFiDevice device : WiFi.getConnectedDevices()) {
    if (device.hasPortOpen(1234)) {
        this.socket = new Socket(device.getIPAddress(), 1234);
    }
}

共有2个答案

吕新
2023-03-14

这是我的答案,它在我的Mac上运行良好,但有点粗糙。

Socket socket = new Socket();

try {
    Process process = Runtime.getRuntime().exec("arp -i en0 -a -n");
    process.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

    while (reader.ready()) {
        String ip = reader.readLine();
        ip = ip.substring(3, ip.indexOf(')'));

        try {
            socket.connect(new InetSocketAddress(ip, 1234), 1000);
            System.out.println("Found socket!");
        } catch (ConnectException | SocketTimeoutException ignored) {

        }
    }

    if (socket == null) {
        System.err.println("Could not find socket.");
    }
} catch (Exception e) {
    e.printStackTrace();
}
沃阳飙
2023-03-14

这个黑客解决方案怎么样:

import java.net.InetAddress;
import java.net.Socket;

public class Main {
    public static void main(String[] args) {

        int timeout=500;
        int port = 1234;

        try {
            String currentIP = InetAddress.getLocalHost().toString();
            String subnet = getSubnet(currentIP);
            System.out.println("subnet: " + subnet);

            for (int i=1;i<254;i++){

                String host = subnet + i;
                System.out.println("Checking :" + host);

                if (InetAddress.getByName(host).isReachable(timeout)){
                    System.out.println(host + " is reachable");
                    try {
                        Socket connected = new Socket(subnet, port);
                    }
                    catch (Exception s) {
                        System.out.println(s);
                    }
                }
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
    }

    public static String getSubnet(String currentIP) {
        int firstSeparator = currentIP.lastIndexOf("/");
        int lastSeparator = currentIP.lastIndexOf(".");
        return currentIP.substring(firstSeparator+1, lastSeparator+1);
    }
}
 类似资料:
  • 我正在尝试在这里制作一个应用程序,它将检测WiFi网络中连接的所有设备。我已经做了足够多的谷歌,并提出了一个应用程序,可以检测在应用程序的WiFi网络中连接的设备的IP地址。 现在我想要更多的东西。 < li >我能否找到设备名称,即电话名称或型号或系统名称,以及我们可以用来检测特定设备的任何信息? < li >我们能否找到设备距离,例如该设备距离我们使用应用的手机有多远? < li >这是主要任

  • 我正在构建一个需要通过没有任何互联网访问的WiFi网络进行通信的android应用程序。问题是,即使连接了WiFi,当wifi网络上没有连接互联网时,android也会选择使用蜂窝/移动数据。 我读了很多关于这个问题的帖子,其中很多都涉及到为设备找根,但这在生产应用程序中是不可能的(为设备找根不是一个选项)。其他解决方案(如下面的代码)建议使用< code > bindProcessToNetwo

  • 问题内容: 我正在开发一个需要扫描WiFi网络并显示所有已连接设备列表的应用程序。 允许用户点击设备,该应用应显示该特定设备的所有硬件信息。这里所说的硬件是指-RAM,存储介质,存储容量,设备名称,设备IP地址等。 现在,该设备可以是xbox,带Linux / Windows的笔记本电脑,iPhone等手机或任何基于Andorid的智能手机甚至打印机。 如何扫描WiFi网络并查询/检测连接到它的所

  • 在Android系统中, > 如何获取在同一WiFi网络中连接的设备列表? 如何从与同一WiFi网络连接的智能手机获取最近的设备? 频率检查是否能够找到最近的设备?

  • 问题内容: 看来android的java.net.NetworkInterface实现没有 byte [] getHardwareAddress()方法 http://developer.android.com/reference/java/net/NetworkInterface.html 我发现有几个论坛的人都试图这样做,但没有明确的答案,我需要获得一个跨设备的UUID,所以我不能依赖电话号码

  • 我正在开发一个应用程序,我需要扫描WiFi网络,并显示所有连接设备的列表。 允许用户点击一个设备,应用程序应该显示该特定设备的所有硬件信息。这里我说的硬件是指内存、存储介质、存储容量、设备名称、设备IP地址等。 现在,这个设备可以是任何东西,比如xbox,带有Linux/Windows的笔记本电脑,iPhone之类的手机,或者任何基于Android的智能手机,甚至是打印机。 我如何扫描WiFi网络