The getBattery()
method provides information about the system's battery. It returns a battery promise, which is resolved in a BatteryManager
object providing also some new events you can handle to monitor the battery status. This implements the Battery Status API; see that documentation for additional details, a guide to using the API, and sample code.
A Promise
that fulfills with a BatteryManager
object which you can use to get information about the battery's state.
This example fetches the current charging state of the battery and establishes a handler for the chargingchange
event, so that the charging state is recorded whenever it changes.
let batteryIsCharging = false;
navigator.getBattery().then((battery) => {
batteryIsCharging = battery.charging;
battery.addEventListener("chargingchange", () => {
batteryIsCharging = battery.charging;
});
});
For more examples and details, see Battery Status API.