Get Network Signal Strength From Raspberry Pi Using Java

Viewed 208

I need to get the WiFi signal strength and save it in a file.

I have tried this code but it is unable to create or execute the iwconfig command:

try {
    String cmd = "sudo iwconfig wlan0 | grep -i --color signal>/home/pi/signal.txt";
    p = Runtime.getRuntime().exec(cmd);  
    p.waitFor();
}
catch (IOException | InterruptedException e) {             
    e.printStackTrace();
}

No Exception is Thrown. The result should be like the result of the command should be store in signal.txt If the file doesn't exist it'll create one and write. If exist it will overwrite in the file.

If I ran the sudo iwconfig wlan0 | grep -i --color signal>/home/pi/signal.txt in the terminal it is working correctly. But if I execute it from Java it is not working.

The same process is used to delete a file in a different module. But this deletion working fine. But the File is not creating in the system

      String cmd = "rm signal.txt"; 
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
0 Answers
Related