Battery level, charging state, and low power mode
One.Battery reports the battery level, charging state, and low power
mode, with one-shot reads, change listeners, and hooks. Reach for it to
defer heavy work off battery, to warn before a long capture, or to dim
optional effects in low power mode.
On iOS it reads UIDevice for level and state and ProcessInfo for low
power mode. On Android it reads BatteryManager and the sticky battery
intent, plus the power saver flag. On web the Battery Status API feeds
reads and listeners where the browser has it.
import { useBatteryLevel } from 'one'
function Level() { const level = useBatteryLevel() return <>{level < 0 ? 'unknown' : `${Math.round(level * 100)}%`}</>}The hooks publish the one-shot read plus live updates: level, state, low power mode, and the combined power state. A level below zero means the device does not report one.
getLevel resolves 0 to 1, or -1 when the device reports no level.
getState resolves unknown, unplugged, charging, full, or not charging,
the last Android only. getPowerState reads level, state, and low power
mode together. isLowPowerModeEnabled reports Power Saver on Android
and Low Power Mode on iOS. isAvailable is synchronous: true on Android
and physical iOS devices, false on iOS simulators.
Each listener takes one payload object: { batteryLevel },
{ batteryState }, or { lowPowerMode }, and returns a subscription
with remove. On Android the level event fires on significant changes
only; on iOS it fires on drops of one percent or more, at most once per
minute, so level UI updates in coarse steps by platform design.
On web, without the Battery Status API, reads resolve 1, unknown, and false, and listeners never fire. Android battery optimization exemptions are not part of this module.
| Methods | Type | Description |
|---|---|---|
getLevel() => Promise<number>Level 0 to 1, or -1 when unreported. | () => Promise<number> | Level 0 to 1, or -1 when unreported. |
getState() => Promise<BatteryState>Unknown, unplugged, charging, full, or notCharging. | () => Promise<BatteryState> | Unknown, unplugged, charging, full, or notCharging. |
getPowerState() => Promise<BatteryPowerState>Level, state, and low power mode in one read. | () => Promise<BatteryPowerState> | Level, state, and low power mode in one read. |
isLowPowerModeEnabled() => Promise<boolean>Power Saver or Low Power Mode state. | () => Promise<boolean> | Power Saver or Low Power Mode state. |
isAvailable() => booleanWhether battery reads work here. | () => boolean | Whether battery reads work here. |
addLevelListener(listener: (event: BatteryLevelEvent) => void) => BatterySubscriptionHear coarse level changes. | (listener: (event: BatteryLevelEvent) => void) => BatterySubscription | Hear coarse level changes. |
addStateListener(listener: (event: BatteryStateEvent) => void) => BatterySubscriptionHear charging state changes. | (listener: (event: BatteryStateEvent) => void) => BatterySubscription | Hear charging state changes. |
addLowPowerModeListener(listener: (event: BatteryPowerModeEvent) => void) => BatterySubscriptionHear power mode flips. | (listener: (event: BatteryPowerModeEvent) => void) => BatterySubscription | Hear power mode flips. |
The hooks useBatteryLevel, useBatteryState, useLowPowerMode, and
usePowerState are named exports from one.
| Power state | Type | Description |
|---|---|---|
batteryLevelnumberLevel 0 to 1, or -1 when unreported. | number | Level 0 to 1, or -1 when unreported. |
batteryStateBatteryStateThe charging state. | BatteryState | The charging state. |
lowPowerModebooleanWhether power saving is on. | boolean | Whether power saving is on. |
| Level event | Type | Description |
|---|---|---|
batteryLevelnumberLevel 0 to 1, or -1 when unreported. | number | Level 0 to 1, or -1 when unreported. |
| State event | Type | Description |
|---|---|---|
batteryStateBatteryStateThe new charging state. | BatteryState | The new charging state. |
| Power mode event | Type | Description |
|---|---|---|
lowPowerModebooleanThe new power mode. | boolean | The new power mode. |
| Subscription | Type | Description |
|---|---|---|
remove() => voidStop this listener. | () => void | Stop this listener. |
| iOS | Android | Web | |
|---|---|---|---|
| Level | Device API | Manager API | Status API |
| Events | 1% per min | On change | On change |
| States | Four | Five | Three |
| Power mode | Low power | Power saver | Unreported |
notCharging is Android only. iOS never reports it; handle every
state on both platforms.import { useEffect, useState } from 'react'import { Text } from 'react-native'import { One } from 'one'
function ExportWarning() { const [low, setLow] = useState(false)
useEffect(() => { One.Battery.getPowerState().then((power) => { setLow(power.batteryLevel >= 0 && power.batteryLevel < 0.2) }) }, [])
if (!low) return null return <Text>Battery is low. Plug in before exporting.</Text>}One combined read answers the only question that matters here. The minus-one guard keeps unknown levels from triggering the warning.
import { useLowPowerMode } from 'one'
function Effects({ children }: { children: React.ReactNode }) { const lowPowerMode = useLowPowerMode() if (lowPowerMode) return null return <>{children}</>}The hook flips the moment the mode changes, so decorative layers drop out without any listener bookkeeping in the screen.
Edit this page on GitHub.