react-native-android-battery
Native Module for getting the battery status on Android devices.
Installation
Note: This package requires React Native >=0.60.
Install with npm/yarn.
npm install react-native-android-battery --save// oryarn add react-native-android-battery
Since this package requires RN 0.60 or higher, this package will be linked automatically. If you run into an issue, you can try react-native link react-native-android-battery
.
Usage
This module exposes 2 possible methods for getting the battery status: an object interface and a react hook.
;; // the interface methods for event listening and getting the current battery stateBatteryInterface; // add your listener logicBatteryInterface; // returns an object containing the battery charge percent and whether or not the battery is charging // you can also use the hook, which already has the event listener hooked upconst Component = { const batteryStatus = ; ; return <View />;};
API
The interface has 2 methods:
addEventListener(listener): void
- event listener for capturing battery status changesgetCurrentState(): object
- gets current battery status (object containingisCharging
andchargePercent
)
The hook sets up the event listener on component mount, and returns the battery status object.