6

I am developing an application which monitors the presence of the power supply of the laptop. If there is a power cut or restoration it will intimate me over email. It will also application monitoring and controlling over email (Basically to control my laptop from my office over email). I am done with email interfacing but I have no idea on how to monitor the power supply / battery supply from java.

If any can give some pointer on this it will be of great help.

Thanks in advance ....

vcosk
  • 2,894
  • 2
  • 23
  • 23
  • 1
    It would help to know the platform (OS) since this is likely platform specific. – jsight Jul 21 '09 at 17:08
  • just curious why u use the verb intimate there? It sounds odd. did you mean to use another word or is that some sort of email api? – Victor Jul 21 '09 at 17:57
  • I am using windows vista and for the "intimate" was a typo should have been "inform" :$. – vcosk Jul 22 '09 at 03:06

5 Answers5

6

You have probably already solved this problem but for the others - you can do it the way Adam Crume suggests, using an already written script battstat.bat for Windows XP and higher. Here is an example of the resulting function:

private Boolean runsOnBattery() {
    try {
        Process proc = Runtime.getRuntime().exec("cmd.exe /c battstat.bat");

        BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(proc.getInputStream()));

        String s;
        while ((s = stdInput.readLine()) != null) {
            if (s.contains("mains power")) {
                return false;
            } else if (s.contains("Discharging")) {
                return true;
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    return false;
}

Or you can simplify the script to return directly True/False or whatever fits.

rlat
  • 492
  • 4
  • 12
  • This is quite interesting, although it is not giving battery's current status properly this is exactly what I was looking for back then. – vcosk Sep 20 '13 at 12:47
2

On linux, you can use /proc/acpi/battery/

KeyserSoze
  • 2,501
  • 1
  • 16
  • 17
1

A quick google search turns up a java acpi library on sourceforge. Hasn't been updated since 2004 though.

Charlie
  • 644
  • 3
  • 7
1

Here's code that works on Windows by using the SYSTEM_POWER_STATUS structure.

Note that you need to add jna to your (Maven) dependencies for this to work.

import java.util.ArrayList;
import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public interface Kernel32 extends StdCallLibrary
{
    public Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32",
            Kernel32.class);

    public class SYSTEM_POWER_STATUS extends Structure
    {
        public byte ACLineStatus;

        @Override
        protected List<String> getFieldOrder()
        {
            ArrayList<String> fields = new ArrayList<String>();
            fields.add("ACLineStatus");

            return fields;
        }

        public boolean isPlugged()
        {
            return ACLineStatus == 1;
        }
    }

    public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result);
}

In your code call it like this:

Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS();
Kernel32.INSTANCE.GetSystemPowerStatus(batteryStatus);

System.out.println(batteryStatus.isPlugged());

Result:

true if charger is plugged in false otherwise

This has been derived off BalsusC's answer.

Community
  • 1
  • 1
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
0

A quick and dirty way to handle this is call a native program (via Runtime.exec(...)) and parse the output. On Windows, the native program might be VBScript that uses WMI.

Adam Crume
  • 15,614
  • 8
  • 46
  • 50