×
Get in touch

Get in touch with Binary

Binary Studio website uses cookies to enhance your browsing experience. By continuing to use our site, you agree to our Privacy Policy and use of cookies.

Learn more more arrow
Agree
kirill.buga@binary-studio.com'
Kirill Buga JS developer 13.05.2015

Battery status API


           With every passing day, quantity of tablets and mobile phones gets bigger and bigger. Thus during websites' and services' development one should pay attention to specifics of this gadgets. It is well known fact that they are the most vulnerable place is battery life. technologies move forward at incredible pace, screens get more pixels, processors from their side - new cores but accumulator lasts for 1 day of active usage tops. As a rule developers generally ignore this factors. But why? The reason is simple - often they don't have API for real time check of battery state. Battery status API provides this kind of information. It allows to control current charge and raises events when this state changes. This standard was suggested in 2012, and wasn't changed much since that time. For now it is W3C standard recommendation. What does it actually mean? It says that guys who developed this specification wait for feedback from developers community. Owing to Battery Status API, developer would have the possibly temporary pause the execution of complex source-consuming operations, lessen requests to the server, switch off some modules and so on.

            Battery status API could also be used for saving user's data right before device switches off due to empty battery. Many will be grateful for such a functionality. It happens quite often when you lose your changes because your gadget goes off without giving any notices.
Battery Status API provides four read-only properties, in window.navigator.battery object:

  1. readonly attribute boolean charging – Represents if the systems battery is charging. If device doesn’t have battery or the value can’t be determined the value is set to true.
  2. readonly attribute boolean chargingTime – Represents the time(in seconds) remaining in seconds until the system’s battery is fully charged. If the battery is fully charged or device doesn’t have a battery, the value is set to 0.
  3. readonly attribute boolean dischargingTime – Represents the time(in seconds) remaining in seconds until the system’s battery is completely discharged and the system is about to be suspended. If the value can’t be determined or device is currently charging then the value is Infinity. If device doesn’t have battery the value is Infinity as well.
  4. readonly attribute boolean level – Represents the current battery level scaled from 0 to 1.0. If value can’t be determined, the battery is fully charged or device doesn’t have battery then the value is 1.

            It is worth remembering that in chargingTime is stored time until complete recharge of the battery and not the discharge. In addition, there are four events that are raised by window.navigator.battery on every change of previously described properties:

  1. chargingchange – is fired when the device’s charger is activated/deactivated.
  2. chargingtimechange – is fired when the remaining charging time changes.
  3. dischargingtimechange – is fired when the remaining time until the battery is fully discharged changes.
  4. levelchange – is fired when the battery level has changed.

let's take a look at the example:

             All this features give developer full control over application execution with possible adjustments based upon gadget's specifics.
And what about support everything looks not so good. Despite the fact that standard was applied by W3C community in 2012, currently only Firefox supports it without vendor prefix. It’s strange cause Battery status API of such type would allow solving problems with energy consumption in web-applications. There is also no analogue for Battery API in any JS library, it is caused by browser's restriction policy on hardware access. That’s why you should always check whether battery API is available at all before trying to make use of it

             And at the end I would like to provide example of Battery API usage in some simplest application, in which clients from time to time requests some information from the server. Timeout between requests depends on battery charge. An example was partially taken from www.w3.org.

1. http://www.w3.org/TR/battery-status/
2. https://developer.mozilla.org/en-US/docs/WebAPI/Battery_Status
3. http://davidwalsh.name/battery-api