Battery service provides access to device battery information, including battery level, charging state, and battery save mode status.
Usage
Add theBattery service to your page overlay and use its methods to retrieve battery information:
Methods
get_battery_level()
Gets the current battery level as a percentage.
None when it is unavailable on the current environment.
Example:
get_battery_state()
Returns the current battery state.
BatteryState enum value indicating the current state.
Example:
is_in_battery_save_mode()
Checks if the device is currently in battery save mode.
True if the device is currently in battery save mode, otherwise False.
Example:
Events
on_state_change
Called when the battery state changes (charging, discharging, full, etc.).
BatteryStateChangeEvent with:
state(BatteryState): Current battery state
Enums
BatteryState
Enum representing the battery state:
CHARGING: The battery is currently chargingCONNECTED_NOT_CHARGING: The battery is connected to a power source but not chargingDISCHARGING: The battery is dischargingFULL: The battery is fully chargedUNKNOWN: The battery state is unknown
Complete Example
Platform Availability
The Battery service is available on all mobile and desktop platforms:| Platform | Support |
|---|---|
| Android | ✓ |
| iOS | ✓ |
| macOS | ✓ |
| Windows | ✓ |
| Linux | ✓ |
| Web | ✓ |