使用Java查找无线网络的SSID

发布于 2024-10-25 21:27:14 字数 524 浏览 5 评论 0原文

我们正在做一个用 Java 编码的项目(为 JRE 1.6 编译),需要一些帮助 一个有点但明显复杂的功能: 我们希望在连接特定无线网络时执行特定操作,例如,当连接的 SSID==“myNetworkAtHome”或类似网络时。

在浏览了这个网站、谷歌和 Java 文档之后,我们已经更接近了一些。 查看这里的代码后: http://download.oracle.com/javase/tutorial/networking/ nifs/retriving.html

看来我们已经接近了,但它遇到了死胡同,所有接口似乎都通过“net13”连接到“net0”(在我的笔记本电脑上)。 而且我们根本无法从任何接口获取 SSID。我确实意识到示例中的代码仅给出了接口名称而不是连接的网络,但它似乎没有提供获取连接的网络信息的方法。

对此的任何帮助都会非常有帮助!

We're doing a project coded in Java (compiled for JRE 1.6) and need some help with
a little but apparently complicated feature:
We want to do a certain action when a specific wireless network is connected e.g. when the connected SSID=="myNetworkAtHome" or similar.

After looking through this site, google and the Java documentation we have come a little closer.
After looking at the code here:
http://download.oracle.com/javase/tutorial/networking/nifs/retrieving.html

It seems we were getting close but it hits a deadend, all the interfaces seems to be connected to "net0" through "net13" (on my laptop that is.)
And we're unable to get the SSID out of any interface at all. I do realise the code in the example is only giving the interface names and not connected networks, but it doesn't seem to offer a way of fetching the connected network information.

Any help on this would be extremely helpfull!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么这么可爱啊 2024-11-01 21:27:14

您无法在 Java 中访问网络的这种低级详细信息。您可以使用 NetworkInterface 类获取网络接口的一些详细信息,但如果您查看提供的方法,没有人与 Wifi 网络相关,也没有提供任何获取 SSID 的方法。如下所述,您应该通过使用 JNI 调用本机库或使用 Runtime 调用操作系统工具来使用一些本机功能。

Java 并不是为做这类事情而设计的,很难以独立于平台的方式实现,而且原则上任何硬件级细节都不能在 Java 中管理。

这同样适用于其他网络,如 3G、GPRS...应用程序不应该知道连接类型及其详细信息。 Java只能管理传输(TCP)级别的事物,不能管理网络(IP)也不能管理链路(3G、Wifi、以太网...),因此只能管理套接字。

You can't access this low-level details of the network in Java. You can get some details of the network interface with the NetworkInterface class but if you see at the provided methods, no one is related to Wifi networks nor any way to get the SSID is provided. As pointed below, you should use some native functionality through calling a native library with JNI or by calling a OS tool with Runtime.

Java is not designed to do that kind of things, is hard to implement in a platform-independent way and any hardware-level detail can not be managed in Java by principle.

Same applies to other networks like 3G, GPRS... the application should not be aware of the connection type nor its details. Java can only manage things at the Transport (TCP) level, not the network (IP) not Link (3G, Wifi, Ethernet...), so you can only manage sockets.

太傻旳人生 2024-11-01 21:27:14
 ArrayList<String>ssids=new ArrayList<String>();
    ArrayList<String>signals=new ArrayList<String>();
    ProcessBuilder builder = new ProcessBuilder(
            "cmd.exe", "/c", "netsh wlan show all");
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (r.read()!=-1) {
        line = r.readLine();
        if (line.contains("SSID")||line.contains("Signal")){
            if(!line.contains("BSSID"))
                if(line.contains("SSID")&&!line.contains("name")&&!line.contains("SSIDs"))
                {
                    line=line.substring(8);
                    ssids.add(line);

                }
                if(line.contains("Signal"))
                {
                    line=line.substring(30);
                    signals.add(line);

                }

                if(signals.size()==7)
                {
                    break;
                }

        }

    }
    for (int i=0;i<ssids.size();i++)
    {
        System.out.println("SSID name == "+ssids.get(i)+"   and its signal == "+signals.get(i)  );
    }
 ArrayList<String>ssids=new ArrayList<String>();
    ArrayList<String>signals=new ArrayList<String>();
    ProcessBuilder builder = new ProcessBuilder(
            "cmd.exe", "/c", "netsh wlan show all");
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (r.read()!=-1) {
        line = r.readLine();
        if (line.contains("SSID")||line.contains("Signal")){
            if(!line.contains("BSSID"))
                if(line.contains("SSID")&&!line.contains("name")&&!line.contains("SSIDs"))
                {
                    line=line.substring(8);
                    ssids.add(line);

                }
                if(line.contains("Signal"))
                {
                    line=line.substring(30);
                    signals.add(line);

                }

                if(signals.size()==7)
                {
                    break;
                }

        }

    }
    for (int i=0;i<ssids.size();i++)
    {
        System.out.println("SSID name == "+ssids.get(i)+"   and its signal == "+signals.get(i)  );
    }
离线来电— 2024-11-01 21:27:14

您必须求助于 JNI 解决方案。 http://sourceforge.net/projects/jwlanscan 上有一些可用的内容,但仅适用于 Windows 系统。或者你可以用丑陋的方式来做,使用 Runtime.getRuntime().exec(...) 并使用适用于你的操作系统的命令行工具 (*nix = iwconfig) 并诉诸解析。

You'll have to resort to a JNI solution. There's something available at http://sourceforge.net/projects/jwlanscan, but that only works for Windows systems. Or you could do it the ugly way and use Runtime.getRuntime().exec(...) and use the command line tools available for your OS (*nix = iwconfig) and resort to parsing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文