react-native-device-battery
Get and observe the devices battery level and charging status
Installation
Install the node module
npm install react-native-device-battery --save
iOS
TBD
Android
Add the following to android/settings.grade
include ':react-native-device-battery'
project(':react-native-device-battery').projectDir = new File(settingsDir, '../node_modules/react-native-device-battery/android')
Add the following to android/app/build.gradle
compile project(':react-native-device-battery')
Register the module in MainActivity.java
import com.robinpowered.react.battery.DeviceBatteryPackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new DeviceBatteryPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "AndroidRNSample", null);
setContentView(mReactRootView);
}
......
}
Example Usage
import DeviceBattery from 'react-native-device-battery';
DeviceBattery.getBatteryLevel().then(level => {
console.log(level);
});
DeviceBattery.isCharging().then(isCharging => {
console.log(isCharging)
});
var onBatteryStateChanged = (state) => {
console.log(state)
};
DeviceBattery.addListener(onBatteryStateChanged);
DeviceBattery.removeListener(onBatteryStateChanged);