Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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';
}
}
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);
})();
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);
})();
const batteryLevelListener = Battery.addBatteryLevelListener(({ batteryLevel }) =>
setBatteryLevel(batteryLevel)
);
const batteryStateListener = Battery.addBatteryStateListener(({ batteryState }) =>
setBatteryState(batteryState)
);
const lowPowerModeListener = Battery.addLowPowerModeListener(({ lowPowerMode }) =>
setLowPowerMode(lowPowerMode)
);
return () => {
batteryLevelListener && batteryLevelListener.remove();
batteryStateListener && batteryStateListener.remove();
lowPowerModeListener && lowPowerModeListener.remove();
};
}, []);
useEffect(() => {
(async () => {
const currentLevel = await Battery.getBatteryLevelAsync();
setLevel(currentLevel);
})();
const subscription = Battery.addBatteryLevelListener(({ batteryLevel }) =>
setLevel(batteryLevel)
);
return () => subscription.remove();
}, []);
return (
(async () => {
const currentLevel = await Battery.getBatteryLevelAsync();
setLevel(currentLevel);
})();
const subscription = Battery.addBatteryLevelListener(({ batteryLevel }) =>
(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);
})();
const batteryLevelListener = Battery.addBatteryLevelListener(({ batteryLevel }) =>
export const UseBatteryState: React.SFC = (props) => {
const [batteryState] = useBatteryState();
return (
This example only uses the Battery module.
It renders the current battery state, received from the listener.
Battery state
);
};
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);
})();
const batteryLevelListener = Battery.addBatteryLevelListener(({ batteryLevel }) =>
setBatteryLevel(batteryLevel)
);
const batteryStateListener = Battery.addBatteryStateListener(({ batteryState }) =>
setBatteryState(batteryState)
);
const lowPowerModeListener = Battery.addLowPowerModeListener(({ lowPowerMode }) =>
setLowPowerMode(lowPowerMode)
);
return () => {
batteryLevelListener && batteryLevelListener.remove();
batteryStateListener && batteryStateListener.remove();
lowPowerModeListener && lowPowerModeListener.remove();
};
}, []);
Battery.getBatteryStateAsync(),
Battery.isLowPowerModeEnabledAsync(),
]);
setIsAvailable(isAvailable && false);
setBatteryLevel(batteryLevel);
setBatteryState(batteryState);
setLowPowerMode(lowPowerMode);
})();
const batteryLevelListener = Battery.addBatteryLevelListener(({ batteryLevel }) =>
setBatteryLevel(batteryLevel)
);
const batteryStateListener = Battery.addBatteryStateListener(({ batteryState }) =>
setBatteryState(batteryState)
);
const lowPowerModeListener = Battery.addLowPowerModeListener(({ lowPowerMode }) =>
setLowPowerMode(lowPowerMode)
);
return () => {
batteryLevelListener && batteryLevelListener.remove();
batteryStateListener && batteryStateListener.remove();
lowPowerModeListener && lowPowerModeListener.remove();
};
}, []);
(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);
})();
const batteryLevelListener = Battery.addBatteryLevelListener(({ batteryLevel }) =>