使用Java查找无线网络的SSID
我们正在做一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法在 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 withRuntime
.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.
您必须求助于 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.