I'm currently working on a Smartmirror project with a Raspberry Pi Zero W and I need my java program to check the internet connection so that I know wether I can update my weather data. For this method I use the iwgetid command to get the SSID. The problem is the following: I takethe return of the command line and put it into an if block. Even though the return(which I checked is the correct outprint) is correct the boolean returns false as an output.
public boolean checkwifi() throws IOException, InterruptedException{
ProcessBuilder ps = new ProcessBuilder("iwgetid");
ps.redirectErrorStream(true);
Process pr = ps.start();
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
line=in.readLine();
line.trim();
if(line == "wlan0 ESSID:\"mySSID\"" || line== "wlan0 ESSID:\"mysecondSSID\"" ){
pr.waitFor();
in.close();
return true;
}
else {
System.err.println(line);
pr.waitFor();
in.close();
return false;
}
}
This is the output I get from the console:
wlan0 ESSID:"mySSID"
false
if(line == "wlan0 ESSID:\"mySSID\"" || line== "wlan0 ESSID:\"mysecondSSID\"" )
compares the reference of line
to the string, and they are different.
Use line.equals("wlan0 ESSID:\"mySSID\"") || line.equals("wlan0 ESSID:\"mysecondSSID\"")
to compare the actual value of the Strings.