Finding SSID of a wireless network with Java

后端 未结 3 1198
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 20:50

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 specif

相关标签:
3条回答
  • 2020-11-27 20:52

    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.

    0 讨论(0)
  • 2020-11-27 20:58
     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)  );
        }
    
    0 讨论(0)
  • 2020-11-27 21:12

    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.

    0 讨论(0)
提交回复
热议问题