Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.webkitBattery not working in Google chrome

Im trying to get the battery level through battery api. It works fine in firefox. But in chrome (navigator.webkitBattery) is not working. Any help will be much appreciated. Thanks in advance.

var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;

alert(battery.level)
like image 422
sajay Avatar asked Jan 29 '26 12:01

sajay


2 Answers

well, on chrome ver 39.0+, there is navigator.getBattery method, which return a promise object when call. so the code could be like below:

var battery = navigator.battery || navigator.mozBattery;
if (battery) {
    // battery status for firefox 
    alert(battery.level * 100 + '%');
} else if (navigator.getBattery) {
    //battery status for chrome
    navigator.getBattery().then(function(battery) {
        alert(battery.level * 100 + '%');
    });
}
like image 101
james li Avatar answered Jan 31 '26 03:01

james li


The shortest answer is that it is not implemented, and is not going to be implemented for a little while.

Update: Jan 2015. This is now available in Chrome (see answer below)

like image 31
Kinlan Avatar answered Jan 31 '26 01:01

Kinlan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!