0

Using the Java JNA Library using How to get the remaining battery life in a Windows system? as a reference, I have made a program. Unfortunately, the BatteryLife Percent function as described at http://msdn.microsoft.com/en-us/library/aa373232.aspx shows up as being -1, or unknown. Can anyone explain how to make it known? I know this seems like a dumb question, but I can't seem to make it work. The battery icon on the taskbar shows the percent fine, and since it is also most likely using the same function, I think that there may be something wrong with the JNA library.

Community
  • 1
  • 1
Michael Bell
  • 184
  • 1
  • 2
  • 16
  • The SO question you reference ( http://stackoverflow.com/questions/3434719/how-to-get-the-remaining-battery-life-in-a-windows-system ) works fine to obtain battery info. – Java42 Feb 28 '13 at 03:32
  • @ChuckFricano THe output I see is ACLineStatus: Offline Battery Flag: Unknown Battery Life: Unknown Battery Left: 0 seconds Battery Full: 6829 seconds, I would like to see a % instead of Unknown – Michael Bell Feb 28 '13 at 03:35
  • When I run that code I get ACLineStatus: Offline, Battery Flag: Low, less than 33 percent , Battery Life: 29% , Battery Left: 5904 seconds , Battery Full: Unknown. You should add your code and results to the question but the solution in question 3434719 works fine for me. – Java42 Feb 28 '13 at 05:39
  • @ChuckFricano Hmmm.... I guess my laptop battery is having issues. I will test it on another laptop tonight. Thanks for the help. – Michael Bell Feb 28 '13 at 15:10

1 Answers1

0

I had the same Problem and this is how I fixed it. I used the same solution you did and found out, that the order of fields in getFieldOrder() is important, otherwise you get an incorrect output. It has to be the same like in the SYSTEM_POWER_STATUS structure you use. The position of Reserved1 seems to be irrelevant:

@Override
protected List getFieldOrder() {
    ArrayList<String> fields = new ArrayList<String>();
    fields.add("ACLineStatus");
    fields.add("BatteryFlag");
    fields.add("BatteryLifePercent");   
    fields.add("BatteryLifeTime");
    fields.add("BatteryFullLifeTime");
    fields.add("Reserved1");
    return fields;
}
G_T
  • 1