@capawesome/capacitor-battery
v0.1.2
Published
Capacitor plugin to access battery information on Android, iOS, and Web.
Maintainers
Readme
Capacitor Battery Plugin
Capacitor plugin to access battery information.
Features
The Capacitor Battery plugin is one of the most complete battery monitoring solutions for Capacitor apps. Here are some of the key features:
- 🔋 Battery level: Read the current battery level of the device.
- ⚡ Battery state: Read whether the device is charging, full or unplugged.
- 🪫 Low power mode: Read whether the low power mode is enabled.
- 👂 Change events: Listen for changes to the battery level, state and low power mode.
- 🌐 Web support: Read the battery level and state on supported browsers.
- 🤝 Compatibility: Works alongside the Android Battery Optimization plugin.
- 📦 CocoaPods & SPM: Supports CocoaPods and Swift Package Manager for iOS.
- 🔁 Up-to-date: Always supports the latest Capacitor version.
Missing a feature? Just open an issue and we'll take a look!
Use Cases
The Battery plugin is typically used whenever an app should adapt its behavior to the device's power situation, for example:
- Energy-aware features: Reduce background work, animations, or sync frequency when the battery level is low.
- Low power mode handling: Disable power-hungry features when the user has enabled low power mode.
- Charging-dependent tasks: Only start heavy tasks such as large downloads while the device is charging.
- Status display: Show the current battery level and charging state inside your app, for example in a kiosk or fleet app.
- Reacting to changes: Warn the user when the battery level drops by listening for change events.
Compatibility
| Plugin Version | Capacitor Version | Status | | -------------- | ----------------- | -------------- | | 0.x.x | >=8.x.x | Active support |
Guides
- The Complete Guide to Capacitor Device Sensors: How battery state pairs with the Thermal State plugin to gate background work.
Installation
You can use our AI-Assisted Setup to install the plugin. Add the Capawesome Skills to your AI tool using the following command:
npx skills add capawesome-team/skills --skill capacitor-pluginsThen use the following prompt:
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-battery` plugin in my project.If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:
npm install @capawesome/capacitor-battery
npx cap syncThis plugin does not require any additional configuration or permissions on Android or iOS.
On the Web, the battery level and state are only available in browsers that implement the Battery Status API (Chromium-based browsers). The low power mode is not available on the Web.
Configuration
No configuration required for this plugin.
Usage
The following examples show how to get the current battery level and state, check if low power mode is enabled, listen for battery changes, and remove all listeners.
Get the current battery level
Read the current battery level of the device as a value between 0.0 and 1.0:
import { Battery } from '@capawesome/capacitor-battery';
const getBatteryLevel = async () => {
const { level } = await Battery.getBatteryLevel();
return level;
};Get the current battery state
Check whether the device is charging, full or unplugged:
import { Battery } from '@capawesome/capacitor-battery';
const getBatteryState = async () => {
const { state } = await Battery.getBatteryState();
return state;
};Check if low power mode is enabled
Read whether the low power mode (power saver mode on Android, Low Power Mode on iOS) is currently enabled. Only available on Android and iOS:
import { Battery } from '@capawesome/capacitor-battery';
const isLowPowerModeEnabled = async () => {
const { enabled } = await Battery.isLowPowerModeEnabled();
return enabled;
};Listen for battery changes
Get notified when the battery level, battery state or low power mode changes. The device is only observed while at least one listener is attached. The lowPowerModeChange event is only available on Android and iOS:
import { Battery } from '@capawesome/capacitor-battery';
const addBatteryLevelChangeListener = async () => {
await Battery.addListener('batteryLevelChange', event => {
console.log('Battery level changed:', event.level);
});
};
const addBatteryStateChangeListener = async () => {
await Battery.addListener('batteryStateChange', event => {
console.log('Battery state changed:', event.state);
});
};
const addLowPowerModeChangeListener = async () => {
await Battery.addListener('lowPowerModeChange', event => {
console.log('Low power mode changed:', event.enabled);
});
};Remove all listeners
Remove all listeners that were registered for this plugin:
import { Battery } from '@capawesome/capacitor-battery';
const removeAllListeners = async () => {
await Battery.removeAllListeners();
};API
getBatteryLevel()getBatteryState()isLowPowerModeEnabled()addListener('batteryLevelChange', ...)addListener('batteryStateChange', ...)addListener('lowPowerModeChange', ...)removeAllListeners()- Interfaces
- Type Aliases
getBatteryLevel()
getBatteryLevel() => Promise<GetBatteryLevelResult>Get the current battery level of the device.
On the web, this is only supported in browsers that implement the Battery Status API (Chromium-based browsers).
Only available on Android, iOS and Web.
Returns: Promise<GetBatteryLevelResult>
Since: 0.1.0
getBatteryState()
getBatteryState() => Promise<GetBatteryStateResult>Get the current battery state of the device.
On the web, this is only supported in browsers that implement the Battery Status API (Chromium-based browsers).
Only available on Android, iOS and Web.
Returns: Promise<GetBatteryStateResult>
Since: 0.1.0
isLowPowerModeEnabled()
isLowPowerModeEnabled() => Promise<IsLowPowerModeEnabledResult>Get whether the low power mode is currently enabled.
On Android, this refers to the power saver mode. On iOS, this refers to the Low Power Mode.
Only available on Android and iOS.
Returns: Promise<IsLowPowerModeEnabledResult>
Since: 0.1.0
addListener('batteryLevelChange', ...)
addListener(eventName: 'batteryLevelChange', listenerFunc: (event: BatteryLevelChangeEvent) => void) => Promise<PluginListenerHandle>Listen for changes to the battery level of the device.
The device is only observed while at least one listener is attached.
On the web, this is only supported in browsers that implement the Battery Status API (Chromium-based browsers).
Only available on Android, iOS and Web.
| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------------------- |
| eventName | 'batteryLevelChange' |
| listenerFunc | (event: BatteryLevelChangeEvent) => void |
Returns: Promise<PluginListenerHandle>
Since: 0.1.0
addListener('batteryStateChange', ...)
addListener(eventName: 'batteryStateChange', listenerFunc: (event: BatteryStateChangeEvent) => void) => Promise<PluginListenerHandle>Listen for changes to the battery state of the device.
The device is only observed while at least one listener is attached.
On the web, this is only supported in browsers that implement the Battery Status API (Chromium-based browsers).
Only available on Android, iOS and Web.
| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------------------- |
| eventName | 'batteryStateChange' |
| listenerFunc | (event: BatteryStateChangeEvent) => void |
Returns: Promise<PluginListenerHandle>
Since: 0.1.0
addListener('lowPowerModeChange', ...)
addListener(eventName: 'lowPowerModeChange', listenerFunc: (event: LowPowerModeChangeEvent) => void) => Promise<PluginListenerHandle>Listen for changes to the low power mode of the device.
The device is only observed while at least one listener is attached.
Only available on Android and iOS.
| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------------------- |
| eventName | 'lowPowerModeChange' |
| listenerFunc | (event: LowPowerModeChangeEvent) => void |
Returns: Promise<PluginListenerHandle>
Since: 0.1.0
removeAllListeners()
removeAllListeners() => Promise<void>Remove all listeners for this plugin.
Since: 0.1.0
Interfaces
GetBatteryLevelResult
| Prop | Type | Description | Since |
| ----------- | ------------------- | --------------------------------------------------------------------------- | ----- |
| level | number | The current battery level of the device as a value between 0.0 and 1.0. | 0.1.0 |
GetBatteryStateResult
| Prop | Type | Description | Since |
| ----------- | ----------------------------------------------------- | ---------------------------------------- | ----- |
| state | BatteryState | The current battery state of the device. | 0.1.0 |
IsLowPowerModeEnabledResult
| Prop | Type | Description | Since |
| ------------- | -------------------- | ------------------------------------------------ | ----- |
| enabled | boolean | Whether the low power mode is currently enabled. | 0.1.0 |
PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| remove | () => Promise<void> |
BatteryLevelChangeEvent
| Prop | Type | Description | Since |
| ----------- | ------------------- | --------------------------------------------------------------------------- | ----- |
| level | number | The current battery level of the device as a value between 0.0 and 1.0. | 0.1.0 |
BatteryStateChangeEvent
| Prop | Type | Description | Since |
| ----------- | ----------------------------------------------------- | ---------------------------------------- | ----- |
| state | BatteryState | The current battery state of the device. | 0.1.0 |
LowPowerModeChangeEvent
| Prop | Type | Description | Since |
| ------------- | -------------------- | ------------------------------------------------ | ----- |
| enabled | boolean | Whether the low power mode is currently enabled. | 0.1.0 |
Type Aliases
BatteryState
The battery state of the device.
charging: The device is plugged into power and the battery is charging.full: The device is plugged into power and the battery is fully charged.unplugged: The device is not plugged into power and the battery is discharging.unknown: The battery state could not be determined.
'charging' | 'full' | 'unplugged' | 'unknown'
Battery Information
Keep the following platform differences in mind when accessing battery information:
- Android: The battery level and state are read from the sticky
ACTION_BATTERY_CHANGEDbroadcast. The low power mode reflects the power saver mode of the device. - iOS: The battery level is not available on the iOS Simulator, so
getBatteryLevel()rejects with an error there. Use a real device to test this method. The low power mode reflects the Low Power Mode of the device. - Web: The battery level and state are only available in browsers that implement the Battery Status API (Chromium-based browsers). The low power mode is not available on the Web.
FAQ
How is this plugin different from other similar plugins?
It reports the full battery picture — level, charging state, and low power mode — plus change events that observe the device only while a listener is attached, all through a fully typed API that works across Android, iOS, and supported browsers on the Web. If you just need a one-off battery reading, that's a quick call; if you want to react to charging and low-power changes over time without wasting energy, this plugin is built for exactly that.
Why does getBatteryLevel fail on the iOS Simulator?
The battery level is not available on the iOS Simulator, so getBatteryLevel() rejects with an error there. Use a real device to test this method.
Does this plugin work in all browsers?
No, on the Web the battery level and state are only available in browsers that implement the Battery Status API, which are Chromium-based browsers. The low power mode is not available on the Web at all.
What is the range of the battery level value?
The battery level is returned as a value between 0.0 and 1.0, where 0.0 means the battery is empty and 1.0 means it is fully charged. Multiply the value by 100 if you want to display it as a percentage.
What does low power mode mean on Android and iOS?
On Android, it refers to the power saver mode of the device. On iOS, it refers to the Low Power Mode. The isLowPowerModeEnabled() method and the lowPowerModeChange event are only available on Android and iOS.
Does listening for battery changes drain the battery?
The device is only observed while at least one listener is attached. As soon as you remove all listeners, for example with removeAllListeners(), the plugin stops observing the device.
Can I use this plugin with Ionic, React, Vue or Angular?
Yes, the plugin is framework-agnostic. It works in any Capacitor app regardless of the web framework, including Ionic with Angular, React, or Vue, as well as plain JavaScript projects.
Related Plugins
- Android Battery Optimization: Manage battery optimization settings and request exemptions on Android.
- Device Info: Read device information, such as the model, manufacturer, operating system, and memory.
- Thermal State: Read the device thermal state and react before the operating system throttles your app.
Newsletter
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our Capawesome Newsletter.
Changelog
See CHANGELOG.md.
License
See LICENSE.
