Commit 270aae2f authored by Hein Rutjes's avatar Hein Rutjes Committed by Mehdi Achour

feat: Add `getBatteryLevel()` for checking the current battery-level of the device (#359)

parent 95b9ea3c
......@@ -2,6 +2,8 @@
### next
* Add `getBatteryLevel` (https://github.com/rebeccahughes/react-native-device-info/pull/359)
### 0.17.4
* Fix `getMACAddress` for Android > 6 (https://github.com/rebeccahughes/react-native-device-info/pull/349)
......
......@@ -196,6 +196,7 @@ var DeviceInfo = require('react-native-device-info');
| ------------------------------------------------- | ------------------- | :--: | :-----: | :-----: | ------ |
| [getAPILevel()](#getapilevel) | `number` | ❌ | ✅ | ❌ | 0.12.0 |
| [getApplicationName()](#getapplicationname) | `string` | ✅ | ✅ | ✅ | 0.14.0 |
| [getBatteryLevel()](#getbatterylevel) | `Promise<number>` | ✅ | ✅ | ❌ | 0.18.0 |
| [getBrand()](#getbrand) | `string` | ✅ | ✅ | ✅ | 0.9.3 |
| [getBuildNumber()](#getbuildnumber) | `string` | ✅ | ✅ | ✅ | ? |
| [getBundleId()](#getbundleid) | `string` | ✅ | ✅ | ✅ | ? |
......@@ -264,6 +265,24 @@ const appName = DeviceInfo.getApplicationName(); // "Learnium Mobile"
---
### getBatteryLevel()
Gets the battery level of the device as a float comprised between 0 and 1.
**Examples**
```js
DeviceInfo.getBatteryLevel().then((batteryLevel) => {
// 0.759999
});
```
**Notes**
> Returns -1 on the iOS Simulator
---
### getBrand()
Gets the device brand.
......
......@@ -304,4 +304,10 @@ RCT_EXPORT_METHOD(isPinOrFingerprintSet:(RCTResponseSenderBlock)callback)
callback(@[[NSNumber numberWithBool:isPinOrFingerprintSet]]);
}
RCT_EXPORT_METHOD(getBatteryLevel:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
float batteryLevel = [UIDevice currentDevice].batteryLevel;
resolve(@(batteryLevel));
}
@end
......@@ -4,6 +4,8 @@ import android.Manifest;
import android.app.KeyguardManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
......@@ -13,6 +15,7 @@ import android.net.wifi.WifiInfo;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.os.BatteryManager;
import android.provider.Settings.Secure;
import android.webkit.WebSettings;
import android.telephony.TelephonyManager;
......@@ -195,6 +198,15 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
return null;
}
@ReactMethod
public void getBatteryLevel(Promise p) {
Intent batteryIntent = this.reactContext.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryLevel = level / (float) scale;
p.resolve(batteryLevel);
}
@Override
public @Nullable
Map<String, Object> getConstants() {
......
......@@ -37,4 +37,5 @@ export function getCarrier(): string;
export function getTotalMemory(): number;
export function getMaxMemory(): number;
export function getTotalDiskCapacity(): number;
export function getFreeDiskStorage(): number;
\ No newline at end of file
export function getFreeDiskStorage(): number;
export function getBatteryLevel(): Promise<number>;
......@@ -115,4 +115,7 @@ module.exports = {
getFreeDiskStorage: function() {
return RNDeviceInfo.freeDiskStorage;
},
getBatteryLevel: function() {
return RNDeviceInfo.getBatteryLevel();
},
};
......@@ -38,4 +38,5 @@ declare module.exports: {
getMaxMemory: () => number,
getTotalDiskCapacity: () => number,
getFreeDiskStorage: () => number,
getBatteryLevel: () => Promise<number>,
};
......@@ -37,4 +37,5 @@ module.exports = {
maxMemory: 0,
totalDiskCapacity: 0,
freeDiskStorage: 0,
getBatteryLevel: () => Promise.resolve(0)
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment