How to check battery level and percentage of android and ios device in React Native
To check the battery level in react native we can use NPM package ( react-native-device-info ). The documentation of this package is very easy. In this tutorial we will learn how to check battery level of a android and ios device using this package. Below is the setups you need to follow:-
1.) Install the react-native-device-info package
Using npm:
npm install --save react-native-device-infoor using yarn:yarn add react-native-device-infoNext you need to run below command to link the package for android and ios
react-native link react-native-device-infoUsage
Now import the package like below
import DeviceInfo from 'react-native-device-info';Get Battery Level
componentDidMount = () => { DeviceInfo.getBatteryLevel().then((batteryLevel) => { console.log(batteryLevel); }); }this will give you value like this:-
0.4923434545 = 49%
0.5 = 50%0.054747564 = 5%
1 = 100%
I hope this will help you