Scroll to top

The number of people browsing the web using mobile devices grows every day. It's therefore important to optimize websites and web applications to accommodate mobile visitors. The W3C (World Wide Web Consortium) is well aware of this trend and has introduced a number of APIs that help with this challenge. In this article, I will introduce you to one of these APIs, the Battery Status API.

Introduction

One of the most irritating limitations of the current generation of mobile devices is their limited battery life. It drives me crazy when the battery of my mobile phone dies when I'm out and about. No matter how small and convenient modern smartphones are, you need to remember to carry an adapter with you in case your phone or tablet runs out of juice.

As developers, we can help remedy this issue, but we usually prefer to ignore it. Until recently, developers had a good excuse as there wasn't an API to get information about the device's battery status. That excuse, however, is no longer valid thanks to the introduction of the Battery Status API. Let's take a few minutes to explore what the Battery Status API is all about.

1. What is it?

The Battery Status API, sometimes referred to as the Battery API, is an API that provides information about the system's battery level. It also defines events that are fired when changes of the battery level or status take place. Even though the Battery Status API is a W3C Candidate Recommendation, the specification hasn't changed since May 2012 so it is likely that the current API won't change much in the future.

There are several actions developers can take thanks to the availability of the Battery Status API. For example, a web application could temporarily pause a long-running process when it detects the battery level is low or critical. It's up to the developer to decide what is best for the user given the circumstances and battery level.

This may seem disruptive to the user, but by acting proactively it is possible to prevent, for example, the loss of data. If you're developing an application that manages content, then it may be useful to save the user's data more frequently when the battery is running low on power. Your users will thank you for it. It happens all too often that people lose data due to a dead battery.

Even though this may seem a bit far-fetched, but it's even possible to switch to a light-on-dark theme when the battery is running low on power. This causes the screen to draw less power from the battery and it may result in a few extra minutes of battery power.

I hope it's clear that we can take a number of precautions simply by being notified of the battery level of the device. It's time to take a look at the Battery Status API to see how all this works in practice.

2. Properties

The Battery Status API exposes four, read-only properties on the window.navigator.battery object.

  • charging: A boolean value that specifies whether the battery is charging or not. If the device doesn't have a battery or the value cannot be determined, the value of this property is set to true.
  • chargingTime: A number that specifies the number of seconds that remain until the battery is fully charged. If the battery is already fully charged or the device doesn't have a battery, then this property is set to 0. If the device isn't charging or it's unable to determine the remaining time, the value is Infinity.
  • dischargingTime: A number value that represents the number of seconds that remain until the battery is completely discharged. If the discharge time cannot be determined or the battery is currently charging, then the value is set to Infinity. If the device doesn't have a battery, then dischargingTime is also set to Infinity.
  • level: A number that specifies the current level of the battery. The value is returned as a float ranging from 0 (discharged) to 1 (fully charged). If the level of the battery cannot be determined, the battery is fully charged, or the device doesn't have a battery, then the value of level is equal to 1.

One element that surprised me when I was browsing the specification is that the chargingTime property holds the number of seconds until the battery is fully charged and not the number of seconds until the battery is fully discharged. Keep this in mind when working with the Battery Status API.

3. Events

The Battery Status API allows us to listen for four events, each of which can be mapped to the change of a property on window.navigator.battery.

  • chargingchange is fired when the device's charger is activated or deactivated.
  • chargingtimechange is fired when the remaining charging time changes.
  • dischargingtimechange is fired when the remaining time until the battery is fully discharged changes.
  • levelchange is fired when the battery level has changed.

It's important to note that you need to attach an event listener to window.navigator.battery if you wish to listen for the aforementioned events. Let's take a look at an example.

1
navigator.battery.addEventListener('levelchange', function(event) {
2
   // Do something...

3
});

The available events of the Battery Status API give developers a lot of flexibility to alter the behavior of websites and web applications. What about support? That's a good question.

4. Detecting Support

Detecting support for the Battery Status API is easy. However, as I'll discuss in the next section on browser compatibility, it's important to detect support of the API due to the low number of browsers that currently support the Battery Status API. Take a look at the following code snippet to see how easy it is to test whether the Battery Status API is supported.

1
if (window.navigator && window.navigator.battery) {
2
   // Grab the battery's information!

3
} else {
4
   // Not supported

5
}

5. Browser Compatibility

Support for the Battery Status API support is pretty poor at this point. At the moment, Firefox is the only browser that provides support for the API without the use of a vendor prefix. This is interesting since the API has several use cases that could really help in a world where mobile devices are becoming more and more dominant as devices to browse the web. A polyfill for the Battery Status API doesn't exist yet for obvious reasons. A JavaScript library, no matter how powerful, cannot access information of a hardware component without the browser's permission.

6. Demo

In this section, I'd like to show you a simple demonstration of how the Battery Status API can be used and to show some of its key features. In the demo, I've included a <div> tag with an id of bt-results in which we'll show the data retrieved from the device's battery. This element is hidden by default and only shown if the browser supports the Battery Status API, which is only Firefox at the time of writing. The demo also includes a <div> tag with an id of log in which we'll log the events fired by the Battery Status API.

With regards to the JavaScript code, the first line tests if the browser supports the Battery Status API. If the test fails, the message "API not supported" is shown. Otherwise, we attach a handler to all the events of the Battery Status API using a for loop. The handler injects the values of the properties of the battery object in the appropriate placeholder elements. You can also view a live demo.

1
<!DOCTYPE html>
2
<html>
3
   <head>
4
      <meta charset="UTF-8">
5
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
      <meta name="author" content="Aurelio De Rosa">
7
      <title>Battery Status API Demo by Aurelio De Rosa</title>
8
      <style>
9
         *
10
         {
11
            -webkit-box-sizing: border-box;
12
            -moz-box-sizing: border-box;
13
            box-sizing: border-box;
14
         }
15
16
         body
17
         {
18
            max-width: 500px;
19
            margin: 2em auto;
20
            padding: 0 0.5em;
21
            font-size: 20px;
22
         }
23
24
         h1
25
         {
26
            text-align: center;
27
         }
28
29
         .hidden
30
         {
31
            display: none;
32
         }
33
34
         .bs-info
35
         {
36
            font-weight: bold;
37
         }
38
39
         #log
40
         {
41
            height: 200px;
42
            width: 100%;
43
            overflow-y: scroll;
44
            border: 1px solid #333333;
45
            line-height: 1.3em;
46
         }
47
48
         .button-demo
49
         {
50
            padding: 0.5em;
51
            margin: 1em;
52
         }
53
54
         .author
55
         {
56
            display: block;
57
            margin-top: 1em;
58
         }
59
      </style>
60
   </head>
61
   <body>
62
      <h1>Battery Status API</h1>
63
      <span id="bs-unsupported" class="hidden">API not supported</span>
64
65
      <div id="bt-results" class="hidden">
66
         <h3>Current Status</h3>
67
         <div id="bs-status">
68
            <ul>
69
               <li>Is battery in charge? <span id="in-charge" class="bs-info">unavailable</span></li>
70
               <li>Battery will be charged in <span id="charging-time" class="bs-info">unavailable</span> seconds</li>
71
               <li>Battery will be discharged in <span id="discharging-time" class="bs-info">unavailable</span> seconds</li>
72
               <li>Current battery level: <span id="battery-level" class="bs-info">unavailable</span>/1</li>
73
            </ul>
74
         </div>
75
      </div>
76
77
      <h3>Log</h3>
78
      <div id="log"></div>
79
      <button id="clear-log" class="button-demo">Clear log</button>
80
81
      <small class="author">
82
         Demo created by <a href="http://www.audero.it">Aurelio De Rosa</a>
83
         (<a href="https://twitter.com/AurelioDeRosa">@AurelioDeRosa</a>)
84
      </small>
85
86
      <script>
87
         window.navigator = window.navigator || {};
88
         navigator.battery = navigator.battery ||
89
                             null;
90
         if (navigator.battery === null) {
91
            document.getElementById('bs-unsupported').classList.remove('hidden');
92
         } else {
93
            var log = document.getElementById('log');
94
95
            document.getElementById('bt-results').classList.remove('hidden');
96
            function updateInfo(event) {
97
               if (event !== undefined) {
98
                  log.innerHTML = 'Event "' + event.type + '" fired<br />' + log.innerHTML;
99
               }
100
               document.getElementById('in-charge').innerHTML = (navigator.battery.charging ? "Yes" : "No");
101
               document.getElementById('charging-time').innerHTML = navigator.battery.chargingTime;
102
               document.getElementById('discharging-time').innerHTML = navigator.battery.dischargingTime;
103
               document.getElementById('battery-level').innerHTML = navigator.battery.level;
104
            }
105
106
            var events = ['chargingchange', 'chargingtimechange', 'dischargingtimechange', 'levelchange'];
107
            for (var i = 0; i < events.length; i++) {
108
               navigator.battery.addEventListener(events[i], updateInfo);
109
            }
110
            updateInfo();
111
112
            document.getElementById('clear-log').addEventListener('click', function() {
113
               log.innerHTML = '';
114
            });
115
         }
116
      </script>
117
   </body>
118
</html>

Conclusion

In this article, we discussed the Battery Status API. As we've seen, it's pretty simple and offers developers the opportunity to improve the user experience of website and web applications. The Battery Status API exposes four properties and four events that we can use to change the behavior of websites and web applications by appropriately responding to changes of the device's battery status. Unfortunately, the only browser that supports the API is Firefox. However, we can expect a broader adoption in the near future. This means you can still prepare for broader support and adopt it in your next project.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.