I am doing a network call every 15 seconds in my app, and if the users device battery percent is lower than 20%, than I would like to do the call every 30 seconds instead. How do I get the user's devices current battery level? Is it possible? Any help would be appreciated.
I am doing a network call every 15 seconds in my app, and if the users device battery percent is lower than 20%, than I would like to do the call every 30 seconds instead. How do I get the user's devices current battery level? Is it possible? Any help would be appreciated.
Share Improve this question asked Jun 10, 2017 at 9:30 KingCoder11KingCoder11 4164 silver badges19 bronze badges 8- Man i don't know whether its done till now or not but I am inspired by the way you are going and considering things. Really appreciate. – geminiousgoel Commented Jun 10, 2017 at 9:35
- I don't want to use up the users battery – KingCoder11 Commented Jun 10, 2017 at 9:35
- up vote from my side. +1 and put it under my favorite. – geminiousgoel Commented Jun 10, 2017 at 9:36
- Thanks, I am still searching on it – KingCoder11 Commented Jun 10, 2017 at 9:36
- I think I found something here, but the battery api isnt supported on many modern browsers, like opera: sitepoint./html5-battery-status-api – KingCoder11 Commented Jun 10, 2017 at 9:40
1 Answer
Reset to default 7Take a look at the Battery Status API. It doesn't work in all browsers, but it's a start.
For browsers that support it, something like this should work:
navigator.getBattery().then(function(battery) {
battery.addEventListener('levelchange', function() {
// Do stuff when the level changes, you can get it
// from battery.level
document.write((battery.level*100)+"%");
})
document.write((battery.level*100)+"%");
});