Skip to main content

@napacitor/battery

Battery level and charging state plugin (corresponds to @capacitor/battery).

Installation​

To use npm:

npm install @napacitor/battery

To use yarn:

yarn add @napacitor/battery

Sync native files:

npx nap sync

Usage​

import { Battery } from '@napacitor/battery';

const info = await Battery.getBatteryInfo();
// { batteryLevel: 0.85, isCharging: false }

// Event listeners (the native side subscribes on demand and unsubscribes when the last listener is removed)
const handle = await Battery.addListener('batteryStatusChange', (info) => {
console.log(info.batteryLevel, info.isCharging);
});

await handle.remove();
await Battery.removeAllListeners();

How It Works​

Wraps the APIs described in the .NET MAUI Battery documentation: Battery.Default.ChargeLevel / PowerSource / BatteryInfoChanged (a non-battery power source is treated as charging). This plugin is the reference implementation of the Napacitor event pipeline: the native BatteryInfoChanged is subscribed when the first JS listener registers (OnListenerAdded), unsubscribed when the last listener is removed (OnListenerRemoved), and state changes are pushed via NotifyListeners('batteryStatusChange', …).

Permissions​

Android​

No additional configuration required.

iOS​

No additional configuration required.

Windows​

No additional configuration required.

API​

getBatteryInfo()​

Gets the current battery information.

Returns: Promise<BatteryInfo>

addListener('batteryStatusChange', callback)​

Listens for battery state changes (level / charging state). The native event is subscribed when the first listener registers and unsubscribed when the last one is removed.

ParamType
eventName'batteryStatusChange'
callbackBatteryChangeCallback

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

removeAllListeners()​

Removes all listeners of this plugin.

Returns: Promise<void>

Interfaces​

BatteryInfo​

PropTypeDescription
batteryLevelnumberBattery level (0.0 - 1.0, native values normalized with Math.Clamp).
isChargingbooleanWhether the device is charging (a non-battery power source is treated as charging).