• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.batteryInfo (Battery Information)
2
3The **batteryInfo** module provides APIs for querying the charger type, battery health status, and battery charging status.
4
5> **NOTE**
6>
7> 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.
8
9
10## Modules to Import
11
12```js
13import batteryInfo from '@ohos.batteryInfo';
14```
15
16## batteryInfo.setBatteryConfig<sup>11+</sup>
17
18setBatteryConfig(sceneName: string, sceneValue: string): number
19
20Sets the battery configuration based on the specified scenario.
21
22**System API**: This is a system API.
23
24**System capability**: SystemCapability.PowerManager.BatteryManager.Core
25
26**Parameters**
27
28| Name    | Type  | Mandatory| Description        |
29| ---------- | ------ | ---- | ------------ |
30| sceneName  | string | Yes  | Scenario name.|
31| sceneValue | string | Yes  | Scenario value.|
32
33**Return value**
34
35| Type  | Description                                                      |
36| ------ | ---------------------------------------------------------- |
37| number | Operation result. The value **0** indicates that the operation is successful, and a non-zero value indicates the opposite.|
38
39**Error codes**
40
41For details about the error codes, see [Battery Information Error Codes](../errorcodes/errorcode-battery-info.md).
42
43| ID  | Error Message   |
44|---------|---------|
45| 4900101 | If connecting to the service failed. |
46
47**Example**
48
49  ```ts
50  import batteryInfo from '@ohos.batteryInfo';
51
52  let sceneName = 'xxx';
53  let sceneValue = '0';
54  let result = batteryInfo.setBatteryConfig(sceneName, sceneValue);
55
56  console.info("The result is: " + result);
57  ```
58
59## batteryInfo.getBatteryConfig<sup>11+</sup>
60
61getBatteryConfig(sceneName: string): string
62
63Obtains the battery configuration based on the specified scenario.
64
65**System API**: This is a system API.
66
67**System capability**: SystemCapability.PowerManager.BatteryManager.Core
68
69**Parameters**
70
71| Name   | Type  | Mandatory| Description        |
72| --------- | ------ | ---- | ------------ |
73| sceneName | string | Yes  | Scenario name.|
74
75**Return value**
76
77| Type  | Description                          |
78| ------ | ------------------------------ |
79| string | Operation result. The battery configuration is returned if the operation is successful. Otherwise, **""** is returned.|
80
81**Error codes**
82
83For details about the error codes, see [Battery Information Error Codes](../errorcodes/errorcode-battery-info.md).
84
85| ID  | Error Message   |
86|---------|---------|
87| 4900101 | If connecting to the service failed. |
88
89**Example**
90
91  ```ts
92  import batteryInfo from '@ohos.batteryInfo';
93
94  let sceneName = 'xxx';
95  let result = batteryInfo.getBatteryConfig(sceneName);
96
97  console.info("The result is: " + result);
98  ```
99
100## batteryInfo.isBatteryConfigSupported<sup>11+</sup>
101
102isBatteryConfigSupported(sceneName: string): boolean
103
104Checks whether the battery configuration is enabled based on the specified scenario.
105
106**System API**: This is a system API.
107
108**System capability**: SystemCapability.PowerManager.BatteryManager.Core
109
110**Parameters**
111
112| Name   | Type  | Mandatory| Description        |
113| --------- | ------ | ---- | ------------ |
114| sceneName | string | Yes  | Scenario name.|
115
116**Return value**
117
118| Type   | Description                                             |
119| ------- | ------------------------------------------------- |
120| boolean | Operation result. The value **true** indicates that the charging scenario is supported, and the value **false** indicates the opposite.|
121
122**Error codes**
123
124For details about the error codes, see [Battery Information Error Codes](../errorcodes/errorcode-battery-info.md).
125
126| ID  | Error Message   |
127|---------|---------|
128| 4900101 | If connecting to the service failed. |
129
130**Example**
131
132  ```ts
133  import batteryInfo from '@ohos.batteryInfo';
134
135  let sceneName = 'xxx';
136  let result = batteryInfo.isBatteryConfigSupported(sceneName);
137
138  console.info("The result is: " + result);
139  ```
140
141## Attributes
142
143Describes battery information.
144
145**System capability**: SystemCapability.PowerManager.BatteryManager.Core
146
147| Name     | Type       | Readable| Writable|  Description    |
148| --------------- | ------------------- | ---- | ---- | ---------------------|
149| batterySOC                                | number                                         | Yes  | No  | Battery state of charge (SoC) of the device, in unit of percentage.                          |
150| chargingStatus                            | [BatteryChargeState](#batterychargestate)      | Yes  | No  | Battery charging status of the device.                              |
151| healthStatus                              | [BatteryHealthState](#batteryhealthstate)      | Yes  | No  | Battery health status of the device.                              |
152| pluggedType                               | [BatteryPluggedType](#batterypluggedtype)      | Yes  | No  | Charger type of the device.                            |
153| voltage                                   | number                                         | Yes  | No  | Battery voltage of the device, in unit of microvolt.                        |
154| technology                                | string                                         | Yes  | No  | Battery technology of the device.                              |
155| batteryTemperature                        | number                                         | Yes  | No  | Battery temperature of the device, in unit of 0.1°C.                   |
156| isBatteryPresent<sup>7+</sup>             | boolean                                        | Yes  | No  | Whether the battery is supported or present.                |
157| batteryCapacityLevel<sup>9+</sup>         | [BatteryCapacityLevel](#batterycapacitylevel9) | Yes  | No  | Battery level of the device.                              |
158| estimatedRemainingChargeTime<sup>9+</sup> | number                                         | Yes  | No  | Estimated time for fully charging the current device, in unit of milliseconds. This is a system API.         |
159| totalEnergy<sup>9+</sup>                  | number                                         | Yes  | No  | Total battery capacity of the device, in unit of mAh. This is a system API.  |
160| nowCurrent<sup>9+</sup>                   | number                                         | Yes  | No  | Battery current of the device, in unit of mA. This is a system API.      |
161| remainingEnergy<sup>9+</sup>              | number                                         | Yes  | No  | Remaining battery capacity of the device, in unit of mAh. This is a system API.|
162
163## BatteryPluggedType
164
165Enumerates charger types.
166
167**System capability**: SystemCapability.PowerManager.BatteryManager.Core
168
169| Name      | Value | Description             |
170| -------- | ---- | ----------------- |
171| NONE     | 0    | Unknown charger type.     |
172| AC       | 1    | AC charger.|
173| USB      | 2    | USB charger.  |
174| WIRELESS | 3    | Wireless charger.|
175
176## BatteryChargeState
177
178Enumerates charging states.
179
180**System capability**: SystemCapability.PowerManager.BatteryManager.Core
181
182| Name     | Value | Description           |
183| ------- | ---- | --------------- |
184| NONE    | 0    | Unknown state.    |
185| ENABLE  | 1    | The battery is being charged. |
186| DISABLE | 2    | The battery is not being charged. |
187| FULL    | 3    | The battery is fully charged.|
188
189## BatteryHealthState
190
191Enumerates battery health states.
192
193**System capability**: SystemCapability.PowerManager.BatteryManager.Core
194
195| Name         | Value | Description          |
196| ----------- | ---- | -------------- |
197| UNKNOWN     | 0    | Unknown state.   |
198| GOOD        | 1    | The battery is in the healthy state.  |
199| OVERHEAT    | 2    | The battery is overheated.  |
200| OVERVOLTAGE | 3    | The battery voltage is over high.  |
201| COLD        | 4    | The battery temperature is low.  |
202| DEAD        | 5    | The battery is dead.|
203
204## BatteryCapacityLevel<sup>9+</sup>
205
206Enumerates battery levels.
207
208**System capability**: SystemCapability.PowerManager.BatteryManager.Core
209
210| Name          | Value| Description                      |
211| -------------- | ------ | ---------------------------- |
212| LEVEL_FULL     | 1      | Full battery level.  |
213| LEVEL_HIGH     | 2      | High battery level.  |
214| LEVEL_NORMAL   | 3      | Normal battery level.|
215| LEVEL_LOW      | 4      | Low battery level.  |
216| LEVEL_WARNING  | 5      | Alarm battery level.|
217| LEVEL_CRITICAL | 6      | Ultra-low battery level.|
218| LEVEL_SHUTDOWN | 7      | Power-down battery level.|
219
220## CommonEventBatteryChangedKey<sup>9+</sup>
221
222Enumerates keys for querying the additional information about the **COMMON_EVENT_BATTERY_CHANGED** event.
223
224**System capability**: SystemCapability.PowerManager.BatteryManager.Core
225
226| Name                | Value| Description                                            |
227| -------------------- | ------ | -------------------------------------------------- |
228| EXTRA_SOC            | "soc" | Remaining battery level in percentage.                  |
229| EXTRA_CHARGE_STATE   | "chargeState" | Battery charging status of the device.                |
230| EXTRA_HEALTH_STATE   | "healthState" | Battery health status of the device.                |
231| EXTRA_PLUGGED_TYPE   | "pluggedType" | Type of the charger connected to the device.            |
232| EXTRA_VOLTAGE        | "voltage" | Battery voltage of the device.                    |
233| EXTRA_TECHNOLOGY     | "technology" | Battery technology of the device.                |
234| EXTRA_TEMPERATURE    | "temperature" | Battery temperature of the device.                    |
235| EXTRA_PRESENT        | "present" | Whether the battery is supported by the device or installed.|
236| EXTRA_CAPACITY_LEVEL | "capacityLevel" | Battery level of the device.                |
237