Finding SSID of a wireless network with Java
Asked Answered
T

3

13

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!

Turbulent answered 21/3, 2011 at 13:17 Comment(0)
B
22

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.

Birdbath answered 21/3, 2011 at 16:50 Comment(10)
Well that sucks... But thanks for the answer, might as well stop searcdhing for direct java implementationens then, thanks again.Turbulent
@David So Java is not designed to be able to write a full OS unless through JNI ?Jeremyjerez
Java is not able to "write" an OS as is an interpreted language requiring a Virtual Machine running on top of some kind of OS. If you ask about accessing all OS resources, Java is not designed to such kind of things, as one of its goals is to be portable across OS and no direct access to HW. If you want some low-level features like working at Wifi 802.X level, use JNI or choose another language like C/C++. Other main interpreted languages such as .Net platform could not also access Wifi-specific details natively as is a no-sense.Oenomel
@DavidOlivánUbieto For the sake of correctness, actually there were a couple of OS's written (primarily) in Java. Can Java be used to develop an OS?Inconstant
@Inconstant Well, this could lead to a different discussion... Real Java applications require the stack Java App + JVM + OS on top of HW (plus BIOS). JNode and JavaOS are some kind of experiments, not real applications and they require always some native/assembler/C kernel to access HW as no access to HW is provided in Java Lang Spec. Java API does not provide access to hardware details in almost any case (filesystem and network are some exceptions) so the typical question about accessing webcam/USB/bluetooth/wifi in Java goes to same general response: not possible.Oenomel
@DavidOlivánUbieto I just draw an example what was done/attempted before. Plus note the word primarily in my previous comment. Of course, for the OS development in general, and low level stuff in particular - it's Assembly/C/C++ who are the main heroes...Inconstant
@Inconstant Yes, I see primarily but want to add more ideas. Yes, OS and low-level are ruled by Assembly and C (C++ not too much, did you know any OS or low-level API developed in C++? OO APIs are not portable between languages).Oenomel
@DavidOlivánUbieto That was an enumeration of languages capable of dealing with low-level stuff, from most powerful and further on. I never wrote about writing an entire OS purely in C++, come on. Some parts can/are written using C++. Although, as you may know, for example, Linus Torvalds doesn't like (to say the least) C++Inconstant
Yes! Don't want to start a flame o troll war!Oenomel
@DavidOlivánUbieto Smart decision :-), I never intended eitherInconstant
D
6
 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)  );
    }
Diversity answered 18/5, 2015 at 7:8 Comment(0)
P
4

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.

Pioneer answered 21/3, 2011 at 14:26 Comment(2)
Mhmmm the point of doing it in Java was to have a way to be crossplatform. But is there no way of utilizing the java.net package?Turbulent
*nix != iwconfig. Linux = iwconfig. OS X = /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport. *BSD = ifconfig. And cross-platform would require different implementations of the Java classes on different platforms.Apure

© 2022 - 2024 — McMap. All rights reserved.