laptop battery

Is there a way to read a laptop's battery in processing? Or eventually to see if energy saving mode is active?

Answers

  • I think you cannot do it directly, but you could call platform-specific commands with exec(). For example on OSX this would give you some information that you could parse:

    import java.io.InputStreamReader;
    
    String commandToRun = "pmset -g batt";
    
    try {
      Process proc = (Runtime.getRuntime()).exec(commandToRun);
    
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
      String response=null;
      while ((response = stdInput.readLine()) != null) {
        System.out.println(response);
      }
    }
    catch(Exception e) {
      println(e);
    }
    
  • sadly I don't have any apple laptop, is there something that works for windows or linux? Or what should I look for anyway?

Sign In or Register to comment.