@capawesome/capacitor-battery 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -5
- package/android/build.gradle +1 -1
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Capacitor Battery Plugin
|
|
2
2
|
|
|
3
3
|
Capacitor plugin to access battery information.
|
|
4
4
|
|
|
@@ -10,7 +10,7 @@ Capacitor plugin to access battery information.
|
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
The Capacitor Battery plugin is one of the most complete battery monitoring solutions for Capacitor apps. Here are some of the key features:
|
|
14
14
|
|
|
15
15
|
- 🔋 **Battery level**: Read the current battery level of the device.
|
|
16
16
|
- ⚡ **Battery state**: Read whether the device is charging, full or unplugged.
|
|
@@ -18,14 +18,20 @@ We are proud to offer one of the most complete and feature-rich Capacitor plugin
|
|
|
18
18
|
- 👂 **Change events**: Listen for changes to the battery level, state and low power mode.
|
|
19
19
|
- 🌐 **Web support**: Read the battery level and state on supported browsers.
|
|
20
20
|
- 📦 **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
21
|
-
-
|
|
21
|
+
- 🤝 **Compatibility**: Works alongside the [Android Battery Optimization](https://capawesome.io/docs/sdks/capacitor/android-battery-optimization/) plugin.
|
|
22
22
|
- 🔁 **Up-to-date**: Always supports the latest Capacitor version.
|
|
23
23
|
|
|
24
24
|
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
25
25
|
|
|
26
|
-
##
|
|
26
|
+
## Use Cases
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
The Battery plugin is typically used whenever an app should adapt its behavior to the device's power situation, for example:
|
|
29
|
+
|
|
30
|
+
- **Energy-aware features**: Reduce background work, animations, or sync frequency when the battery level is low.
|
|
31
|
+
- **Low power mode handling**: Disable power-hungry features when the user has enabled low power mode.
|
|
32
|
+
- **Charging-dependent tasks**: Only start heavy tasks such as large downloads while the device is charging.
|
|
33
|
+
- **Status display**: Show the current battery level and charging state inside your app, for example in a kiosk or fleet app.
|
|
34
|
+
- **Reacting to changes**: Warn the user when the battery level drops by listening for change events.
|
|
29
35
|
|
|
30
36
|
## Compatibility
|
|
31
37
|
|
|
@@ -65,6 +71,12 @@ No configuration required for this plugin.
|
|
|
65
71
|
|
|
66
72
|
## Usage
|
|
67
73
|
|
|
74
|
+
The following examples show how to get the current battery level and state, check if low power mode is enabled, listen for battery changes, and remove all listeners.
|
|
75
|
+
|
|
76
|
+
### Get the current battery level
|
|
77
|
+
|
|
78
|
+
Read the current battery level of the device as a value between `0.0` and `1.0`:
|
|
79
|
+
|
|
68
80
|
```typescript
|
|
69
81
|
import { Battery } from '@capawesome/capacitor-battery';
|
|
70
82
|
|
|
@@ -72,16 +84,40 @@ const getBatteryLevel = async () => {
|
|
|
72
84
|
const { level } = await Battery.getBatteryLevel();
|
|
73
85
|
return level;
|
|
74
86
|
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Get the current battery state
|
|
90
|
+
|
|
91
|
+
Check whether the device is charging, full or unplugged:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { Battery } from '@capawesome/capacitor-battery';
|
|
75
95
|
|
|
76
96
|
const getBatteryState = async () => {
|
|
77
97
|
const { state } = await Battery.getBatteryState();
|
|
78
98
|
return state;
|
|
79
99
|
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Check if low power mode is enabled
|
|
103
|
+
|
|
104
|
+
Read whether the low power mode (power saver mode on Android, Low Power Mode on iOS) is currently enabled. Only available on Android and iOS:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { Battery } from '@capawesome/capacitor-battery';
|
|
80
108
|
|
|
81
109
|
const isLowPowerModeEnabled = async () => {
|
|
82
110
|
const { enabled } = await Battery.isLowPowerModeEnabled();
|
|
83
111
|
return enabled;
|
|
84
112
|
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Listen for battery changes
|
|
116
|
+
|
|
117
|
+
Get notified when the battery level, battery state or low power mode changes. The device is only observed while at least one listener is attached. The `lowPowerModeChange` event is only available on Android and iOS:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { Battery } from '@capawesome/capacitor-battery';
|
|
85
121
|
|
|
86
122
|
const addBatteryLevelChangeListener = async () => {
|
|
87
123
|
await Battery.addListener('batteryLevelChange', event => {
|
|
@@ -100,6 +136,14 @@ const addLowPowerModeChangeListener = async () => {
|
|
|
100
136
|
console.log('Low power mode changed:', event.enabled);
|
|
101
137
|
});
|
|
102
138
|
};
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Remove all listeners
|
|
142
|
+
|
|
143
|
+
Remove all listeners that were registered for this plugin:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { Battery } from '@capawesome/capacitor-battery';
|
|
103
147
|
|
|
104
148
|
const removeAllListeners = async () => {
|
|
105
149
|
await Battery.removeAllListeners();
|
|
@@ -357,6 +401,46 @@ Keep the following platform differences in mind when accessing battery informati
|
|
|
357
401
|
- **iOS**: The battery level is not available on the iOS Simulator, so `getBatteryLevel()` rejects with an error there. Use a real device to test this method. The low power mode reflects the [Low Power Mode](https://support.apple.com/en-us/101604) of the device.
|
|
358
402
|
- **Web**: The battery level and state are only available in browsers that implement the [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API) (Chromium-based browsers). The low power mode is not available on the Web.
|
|
359
403
|
|
|
404
|
+
## FAQ
|
|
405
|
+
|
|
406
|
+
### How is this plugin different from other similar plugins?
|
|
407
|
+
|
|
408
|
+
It reports the full battery picture — level, charging state, and low power mode — plus change events that observe the device only while a listener is attached, all through a fully typed API that works across Android, iOS, and supported browsers on the Web. If you just need a one-off battery reading, that's a quick call; if you want to react to charging and low-power changes over time without wasting energy, this plugin is built for exactly that.
|
|
409
|
+
|
|
410
|
+
### Why does `getBatteryLevel` fail on the iOS Simulator?
|
|
411
|
+
|
|
412
|
+
The battery level is not available on the iOS Simulator, so `getBatteryLevel()` rejects with an error there. Use a real device to test this method.
|
|
413
|
+
|
|
414
|
+
### Does this plugin work in all browsers?
|
|
415
|
+
|
|
416
|
+
No, on the Web the battery level and state are only available in browsers that implement the [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API), which are Chromium-based browsers. The low power mode is not available on the Web at all.
|
|
417
|
+
|
|
418
|
+
### What is the range of the battery level value?
|
|
419
|
+
|
|
420
|
+
The battery level is returned as a value between `0.0` and `1.0`, where `0.0` means the battery is empty and `1.0` means it is fully charged. Multiply the value by 100 if you want to display it as a percentage.
|
|
421
|
+
|
|
422
|
+
### What does low power mode mean on Android and iOS?
|
|
423
|
+
|
|
424
|
+
On Android, it refers to the [power saver mode](https://developer.android.com/reference/android/os/PowerManager#isPowerSaveMode()) of the device. On iOS, it refers to the [Low Power Mode](https://support.apple.com/en-us/101604). The `isLowPowerModeEnabled()` method and the `lowPowerModeChange` event are only available on Android and iOS.
|
|
425
|
+
|
|
426
|
+
### Does listening for battery changes drain the battery?
|
|
427
|
+
|
|
428
|
+
The device is only observed while at least one listener is attached. As soon as you remove all listeners, for example with `removeAllListeners()`, the plugin stops observing the device.
|
|
429
|
+
|
|
430
|
+
### Can I use this plugin with Ionic, React, Vue or Angular?
|
|
431
|
+
|
|
432
|
+
Yes, the plugin is framework-agnostic. It works in any Capacitor app regardless of the web framework, including Ionic with Angular, React, or Vue, as well as plain JavaScript projects.
|
|
433
|
+
|
|
434
|
+
## Related Plugins
|
|
435
|
+
|
|
436
|
+
- [Android Battery Optimization](https://capawesome.io/docs/sdks/capacitor/android-battery-optimization/): Manage battery optimization settings and request exemptions on Android.
|
|
437
|
+
- [Device Info](https://capawesome.io/docs/sdks/capacitor/device-info/): Read device information, such as the model, manufacturer, operating system, and memory.
|
|
438
|
+
- [Thermal State](https://capawesome.io/docs/sdks/capacitor/thermal-state/): Read the device thermal state and react before the operating system throttles your app.
|
|
439
|
+
|
|
440
|
+
## Newsletter
|
|
441
|
+
|
|
442
|
+
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our [Capawesome Newsletter](https://cloud.capawesome.io/newsletter/).
|
|
443
|
+
|
|
360
444
|
## Changelog
|
|
361
445
|
|
|
362
446
|
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/battery/CHANGELOG.md).
|
package/android/build.gradle
CHANGED
|
@@ -30,7 +30,7 @@ android {
|
|
|
30
30
|
buildTypes {
|
|
31
31
|
release {
|
|
32
32
|
minifyEnabled false
|
|
33
|
-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
lintOptions {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capawesome/capacitor-battery",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Capacitor plugin to access battery information.",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Capacitor plugin to access battery information on Android, iOS, and Web.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
7
|
"types": "dist/esm/index.d.ts",
|
|
@@ -33,11 +33,19 @@
|
|
|
33
33
|
"url": "https://opencollective.com/capawesome"
|
|
34
34
|
}
|
|
35
35
|
],
|
|
36
|
-
"homepage": "https://capawesome.io/docs/
|
|
36
|
+
"homepage": "https://capawesome.io/docs/sdks/capacitor/battery/",
|
|
37
37
|
"keywords": [
|
|
38
38
|
"capacitor",
|
|
39
39
|
"plugin",
|
|
40
|
-
"native"
|
|
40
|
+
"native",
|
|
41
|
+
"capacitor-plugin",
|
|
42
|
+
"battery",
|
|
43
|
+
"battery level",
|
|
44
|
+
"battery status",
|
|
45
|
+
"battery state",
|
|
46
|
+
"low power mode",
|
|
47
|
+
"power saver",
|
|
48
|
+
"charging state"
|
|
41
49
|
],
|
|
42
50
|
"scripts": {
|
|
43
51
|
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|