1.0.0 • Published 7 years ago

phonegap-plugin-battery-status v1.0.0

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
7 years ago

title: Battery Status

description: Get events for device battery level.

phonegap-plugin-battery-status

This plugin provides an implementation based on the W3C Battery Status Events API. The method navigator.getBattery() returns a promise with a BatteryManager object which has the following event handlers:

  • onchargingchange
  • onchargingtimechange
  • ondischargingtimechange
  • onlevelchange

Installation

phonegap plugin add phonegap-plugin-battery-status

Status object

The BatteryManager object has the following properties:

  • level: The battery charge percentage (0-100). (Number)
  • charging: A boolean that indicates whether the device is plugged in. (Boolean)
  • chargingTime: The time remaining to fully charge the battery. (Number)
  • dischargingTime: The time remaining for the battery level to come down to 0. (Number)

onchargingchange event

Fires when the device is plugged in or unplugged.

Example

    navigator.getBattery().then(function(battery) {
        battery.onchargingchange = function() {
            console.log(this.level);
        };
    });

onchargingtimechange event

Fires when the time required to charge the device changes.

Example

    navigator.getBattery().then(function(battery) {
        console.log(battery.level);
        battery.onchargingtimechange = function() {
            console.log(this.level);
        };
    });

ondischargingtimechange event

Fires when the time required to discharge the device changes.

Example

    navigator.getBattery().then(function(battery) {
        battery.ondischargingtimechange = function() {
            console.log(this.level);
        };
    });

onlevelchange event

Fires when the battery level of the device changes.

Example

    navigator.getBattery().then(function(battery) {
        battery.onlevelchange = function() {
        console.log(this.level);
        };
    });

Quirks: The onchargingtimechange and the ondischargingtimechange events and the chargingtime and dischargingTime properties are not supported on iOS.