Battery Manager API

Sreelal TS
2 min readOct 17, 2018

--

BatteryManager API

This API lets us keep track of the system’s battery charge level. This API is obsolete. It works only for some browsers.

First, we need to know if this API is available on the clients’ browser.

‘BatteryManager’ in window

This will return true only if BatteryManager is available on the clients’ browser.

Now let’s check its properties:

Properties

  • BatteryManager.charging
  • This will return a boolean value indicating whether or not the battery is currently being charged
  • BatteryManager.chargingTime
  • A number representing the remaining time in seconds until the battery is fully charged, or 0 if the battery is already fully charged.
  • BatteryManager.dischargingTime
  • A number representing the remaining time in seconds until the battery is completely discharged or Infinity if the battery is currently being charged.
  • BatteryManager.level
  • This will return a number in the range of 0.0 to 1.0 which represents the battery charge level.

Then, event handlers:

Event Handlers

  • BatteryManager.addEventListener
  • A handler for the chargingchange event. This vent is sent when the battery charging state is updated
  • BatteryManager.onchargingtimechange
  • A handler for the chargingtimechange event. This event is sent when the battery charging time is updated.
  • BatteryManager.ondischargingtimechange
  • A handler for the dischargingtimechange event. This event is sent when the battery discharging time is updated.
  • BatteryManager.onlevelchange
  • A handler for the levelchange event. This event is sent when the battery level is updated.

Now let’s try it out…

Consider, we need to get the battery level of the device.

It’s clear from the example, the method getBattery() of navigator will return a Promise of proto of BatteryManager.

For further reference of a particular battery state, you can get the promise into a variable and then collect the Promise values.

Read more on W3C -> here.

Happy coding,

Thanks :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sreelal TS
Sreelal TS

Written by Sreelal TS

codes • dreams • thoughts . 💙 Organizer, Flutter Kozhikode 🥇 Platinum Product Expert at Google PE Program #Flutter #GDGKozhikode

No responses yet

Write a response