In my java application, I want to see what state the wifi is at (E.G. 1 bar, 2 bar or no wifi)
I was wondering how I would be able to find the wifi state of the computer in native Java, or by using a ping/pong application or if I have to parse the state of the wifi directly to the application
Any help would be gratefully appreciated!
P.S. I'm not using android
If you want to cover the Windows platform then you can utilize a little method like this:
public int getWirelessStrength() {
// The returned integer value is in relation to strength percent.
// If 75 is returned then the wireless signal strength is at 75%.
List<String> list = new ArrayList<>();
int signalStrength = 0;
String cmd = "netsh wlan show interfaces";
try {
Process p = Runtime.getRuntime().exec("cmd /c " + cmd);
p.waitFor();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
list.add(line);
}
}
if (p.isAlive()) { p.destroy(); }
// Get the Signal Strength.
for (int i = 0; i < list.size(); i++) {
if (list.get(i).trim().toLowerCase().startsWith("signal")) {
String[] ss = list.get(i).split(":");
if(ss.length == 2) {
signalStrength = Integer.parseInt(ss[1].replace("%","").trim());
}
break;
}
}
}
catch (IOException | InterruptedException ex) {
Logger.getLogger("getWirelessStrength()").log(Level.SEVERE, null, ex);
}
return signalStrength;
}
The above method utilizes the Windows Netsh Command-line Utility to acquire the desired information. The returned integer result is a percent of signal strength so, if 75 is returned then the WiFi signal strength is 75%. If you want to do something similar for Unix or Linux then you can use the iwlist or iwconfig command and parse out the required signal strength in a similar fashion. For the MAC I believe you would need to use the Airport Utility.