1e41f4b71Sopenharmony_ci# @ohos.batteryStatistics (Battery Statistics) (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **batteryStatistics** module provides APIs for querying software and hardware power consumption statistics.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> - The APIs provided by this module are system APIs.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Modules to Import
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```js
14e41f4b71Sopenharmony_ciimport {batteryStats} from '@kit.BasicServicesKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## batteryStats.getBatteryStats
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cigetBatteryStats(): Promise<Array&lt;BatteryStatsInfo&gt;>
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciObtains the power consumption information list. This API uses a promise to return the result.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**System API**: This is a system API.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**Return value**
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci| Type                                                 | Description                           |
30e41f4b71Sopenharmony_ci| ----------------------------------------------------- | ------------------------------- |
31e41f4b71Sopenharmony_ci| Promise<Array<[BatteryStatsInfo](#batterystatsinfo)>> | Promise used to return the power consumption information list.|
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**Error codes**
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciFor details about the error codes, see [Thermal Manager Error Codes](errorcode-batteryStatistics.md).
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci| Code  | Error Message   |
38e41f4b71Sopenharmony_ci|---------|---------|
39e41f4b71Sopenharmony_ci| 4600101 | Failed to connect to the service. |
40e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API.  |
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci**Example**
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci```js
45e41f4b71Sopenharmony_cibatteryStats.getBatteryStats()
46e41f4b71Sopenharmony_ci.then((data: batteryStats.BatteryStatsInfo[]) => {
47e41f4b71Sopenharmony_ci    console.info('battery statistics info: ' + data);
48e41f4b71Sopenharmony_ci})
49e41f4b71Sopenharmony_ci.catch((err: Error) => {
50e41f4b71Sopenharmony_ci    console.error('get battery statistics failed, err: ' + err);
51e41f4b71Sopenharmony_ci});
52e41f4b71Sopenharmony_ci```
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci## batteryStats.getBatteryStats
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_cigetBatteryStats(callback: AsyncCallback<Array&lt;BatteryStatsInfo&gt;>): void
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciObtains the power consumption information list. This API uses an asynchronous callback to return the result.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**System API**: This is a system API.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**Parameters**
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci| Name  | Type                                                       | Mandatory| Description                                                        |
67e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ |
68e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[BatteryStatsInfo](#batterystatsinfo)>> | Yes  | Callback used to return the result. If the operation is successful, **err** is undefined and **data** is the obtained Array<[BatteryStatsInfo](#batterystatsinfo)>. Otherwise, **err** is an error object. **AsyncCallback** has encapsulated an API of the **BatteryStatsInfo** class.|
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci**Error codes**
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ciFor details about the error codes, see [Thermal Manager Error Codes](errorcode-batteryStatistics.md).
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci| Code  | Error Message   |
75e41f4b71Sopenharmony_ci|---------|---------|
76e41f4b71Sopenharmony_ci| 4600101 | Failed to connect to the service. |
77e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
78e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API.  |
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci**Example**
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci```js
83e41f4b71Sopenharmony_cibatteryStats.getBatteryStats((err: Error, data : batteryStats.BatteryStatsInfo[]) => {
84e41f4b71Sopenharmony_ci    if (typeof err === 'undefined') {
85e41f4b71Sopenharmony_ci        console.info('battery statistics info: ' + data);
86e41f4b71Sopenharmony_ci    } else {
87e41f4b71Sopenharmony_ci        console.error('get battery statistics failed, err: ' + err);
88e41f4b71Sopenharmony_ci    }
89e41f4b71Sopenharmony_ci});
90e41f4b71Sopenharmony_ci```
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci## batteryStats.getAppPowerValue
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_cigetAppPowerValue(uid: number): number
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ciObtains the power consumption of an application.
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci**System API**: This is a system API.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci**Parameters**
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description       |
105e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ----------- |
106e41f4b71Sopenharmony_ci| uid    | number | Yes  | Application UID.|
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci**Return value**
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci| Type  | Description                             |
111e41f4b71Sopenharmony_ci| ------ | --------------------------------- |
112e41f4b71Sopenharmony_ci| number | Power consumption of the application with this UID, in unit of mAh.|
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci**Error codes**
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ciFor details about the error codes, see [Thermal Manager Error Codes](errorcode-batteryStatistics.md).
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci| Code  | Error Message   |
119e41f4b71Sopenharmony_ci|---------|---------|
120e41f4b71Sopenharmony_ci| 4600101 | Failed to connect to the service. |
121e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API.  |
122e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**Example**
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci```js
127e41f4b71Sopenharmony_citry {
128e41f4b71Sopenharmony_ci    let value = batteryStats.getAppPowerValue(10021);
129e41f4b71Sopenharmony_ci    console.info('battery statistics value of app is: ' + value);
130e41f4b71Sopenharmony_ci} catch(err) {
131e41f4b71Sopenharmony_ci    console.error('get battery statistics value of app failed, err: ' + err);
132e41f4b71Sopenharmony_ci}
133e41f4b71Sopenharmony_ci```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci## batteryStats.getAppPowerPercent
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_cigetAppPowerPercent(uid: number): number
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ciObtains the proportion of the power consumption of an application.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci**System API**: This is a system API.
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci**Parameters**
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description       |
148e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ----------- |
149e41f4b71Sopenharmony_ci| uid    | number | Yes  | Application UID.|
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci**Return value**
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci| Type  | Description                     |
154e41f4b71Sopenharmony_ci| ------ | ------------------------- |
155e41f4b71Sopenharmony_ci| number | Proportion of the power consumption of an application with this UID.|
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci**Error codes**
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ciFor details about the error codes, see [Thermal Manager Error Codes](errorcode-batteryStatistics.md).
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci| Code  | Error Message   |
162e41f4b71Sopenharmony_ci|---------|---------|
163e41f4b71Sopenharmony_ci| 4600101 | Failed to connect to the service. |
164e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API.  |
165e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci**Example**
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci```js
170e41f4b71Sopenharmony_citry {
171e41f4b71Sopenharmony_ci    let percent = batteryStats.getAppPowerPercent(10021);
172e41f4b71Sopenharmony_ci    console.info('battery statistics percent of app is: ' + percent);
173e41f4b71Sopenharmony_ci} catch(err) {
174e41f4b71Sopenharmony_ci    console.error('get battery statistics percent of app failed, err: ' + err);
175e41f4b71Sopenharmony_ci}
176e41f4b71Sopenharmony_ci```
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci## batteryStats.getHardwareUnitPowerValue
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_cigetHardwareUnitPowerValue(type: ConsumptionType): number
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ciObtains the power consumption of a hardware unit according to the consumption type.
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci**System API**: This is a system API.
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci**Parameters**
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci| Name| Type                               | Mandatory| Description          |
191e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | -------------- |
192e41f4b71Sopenharmony_ci| type   | [ConsumptionType](#consumptiontype) | Yes  | Power consumption type. The value must be an enum.|
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci**Return value**
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci| Type  | Description                                      |
197e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ |
198e41f4b71Sopenharmony_ci| number | Power consumption of the hardware unit corresponding to the power consumption type, in unit of mAh.|
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci**Error codes**
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ciFor details about the error codes, see [Thermal Manager Error Codes](errorcode-batteryStatistics.md).
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci| Code  | Error Message   |
205e41f4b71Sopenharmony_ci|---------|---------|
206e41f4b71Sopenharmony_ci| 4600101 | Failed to connect to the service. |
207e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
208e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API.  |
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci**Example**
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci```js
213e41f4b71Sopenharmony_citry {
214e41f4b71Sopenharmony_ci    let value = batteryStats.getHardwareUnitPowerValue(batteryStats.ConsumptionType.CONSUMPTION_TYPE_SCREEN);
215e41f4b71Sopenharmony_ci    console.info('battery statistics value of hardware is: ' + value);
216e41f4b71Sopenharmony_ci} catch(err) {
217e41f4b71Sopenharmony_ci    console.error('get battery statistics percent of hardware failed, err: ' + err);
218e41f4b71Sopenharmony_ci}
219e41f4b71Sopenharmony_ci```
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci## batteryStats.getHardwareUnitPowerPercent
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_cigetHardwareUnitPowerPercent(type: ConsumptionType): number
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ciObtains the proportion of the power consumption of a hardware unit according to the power consumption type.
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci**System API**: This is a system API.
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci**Parameters**
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci| Name| Type                               | Mandatory| Description          |
234e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | -------------- |
235e41f4b71Sopenharmony_ci| type   | [ConsumptionType](#consumptiontype) | Yes  | Power consumption type. The value must be an enum.|
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci**Return value**
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci| Type  | Description                              |
240e41f4b71Sopenharmony_ci| ------ | ---------------------------------- |
241e41f4b71Sopenharmony_ci| number | Proportion of the power consumption of the hardware unit corresponding to the power consumption type.|
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci**Error codes**
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ciFor details about the error codes, see [Thermal Manager Error Codes](errorcode-batteryStatistics.md).
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci| Code  | Error Message   |
248e41f4b71Sopenharmony_ci|---------|---------|
249e41f4b71Sopenharmony_ci| 4600101 | Failed to connect to the service. |
250e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
251e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API. |
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci**Example**
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci```js
256e41f4b71Sopenharmony_citry {
257e41f4b71Sopenharmony_ci    let percent = batteryStats.getHardwareUnitPowerPercent(batteryStats.ConsumptionType.CONSUMPTION_TYPE_SCREEN);
258e41f4b71Sopenharmony_ci    console.info('battery statistics percent of hardware is: ' + percent);
259e41f4b71Sopenharmony_ci} catch(err) {
260e41f4b71Sopenharmony_ci    console.error('get battery statistics percent of hardware failed, err: ' + err);
261e41f4b71Sopenharmony_ci}
262e41f4b71Sopenharmony_ci```
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci## BatteryStatsInfo
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ciDescribes the device power consumption information.
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci**System API**: This is a system API.
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci### Attributes
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci| Name | Type                               | Readable| Writable| Description                  |
275e41f4b71Sopenharmony_ci| ----- | ----------------------------------- | ---- | ---- | ---------------------- |
276e41f4b71Sopenharmony_ci| uid   | number                              | Yes  | No  | UID related to power consumption information.   |
277e41f4b71Sopenharmony_ci| type  | [ConsumptionType](#consumptiontype) | Yes  | No  | Power consumption type.  |
278e41f4b71Sopenharmony_ci| power | number                              | Yes  | No  | Power consumption, in unit of mAh.|
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci## ConsumptionType
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ciEnumerates power consumption types.
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci**System API**: This is a system API.
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci**System capability**: SystemCapability.PowerManager.BatteryStatistics
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci| Name                      | Value  | Description                         |
289e41f4b71Sopenharmony_ci| -------------------------- | ---- | ----------------------------- |
290e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_INVALID   | -17  | Unknown type.       |
291e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_APP       | -16  | Power consumption of an application.     |
292e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_BLUETOOTH | -15  | Power consumption of Bluetooth.     |
293e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_IDLE      | -14  | Power consumption when the CPU is idle.|
294e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_PHONE     | -13  | Power consumption of a phone call. |
295e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_RADIO     | -12  | Power consumption of wireless communication. |
296e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_SCREEN    | -11  | Power consumption of the screen.     |
297e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_USER      | -10  | Power consumption of the user.     |
298e41f4b71Sopenharmony_ci| CONSUMPTION_TYPE_WIFI      | -9   | Power consumption of Wi-Fi.   |
299