12

There is some easily available information on finding the status of a battery, or whether it's charging or not. (GetSystemPowerStatus API or System.Windows.Forms.SystemInformation.PowerStatus).

I want to be able to stop a battery from charging based on some criteria, e.g. battery power > 20%.

Is there an API to do this?

cigien
  • 57,834
  • 11
  • 73
  • 112
Marcel
  • 7,909
  • 5
  • 22
  • 25
  • 2
    Why are you trying to do this? – DevinB Jul 07 '09 at 12:22
  • Most laptops don't have any way to do this in their firmware at all. – pjc50 Jul 08 '09 at 12:59
  • 1
    Plug into a power strip and then toggle the on/off button on the strip. =) – Garrett Aug 25 '09 at 21:14
  • @DevinB: [This vaguely-related question of mine](http://android.stackexchange.com/questions/120480/i-want-my-phones-lithium-ion-battery-to-remain-useful-for-as-long-as-possible) explains why it's not ideal to always keep your laptop charged at 100%. – unforgettableidSupportsMonica Aug 28 '15 at 02:44
  • @DevinB a good reason to do this is as a security issue. if you feel you are compromised down to the firmware level, you can keep you devices battery low so it can completely power down with a dead battery. its a last step workaround if you cant find the exploit. – michael g Jun 01 '21 at 03:14

5 Answers5

5

I think it's impossible, because you have need some API for battery or battery charger.

And this API can provide to you manufacturer of notebook and battery or battery charger support this.

MicTech
  • 42,457
  • 14
  • 62
  • 79
  • I wonder when (or if) we will ever see a hardware manufacturer which provides every possible aspect of his designs to programmatically achieve anything! Being stopped by hardware is so uncool. – Kensai Jan 17 '10 at 09:23
  • 2
    @Kensai - imagine the havok that malware could wreak! Destroy your battery as well as stealing your personal info! – Coxy Jun 02 '10 at 02:44
  • I use Sony Vaio. It has a utility called Battery Care Function which allows you to control the charging level. – isara Nov 21 '09 at 21:19
2

I honestly don't know, but I'd have a look at the APM or ACPI APIs.

Other than that, the only option I can think of right now is a USB controlled robotic arm that ejects the battery when you need to stop charging, but that's probably not what you are looking for, and borders on the complicator's glove in terms of level of over-engineering. :)

Jack Leow
  • 21,945
  • 4
  • 50
  • 55
2

I would just get a UPS and programatically tell it to cut all power... most should have an interface for doing this. Otherwise, as someone already said - a computer-controlled power strip would do it ^^

I've actually played with this idea when I was testing/writing about way too many new laptop models a while ago and the battery testing was annoying to set up, monitor and analyze.

I wrote an app that would do exactly everything (setup, listening, measuring, reporting) except unplugging the power and then replugging it and starting the computer again...

Oskar Duveborn
  • 2,189
  • 16
  • 20
  • Great idea! Should be much easier nowadays with smart outlet/plugs, though it's still hard to find a low cost one with an available API. Please comment if anyone has a product suggestion. – user193130 Mar 31 '22 at 14:46
2

Actually I use such a charge limiter. There is the control software - a Python script that monitors the battery level (psutil module) and controls external hardware - i.e. a switch that can be software controlled. I have Energenie and TP-Link homeplugs plus my own hardware contraption.

As it is for home use the software it's not polished at all, but with minimal effort can be adapted to any OS or hardware.

Let know if interested. The software lives here: CCC

Adrian Rosoga
  • 452
  • 4
  • 9
  • Great idea and thanks for sharing! Just noticed this answer after [the one by @OskarDuveborn][https://stackoverflow.com/a/1776865/2891365] above. – user193130 Mar 31 '22 at 14:52
1

One of the options is to get hold of the device(I) for battery (Microsoft ACPI-Compliant Control Method Battery). Listen for PowerNotification events forever. On each notification check the PowerStatus of the battery. There are APIs for all of the above purposes in .Net and win32

Keep the device(I) disabled as long as the powerstatus is >threshold. Enable it as soon as goes below that or when you are not on AC power (i.e. before removing AC power, your continuously monitoring software should enable that battery device - or you manually enable it).

hmm,...this is a very buggy solution, but it can achieve what you want, although you have to be very careful.

TJ-
  • 14,085
  • 12
  • 59
  • 90