WebAPI/BatteryAPI

From MozillaWiki
Jump to navigation Jump to search

Device API specification

The DAP WG (W3C) has specification sharing this proposal goal [1]. For the moment, the Battery API proposal might or might not look like the DAP specification.

[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 this URL:
http://oldworld.fr/mozilla/battery.html

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;
   readonly attribute DOMString status;
 }

window.navigator.mozBattery will return null if the device has no battery.

The charging attribute will return true if the device is currently charging and false otherwise.
The level attribute will return the current battery level of the device from 0.0 to 100.0.
The status attribute will return whether "critical", "low" or "ok" depending on the battery level.

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 1.0;
 statuschange: when the status changes.

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;