8

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.

KingCoder11
  • 415
  • 4
  • 19

1 Answers1

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