-1

I am making a program in Java where it needs to print out a list of available WiFi's names, the SSID, into the console all separated by semicolons.

So like: wifi1;thekingswifi;otherwifi

Wiz Gamin
  • 29
  • 3
  • 1
    @LawrenceAiello You know instead of being a jerk you could actually be of some use. – Wiz Gamin Feb 17 '15 at 01:29
  • 1
    @WizGamin, I'm with you - that wasn't appropriate. However, your question isn't very clear. Can you please update it with a sample of the code you've written, and an explanation of which part of it isn't working for you? – Simon MᶜKenzie Feb 17 '15 at 01:31
  • @SimonMᶜKenzie that is where I am stuck...essentially it needs to search the for wifi's near and then say the names of those wifi's – Wiz Gamin Feb 17 '15 at 01:33
  • So you essentially want us to write code for you. Now you see why I made that comment. – Lawrence Aiello Feb 17 '15 at 01:36
  • I'm afraid that's too broad for stack overflow. You will need to show that you've made an effort, then you can ask questions about the _specific_ coding problems you've encountered. As @LawrenceAiello said, nobody is going to write the code for you, but they _will_ help you fix the code you've already written! – Simon MᶜKenzie Feb 17 '15 at 01:38
  • @SimonMᶜKenzie no, I need help. I can use netsh wlan to show a list of all wifi's near but I don't know how to go through the data it outputs to find and print the SSID's, the wifi's names. – Wiz Gamin Feb 17 '15 at 01:39
  • So put that in your question. It sounds like you're trying to capture stdout from `netsh wlan` - perhaps [this post](http://stackoverflow.com/a/882785/622391) will be of use. – Simon MᶜKenzie Feb 17 '15 at 01:43
  • @SimonMᶜKenzie except that it can't use netsh Alan because I can't have this program bound to any other OS like I need a universal way to get the wifi names. – Wiz Gamin Feb 17 '15 at 18:03

1 Answers1

0

Querying the wifi data on a system is sadly OS specific so there isn't a common library to do this.

Nevertheless, here's a class that I wrote that uses the windows command line utility netsh to retrieve the SSID's and return them in a JSON string. There are similar utilities on the other operating systems that you can use to achieve the same effect. Hopefully this class will get you started.

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WindowsNetworkHelper
{

    public String getSsidsJson(String regex) throws Exception
    {
        Runtime rt = Runtime.getRuntime();
        try
        {
            Process pr = rt.exec("netsh wlan show networks mode=bssid");

            String processOutput = processOutputToString(pr);
            ArrayList<String> ssidList = extractSsids(processOutput,regex);
            String result = "[";
            boolean addComma = false;
            for(String ssid : ssidList)
            {
                if(addComma)
                {
                    result += ",";
                }
                result += "{\"ssid\":\"" + ssid + "\"}";
                addComma = true;
            }
            result += "]";
            return result;
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "[]";
    }

    private ArrayList<String> extractSsids(String result,String regex)
    {
        ArrayList<String> ssids = new ArrayList<String>();
        String[] lines = result.split("\r\n\r\n");
        Pattern ssidPattern = Pattern.compile("^SSID [0-9]+ : (.+)");

        for(String line : lines)
        {
            Matcher ssidMatcher = ssidPattern.matcher(line);
            if(ssidMatcher.find())
            {
                String capture = ssidMatcher.group(1);

                if(ssidMatchesFilter(capture,regex))
                {
                    ssids.add(capture);
                }
            }
        }

        return ssids;
    }

    private boolean ssidMatchesFilter(String ssid,String regex)
    {
        if(regex == null)
        {
            return true;
        }
        Pattern filterPattern = Pattern.compile(regex);
        Matcher filterMatcher = filterPattern.matcher(ssid);
        return filterMatcher.matches();
    }

    private String processOutputToString(Process pr)
    {
        InputStream is = pr.getInputStream();
        java.util.Scanner s = new java.util.Scanner(is);
        s.useDelimiter("\\A");
        String result = s.hasNext() ? s.next() : "";
        s.close();
        return result;
    }

}
spectacularbob
  • 3,080
  • 2
  • 20
  • 41