Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-device-battery

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-device-battery

Observe battery state changes in your react native application

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

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;  // <--- import

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()) // <------ add this line to yout MainActivity class
      .setUseDeveloperSupport(BuildConfig.DEBUG)
      .setInitialLifecycleState(LifecycleState.RESUMED)
      .build();

    mReactRootView.startReactApplication(mReactInstanceManager, "AndroidRNSample", null);

    setContentView(mReactRootView);
  }

  ......

}

Example Usage

import DeviceBattery from 'react-native-device-battery';

// get the battery level
DeviceBattery.getBatteryLevel().then(level => {
  console.log(level); // between 0 and 1
});

// check if the device is charging
DeviceBattery.isCharging().then(isCharging => {
  console.log(isCharging) // true or false
});

// as a listener
var onBatteryStateChanged = (state) => {
  console.log(state) // {level: 0.95, charging: true}
};

// to attach a listener
DeviceBattery.addListener(onBatteryStateChanged);

// to remove a listener
DeviceBattery.removeListener(onBatteryStateChanged);

Keywords

FAQs

Package last updated on 14 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc