Cordova plugin to get battery status on demand.
- iOS (including iOS8)
- android
The plugin can be installed from git repository.
From master:
# ~~ from master branch ~~
cordova plugin add https://github.com/angel1st/cordova-plugin-battery-status-ondemand.git
The plugin creates the object cordova.plugins.battery
with the following method:
battery.level [battery.isPluggedIn][is-plugged-in] [battery.status][status]
The plugin and its methods are not available before the deviceready event has been fired.
document.addEventListener('deviceready', function () {
// cordova.plugins.battery.level is now available
}, false);
The battery level can be accessed through the battery.level
interface.
The method takes a callback function as its argument which will be called with the battery level. Optional the scope of the callback function ca be defined through a second argument.
cordova.plugins.battery.level(function (battLevel) {
// console.log('battery level: ' + battLevel);
}, scope);
The isPluggedIn flag can be accessed through the battery.level.isPluggedIn
interface.
The method takes a callback function as its argument which will be called with the isPluggedIn flag. Optional the scope of the callback function ca be defined through a second argument.
cordova.plugins.battery.isPluggedIn(function (isPluggedIn) {
// console.log('device is ' + isPluggedIn ? 'plugged in' : 'plugged out');
}, scope);
The third method returns and object {level: batteryLevel, isPlugged: isDevicePlugged}
. Basically, this method provides two above methods information at once.
It is available via cordova.plugins.battery.level.status
interface.
The method takes a callback function as its argument which will return status
object. Optional the scope of the callback function ca be defined through a second argument.
cordova.plugins.battery.status(function (battStatus) {
// console.log('battery status: ' + JSON.stringify(battStatus));
}, scope);
This software is released under the Apache 2.0 License.
© 2018 Angel Todorov. All rights reserved