Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
R
react-native-device-info
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
放牛的园子
react-native-device-info
Commits
270aae2f
Commit
270aae2f
authored
Mar 14, 2018
by
Hein Rutjes
Committed by
Mehdi Achour
Mar 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Add `getBatteryLevel()` for checking the current battery-level of the device (#359)
parent
95b9ea3c
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
46 additions
and
2 deletions
+46
-2
CHANGELOG.md
CHANGELOG.md
+2
-0
README.md
README.md
+19
-0
RNDeviceInfo.m
RNDeviceInfo/RNDeviceInfo.m
+6
-0
RNDeviceModule.java
...c/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java
+12
-0
deviceinfo.d.ts
deviceinfo.d.ts
+2
-2
deviceinfo.js
deviceinfo.js
+3
-0
deviceinfo.js.flow
deviceinfo.js.flow
+1
-0
index.js
web/index.js
+1
-0
No files found.
CHANGELOG.md
View file @
270aae2f
...
...
@@ -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)
...
...
README.md
View file @
270aae2f
...
...
@@ -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.
...
...
RNDeviceInfo/RNDeviceInfo.m
View file @
270aae2f
...
...
@@ -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
android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java
View file @
270aae2f
...
...
@@ -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
()
{
...
...
deviceinfo.d.ts
View file @
270aae2f
...
...
@@ -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
>
;
deviceinfo.js
View file @
270aae2f
...
...
@@ -115,4 +115,7 @@ module.exports = {
getFreeDiskStorage
:
function
()
{
return
RNDeviceInfo
.
freeDiskStorage
;
},
getBatteryLevel
:
function
()
{
return
RNDeviceInfo
.
getBatteryLevel
();
},
};
deviceinfo.js.flow
View file @
270aae2f
...
...
@@ -38,4 +38,5 @@ declare module.exports: {
getMaxMemory: () => number,
getTotalDiskCapacity: () => number,
getFreeDiskStorage: () => number,
getBatteryLevel: () => Promise<number>,
};
web/index.js
View file @
270aae2f
...
...
@@ -37,4 +37,5 @@ module.exports = {
maxMemory
:
0
,
totalDiskCapacity
:
0
,
freeDiskStorage
:
0
,
getBatteryLevel
:
()
=>
Promise
.
resolve
(
0
)
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment