battery
Reads device battery level and charging state via the Battery Status API.
Import: @rtorcato/browser-common/battery
The Battery Status API is Chromium-only and the spec is dormant — getBatteryManager()
resolves to undefined where unsupported, so guard with isBatteryApiAvailable() first.
Example
import {
isBatteryApiAvailable,
getBatteryManager,
onBatteryLevelChange,
} from '@rtorcato/browser-common/battery'
if (isBatteryApiAvailable()) {
const battery = await getBatteryManager()
const off = battery && onBatteryLevelChange(battery, () => console.log(battery.level))
off?.()
}
Exports
isBatteryApiAvailable()— feature checkgetBatteryManager()— resolves to theBatteryManager, orundefinedonBatteryLevelChange(battery, callback)— subscribes to level changes, returns an unsubscribe functiononBatteryChargingChange(battery, callback)— subscribes to charging state changes, returns an unsubscribe function
See the API reference for full signatures.