Cordova battery API does not work

0

I'm trying to use the BatteryStatus Corel API and I'm not getting it, neither in the Intel XDK emulator nor in the debug on my Android, nor when I make a .APK. It just does not happen when I run.

Basically, using this code found in link :

    window.addEventListener("batterystatus", onBatteryStatus, false);

function onBatteryStatus(info) {
    // Handle the online event
    console.log("Level: " + info.level + " isPlugged: " + info.isPlugged); 
}
    
asked by anonymous 06.11.2014 / 13:23

2 answers

0

What is the expected result?

It should be understood that Cordova does not report the battery level upon request. Only when the batterystatus event is triggered will you get some feedback.

In other words, you will only have level of the battery when you receive a status of low battery,% changed%, connected power cord (or any other battery status).

If you need a plugin that tells you the level of the battery at any time you can check this answer .

    
20.11.2014 / 12:02
1

You have to wait for the deviceready event in Cordova. Please try the following:

document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is loaded and it is now safe to make calls Cordova methods
function onDeviceReady() {
    window.addEventListener("batterylow", onBatteryLow, false);
}

// Handle the batterylow event
function onBatteryLow(info) {
    alert("Battery Level Low " + info.level + "%"); 
}
    
28.12.2014 / 13:40