Personal tools

WebAPI/BatteryAPI

From MozillaWiki

Jump to: navigation, search
This wiki page is out of date.

The work on Battery API has been moved to the official W3C specification in the DAP Working Group: http://www.w3.org/TR/battery-status/

Contents

Device API specification

The DAP WG (W3C) has specification sharing this proposal goal [1]. The specification recently moved to match our proposal. So, most of it should look like the proposed API below.

[1] http://www.w3.org/TR/2011/WD-battery-status-20110915/

Status

The proposed specification has a working implementation in bug 678694. From this implementation, we will request feedbacks for changes.

Demo

You can find a demo at these URLs:

Proposed API

There is an object in window.navigator named mozBattery with the following interface:

 interface BatteryManager : EventTarget
 {
   readonly attribute boolean   charging;
   readonly attribute float     level;
 
            attribute Function? onchargingchange;
            attribute Function? onlevelchange;
 }

The charging attribute will return true if the device is currently charging and false otherwise. True if there is no battery or if this information is not available.
The level attribute will return the current battery level of the device from 0.0 to 1.0. 1.0 if there is no battery or if this information is not available.

The following events can be sent to the mozBattery object:

 chargingchange: when the charging state changes;
 levelchange: when the level value changes of at least 0.01;

Considered changes

  • mozBattery object might move to window.navigator.device;
  • Instead of having a mozBattery object in navigator.* we could instantiate an object with new mozBatteryStatus();
    • In that case, we could allow authors to set the low and critical values threshold;
    • In that case, not having a battery should be handled differently;