{"id":4610,"url":"https://github.com/robinpowered/react-native-device-battery","last_synced_at":"2025-04-10T03:30:20.627Z","repository":{"id":57336532,"uuid":"47232336","full_name":"robinpowered/react-native-device-battery","owner":"robinpowered","description":"Monitor device battery state changes","archived":false,"fork":false,"pushed_at":"2023-01-18T11:46:51.000Z","size":133,"stargazers_count":43,"open_issues_count":8,"forks_count":28,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-04-14T07:57:17.067Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robinpowered.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-02T02:42:00.000Z","updated_at":"2023-11-14T03:29:29.000Z","dependencies_parsed_at":"2023-02-10T14:46:35.626Z","dependency_job_id":null,"html_url":"https://github.com/robinpowered/react-native-device-battery","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinpowered%2Freact-native-device-battery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinpowered%2Freact-native-device-battery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinpowered%2Freact-native-device-battery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinpowered%2Freact-native-device-battery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robinpowered","download_url":"https://codeload.github.com/robinpowered/react-native-device-battery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248150621,"owners_count":21055955,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:17:17.830Z","updated_at":"2025-04-10T03:30:20.571Z","avatar_url":"https://github.com/robinpowered.png","language":"Java","funding_links":[],"categories":["Components"],"sub_categories":["System"],"readme":"# react-native-device-battery\n\nGet and observe the devices battery level and charging status\n\n\n## Installation\n\nInstall the node module\n```\nnpm install react-native-device-battery --save\n```\n\n### iOS\nTBD\n\n### Android\nAdd the following to `android/settings.grade`\n```\ninclude ':react-native-device-battery'\nproject(':react-native-device-battery').projectDir = new File(settingsDir, '../node_modules/react-native-device-battery/android')\n```\n\nAdd the following to `android/app/build.gradle`\n```\ncompile project(':react-native-device-battery')\n```\n\nRegister the module in `MainActivity.java`\n```java\nimport com.robinpowered.react.battery.DeviceBatteryPackage;  // \u003c--- import\n\npublic class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {\n  ......\n\n  @Override\n  protected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    mReactRootView = new ReactRootView(this);\n\n    mReactInstanceManager = ReactInstanceManager.builder()\n      .setApplication(getApplication())\n      .setBundleAssetName(\"index.android.bundle\")\n      .setJSMainModuleName(\"index.android\")\n      .addPackage(new MainReactPackage())\n      .addPackage(new DeviceBatteryPackage()) // \u003c------ add this line to yout MainActivity class\n      .setUseDeveloperSupport(BuildConfig.DEBUG)\n      .setInitialLifecycleState(LifecycleState.RESUMED)\n      .build();\n\n    mReactRootView.startReactApplication(mReactInstanceManager, \"AndroidRNSample\", null);\n\n    setContentView(mReactRootView);\n  }\n\n  ......\n\n}\n```\n\n\n## Example Usage\n```js\nimport DeviceBattery from 'react-native-device-battery';\n\n// get the battery level\nDeviceBattery.getBatteryLevel().then(level =\u003e {\n  console.log(level); // between 0 and 1\n});\n\n// check if the device is charging\nDeviceBattery.isCharging().then(isCharging =\u003e {\n  console.log(isCharging) // true or false\n});\n\n// as a listener\nvar onBatteryStateChanged = (state) =\u003e {\n  console.log(state) // {level: 0.95, charging: true}\n};\n\n// to attach a listener\nDeviceBattery.addListener(onBatteryStateChanged);\n\n// to remove a listener\nDeviceBattery.removeListener(onBatteryStateChanged);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinpowered%2Freact-native-device-battery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobinpowered%2Freact-native-device-battery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinpowered%2Freact-native-device-battery/lists"}