Fair enough, and it works although – at least on my machine – it works around the fact that some of the code is clearly broken – the grep and sed fail to return values, while the airport command prints like 90+ lines of unnecessary usage text, etc. This doesn’t happen if you run airport -I
from the command line. The wanted value is down there on line 100, but any system update with the tiniest change to airport would make that not true.
Here is an alternative – because pipes work grep/sed actually filter correctly, it will always return only the value from the last line containing CtlRSSI, and it will always work as long as some line contains CtlRSSI.
// https://discourse.processing.org/t/get-wifi-rssi-on-mac/13942/4
// https://forum.processing.org/one/topic/running-bash-script-from-processing#25080000001056314.html
import java.io.InputStreamReader;
int RSSI = 0;
void setup() {
String[] params = { sketchPath() + "/WiFiCheck.sh"};
Process p = exec(params);
try {
int result = p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String returnedValues;
while ( (returnedValues = stdInput.readLine ()) != null) {
RSSI = int(returnedValues);
}
}
catch (InterruptedException e) {
println("failed");
}
catch (IOException e) {
println("failed");
}
println(RSSI);
}
/**
WiFiCheck.sh
#!/usr/bin/env bash
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep CtlRSSI | sed -e 's/^.*://g' | xargs -I SIGNAL
*/
If you want a full airport report, don’t filter the lines, and instead dump them into a HashMap or a StringDict. Then you can retrieve whatever keys you want later.
// https://discourse.processing.org/t/get-wifi-rssi-on-mac/13942/4
// https://forum.processing.org/one/topic/running-bash-script-from-processing#25080000001056314.html
import java.io.InputStreamReader;
int RSSI = 0;
HashMap<String,String> report = new HashMap<String,String>();
void setup() {
String[] params = { sketchPath() + "/WiFiCheck.sh"};
Process p = exec(params);
try {
int result = p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String returnedValues;
while ( (returnedValues = stdInput.readLine ()) != null) {
String[] terms = returnedValues.split(":");
report.put(terms[0],terms[1]);
}
}
catch (InterruptedException e) {
println("failed");
}
catch (IOException e) {
println("failed");
}
println(report);
println(report.get("agrCtlRSSI"));
}
/**
WiFiCheck.sh
#!/usr/bin/env bash
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | xargs -I SIGNAL
*/
Now you can look up the value for any key.
{link auth= wpa2-psk, agrCtlRSSI= -60, channel= 4, op mode= station lastTxRate, maxRate= 54, 802.11 auth= open, lastAssocStatus= 0, agrCtlNoise= -99, agrExtRSSI= 0, BSSID= 0, state= running, MCS= -1, SSID= NETGEAR1, agrExtNoise= 0}
-60