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.
Asked
Active
Viewed 4,084 times
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 Jun 10 '17 at 09:35
-
I don't want to use up the users battery – KingCoder11 Jun 10 '17 at 09:35
-
up vote from my side. +1 and put it under my favorite. – geminiousgoel Jun 10 '17 at 09:36
-
Thanks, I am still searching on it – KingCoder11 Jun 10 '17 at 09:36
-
I think I found something here, but the battery api isnt supported on many modern browsers, like opera: https://www.sitepoint.com/html5-battery-status-api/ – KingCoder11 Jun 10 '17 at 09:40
-
Looks like my answer wasnt working on firefox mac, so I deleted it. – KingCoder11 Jun 10 '17 at 09:42
-
After having a quick look, it seems to me that it is not possible in browsers like Safari. – Erik Lumme Jun 10 '17 at 09:48
-
Looks like that to me as well. – KingCoder11 Jun 10 '17 at 09:49
1 Answers
5
Take 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)+"%");
});

KingCoder11
- 415
- 4
- 19

Erik Lumme
- 5,202
- 13
- 27
-
-
-
-
-
note, even in browsers that **do** support this API, it may not be that simple. For example, from Firefox 52 onwards, the Battery Status API is only available in chrome/privileged code (in other words, "add ons") – Jaromanda X Jun 10 '17 at 10:00