How to check battery level and percentage of android and ios device in React Native

How to check battery level and percentage of android and ios device in React Native

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-info

or using yarn:
yarn add react-native-device-info

Next you need to run below command to link the package for android and ios

react-native link react-native-device-info

Usage

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

Post Created 130

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top