expo-battery
provides battery information for the physical device (such as battery level, whether or not the device is charging, and more) as well as corresponding event listeners.Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
expo install expo-battery
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
import * as React from 'react'; import * as Battery from 'expo-battery'; import { StyleSheet, Text, View } from 'react-native'; export default class App extends React.Component { state = { batteryLevel: null, }; componentDidMount() { this._subscribe(); } componentWillUnmount() { this._unsubscribe(); } async _subscribe() { const batteryLevel = await Battery.getBatteryLevelAsync(); this.setState({ batteryLevel }); this._subscription = Battery.addBatteryLevelListener(({ batteryLevel }) => { this.setState({ batteryLevel }); console.log('batteryLevel changed!', batteryLevel); }); } _unsubscribe() { this._subscription && this._subscription.remove(); this._subscription = null; } render() { return ( <View style={styles.container}> <Text>Current Battery Level: {this.state.batteryLevel}</Text> </View> ); } }
import * as Battery from 'expo-battery';
true
on Android and physical iOS devices and false
on iOS simulators. On web, it depends on whether the browser supports the web battery API.Promise
that resolves to a number
between 0 and 1 representing the battery level, or -1 if the device does not provide it.await Battery.getBatteryLevelAsync(); // 0.759999
BatteryState.UNKNOWN
.Promise
that resolves to a Battery.BatteryState
enum value for whether the device is any of the four states.await Battery.getBatteryStateAsync(); // BatteryState.CHARGING
false
, even if the device is actually in low-power mode.Promise
that resolves to a boolean
value of either true
or false
, indicating whether low power mode is enabled or disabled, respectively.await Battery.isLowPowerModeEnabledAsync(); // true
Battery.BatteryState
enum valuetrue
if lowPowerMode is on, false
if lowPowerMode is offawait Battery.getPowerStateAsync(); // { // batteryLevel: 0.759999, // batteryState: BatteryState.UNPLUGGED, // lowPowerMode: true, // }
"android.intent.action.BATTERY_LOW"
or rises above "android.intent.action.BATTERY_OKAY"
from a low battery level. See here to read more from the Android docs.batteryLevel
key.EventSubscription
object on which you can call remove()
to unsubscribe from the listener.Battery.BatteryState
enum value for whether the device is any of the four states. On web, the event never fires.batteryState
key.EventSubscription
object on which you can call remove()
to unsubscribe from the listener.lowPowerMode
key.EventSubscription
object on which you can call remove()
to unsubscribe from the listener.BatteryState.UNKNOWN
- if the battery state is unknown or unable to accessBatteryState.UNPLUGGED
- if battery is not charging or dischargingBatteryState.CHARGING
- if battery is chargingBatteryState.FULL
- if the battery level is full