How to use the expo-battery.BatteryState function in expo-battery

To help you get started, we’ve selected a few expo-battery examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github expo / expo / apps / native-component-list / src / screens / BatteryScreen.tsx View on Github external
function getBatteryStateString(batteryState: Battery.BatteryState): string {
  switch (batteryState) {
    case Battery.BatteryState.UNPLUGGED:
      return 'UNPLUGGED';
    case Battery.BatteryState.CHARGING:
      return 'CHARGING';
    case Battery.BatteryState.FULL:
      return 'FULL';
    case Battery.BatteryState.UNKNOWN:
    default:
      return 'UNKNOWN';
  }
}
github expo / expo / apps / native-component-list / src / screens / BatteryScreen.tsx View on Github external
export default function BatteryScreen() {
  const [isAvailable, setIsAvailable] = React.useState(null);
  const [batteryLevel, setBatteryLevel] = React.useState(-1);
  const [batteryState, setBatteryState] = React.useState(Battery.BatteryState.UNKNOWN);
  const [lowPowerMode, setLowPowerMode] = React.useState(false);

  React.useEffect(() => {
    (async () => {
      const [isAvailable, batteryLevel, batteryState, lowPowerMode] = await Promise.all([
        Battery.isAvailableAsync(),
        Battery.getBatteryLevelAsync(),
        Battery.getBatteryStateAsync(),
        Battery.isLowPowerModeEnabledAsync(),
      ]);

      setIsAvailable(isAvailable && false);
      setBatteryLevel(batteryLevel);
      setBatteryState(batteryState);
      setLowPowerMode(lowPowerMode);
    })();