Detecting battery level in a device or computer can help you inform the user of the current status. Within Mozilla’s WebAPI, we have the Battery API to offer that possibility.
Accessing the battery
First, it’s a matter of accessing the battery object:
var battery = navigator.mozBattery;
Properties
There are a few properties offered to detect the charging level of the battery in the device:
- Battery level
- Check the currenty battery level. Returns a value between 0 and 1.
- Battery charging
- A boolean, returning if the device/computer is currently being charged.
- Battery chargingTime
- Time left in seconds until it is fully charged. Available when charging.
- Battery dischargingTime
- Time left in seconds until it is discharged. Available when not charging.
// Get battery level in percentage
var batteryLevel = battery.level * 100 + "%";
// Get whether device is charging or not
var chargingStatus = battery.charging;
// Time until the device is fully charged
var batteryCharged = battery.chargingTime;
// Time until the device is discharged
var batteryDischarged = battery.dischargingTime;
Events
There are four events available for detecting changes to the battery’s status:
- levelchange
- If the battery level changes.
- chargingchange
- Detect if the device went from being charged to unplugged, or vice versa.
- chargingtimechange
- When the device’s charging time changes (when plugged in)
- dischargingtimechange
- When the device’s discharging time changed (when unplugged)
battery.addEventLister("levelchange", function () {
// Device's battery level changed
}, false);
battery.addEventListener("chargingchange", function () {
// Device got plugged in to power, or unplugged
}, false);
battery.addEventListener("chargingtimechange", function () {
// Device's charging time changed
}, false);
battery.addEventListener("dischargingtimechange", function () {
// Device's discharging time changed
}, false);
Device support
Battery API is supported in Firefox Beta on:
- Android (Firefox Aurora only, for now)
- Windows
- Linux (for those distros that have UPower installed – bundled with most nowadays)
Right now we don’t have anyone working on the Mac OS X implemementation, so if you have the skills, we’d love to see you contribute!
Demo and code
I’ve put together a basic demo of the Battery API and code is also available in the Battery API repository on GitHub.
If you don’t experience the expected results on your device, please file a bug and we can look into it. This feature is experimental at this time, and may not be ready for production use just yet.
Update: September 26th, 2012
Since this post was published, this API has now been unprefixed and support in Mac OS X has been added.
About Robert Nyman [Editor emeritus]
Technical Evangelist & Editor of Mozilla Hacks. Gives talks & blogs about HTML5, JavaScript & the Open Web. Robert is a strong believer in HTML5 and the Open Web and has been working since 1999 with Front End development for the web - in Sweden and in New York City. He regularly also blogs at http://robertnyman.com and loves to travel and meet people.
16 comments