Cordova battery API does not work

Asked

Viewed 365 times

0

I’m trying to use the Cordova Batterystatus API and I’m not getting it, neither in the Intel XDK emulator, nor in the debug on my Android and when I get a .APK. It just doesn’t happen when I run.

Basically I’m using this code found in http://cordova.apache.org/docs/en/2.5.0/cordova_events_events.md.html#batterystatus:

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

function onBatteryStatus(info) {
    // Handle the online event
    console.log("Level: " + info.level + " isPlugged: " + info.isPlugged); 
}

2 answers

1

You need to wait for the event deviceready from Cordova. Try as follows:

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 + "%"); 
}

0


What is the expected result?

It is necessary to understand that Cordova does not inform the level of the battery about requisition. Only when the event batterystatus is fired you will get some return.

I mean, you’ll only have the level battery when receiving a low battery status, level altered, connected power cable (or any other battery status).

If you need a plugin to inform you level of the battery at any time can check this reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.