js-apis-battery-info.md 6.1 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.batteryInfo (Battery Information)
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The **batteryInfo** module provides APIs for querying the charger type, battery health status, and battery charging status.
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6
> **NOTE**
>
S
shawn_he 已提交
7 8
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

S
shawn_he 已提交
9

10
## Modules to Import
Z
zengyawen 已提交
11

S
shawn_he 已提交
12
```js
Z
zengyawen 已提交
13 14 15
import batteryInfo from '@ohos.batteryInfo';
```

16
## Attributes
Z
zengyawen 已提交
17 18 19

Describes battery information.

S
shawn_he 已提交
20 21 22 23 24
**System capability**: SystemCapability.PowerManager.BatteryManager.Core

| Name     | Type       | Readable| Writable|  Description    |
| --------------- | ------------------- | ---- | ---- | ---------------------|
| batterySOC                                | number                                         | Yes  | No  | Battery state of charge (SoC) of the device, in unit of percentage.                          |
S
shawn_he 已提交
25 26
| chargingStatus                            | [BatteryChargeState](#batterychargestate)      | Yes  | No  | Battery charging status of the device.                              |
| healthStatus                              | [BatteryHealthState](#batteryhealthstate)      | Yes  | No  | Battery health status of the device.                              |
S
shawn_he 已提交
27 28 29 30 31 32
| pluggedType                               | [BatteryPluggedType](#batterypluggedtype)      | Yes  | No  | Charger type of the device.                            |
| voltage                                   | number                                         | Yes  | No  | Battery voltage of the device, in unit of microvolt.                        |
| technology                                | string                                         | Yes  | No  | Battery technology of the device.                              |
| batteryTemperature                        | number                                         | Yes  | No  | Battery temperature of the device, in unit of 0.1°C.                   |
| isBatteryPresent<sup>7+</sup>             | boolean                                        | Yes  | No  | Whether the battery is supported or present.                |
| batteryCapacityLevel<sup>9+</sup>         | [BatteryCapacityLevel](#batterycapacitylevel9) | Yes  | No  | Battery level of the device.                              |
S
shawn_he 已提交
33 34 35 36
| estimatedRemainingChargeTime<sup>9+</sup> | number                                         | Yes  | No  | Estimated time for fully charging the current device, in unit of milliseconds. This is a system API.         |
| totalEnergy<sup>9+</sup>                  | number                                         | Yes  | No  | Total battery capacity of the device, in unit of mAh. This is a system API.  |
| nowCurrent<sup>9+</sup>                   | number                                         | Yes  | No  | Battery current of the device, in unit of mA. This is a system API.      |
| remainingEnergy<sup>9+</sup>              | number                                         | Yes  | No  | Remaining battery capacity of the device, in unit of mAh. This is a system API.|
Z
zengyawen 已提交
37

38
## BatteryPluggedType
Z
zengyawen 已提交
39 40 41

Enumerates charger types.

S
shawn_he 已提交
42
**System capability**: SystemCapability.PowerManager.BatteryManager.Core
Z
zengyawen 已提交
43

S
shawn_he 已提交
44 45
| Name      | Value | Description             |
| -------- | ---- | ----------------- |
S
shawn_he 已提交
46 47 48 49
| NONE     | 0    | Unknown charger type.     |
| AC       | 1    | AC charger.|
| USB      | 2    | USB charger.  |
| WIRELESS | 3    | Wireless charger.|
50 51

## BatteryChargeState
Z
zengyawen 已提交
52 53 54

Enumerates charging states.

S
shawn_he 已提交
55
**System capability**: SystemCapability.PowerManager.BatteryManager.Core
56

S
shawn_he 已提交
57 58 59 60 61 62
| Name     | Value | Description           |
| ------- | ---- | --------------- |
| NONE    | 0    | Unknown state.    |
| ENABLE  | 1    | The battery is being charged. |
| DISABLE | 2    | The battery is not being charged. |
| FULL    | 3    | The battery is fully charged.|
Z
zengyawen 已提交
63

64
## BatteryHealthState
Z
zengyawen 已提交
65 66 67

Enumerates battery health states.

S
shawn_he 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
**System capability**: SystemCapability.PowerManager.BatteryManager.Core

| Name         | Value | Description          |
| ----------- | ---- | -------------- |
| UNKNOWN     | 0    | Unknown state.   |
| GOOD        | 1    | The battery is in the healthy state.  |
| OVERHEAT    | 2    | The battery is overheated.  |
| OVERVOLTAGE | 3    | The battery voltage is over high.  |
| COLD        | 4    | The battery temperature is low.  |
| DEAD        | 5    | The battery is dead.|

## BatteryCapacityLevel<sup>9+</sup>

Enumerates battery levels.

**System capability**: SystemCapability.PowerManager.BatteryManager.Core

| Name          | Value| Description                      |
| -------------- | ------ | ---------------------------- |
| LEVEL_FULL     | 1      | Full battery level.  |
| LEVEL_HIGH     | 2      | High battery level.  |
| LEVEL_NORMAL   | 3      | Normal battery level.|
| LEVEL_LOW      | 4      | Low battery level.  |
S
shawn_he 已提交
91 92 93
| LEVEL_WARNING  | 5      | Alarm battery level.|
| LEVEL_CRITICAL | 6      | Ultra-low battery level.|
| LEVEL_SHUTDOWN | 7      | Power-down battery level.|
S
shawn_he 已提交
94

S
shawn_he 已提交
95
## CommonEventBatteryChangedKey<sup>9+</sup>
S
shawn_he 已提交
96 97 98 99 100 101 102

Enumerates keys for querying the additional information about the **COMMON_EVENT_BATTERY_CHANGED** event.

**System capability**: SystemCapability.PowerManager.BatteryManager.Core

| Name                | Value| Description                                            |
| -------------------- | ------ | -------------------------------------------------- |
S
shawn_he 已提交
103 104 105 106 107 108 109 110 111
| EXTRA_SOC            | "soc" | Remaining battery level in percentage.                  |
| EXTRA_CHARGE_STATE   | "chargeState" | Battery charging status of the device.                |
| EXTRA_HEALTH_STATE   | "healthState" | Battery health status of the device.                |
| EXTRA_PLUGGED_TYPE   | "pluggedType" | Type of the charger connected to the device.            |
| EXTRA_VOLTAGE        | "voltage" | Battery voltage of the device.                    |
| EXTRA_TECHNOLOGY     | "technology" | Battery technology of the device.                |
| EXTRA_TEMPERATURE    | "temperature" | Battery temperature of the device.                    |
| EXTRA_PRESENT        | "present" | Whether the battery is supported by the device or installed.|
| EXTRA_CAPACITY_LEVEL | "capacityLevel" | Battery level of the device.                |