@system.battery (Battery Information)

The battery module allows you to query the charging status and remaining power of a device.

NOTE

  • Module maintenance policy:

    - For lite wearables, this module is constantly maintained and available.

    - For other device types, this module is no longer maintained since API version 6. You are advised to use @ohos.batteryInfo instead.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import {Battery, BatteryResponse } from '@kit.BasicServicesKit';

Battery.getStatus(deprecated)

getStatus(options?: GetStatusOptions): void;

Obtains the current charging state and battery level.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Parameters

Name Type Mandatory Description
options GetStatusOptions No Object that contains the API calling result. This parameter is optional and is left blank by default.

Example

ArkTS example:

Battery.getStatus({
    success: (data: BatteryResponse) => {
        console.info('success get battery level:' + data.level);
    },
    fail: (data: string, code: number) => {
        console.error('fail to get battery level code:' + code + ', data: ' + data);
    }
});

JS example:

<!-- xxx.hml -->
<div class="container">
    <input type="button" value="Get Data" style="width: 240px; height: 50px; margin: 5px;" onclick="getBatteryInfo"></input>
    <text class="title">level: {{ capacity }}</text>
    <text class="title">charging: {{ charging }}</text>
</div>
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.title {
  width: 200px;
  font-size: 30px;
  text-align: center;
}
// xxx.js
import Battery from '@system.battery';

export default {
    data: {
        capacity: '',
        charging: ''
    },
    getBatteryInfo() {
        let TAG = 'get_status_success_test';
        Battery.getStatus({
            success: (batteryResponse) => {
                this.capacity = batteryResponse.level;
                this.charging = batteryResponse.charging;
                console.info(`${TAG} batteryResponse.level: ${batteryResponse.level}`);
                console.info(`${TAG} batteryResponse.charging: ${batteryResponse.charging}`);
            },
            fail: (data, code) => {
                console.error(`${TAG} fail data: ${data}, code: ${code}`);
            },
            complete: () => {
                console.info(`${TAG} getStatus complete`);
            }
        });
    },
}

GetStatusOptions(deprecated)

Object that contains the API calling result.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Name Type Mandatory Description
success (data: BatteryResponse) => void No Called when an API call is successful. data is a return value of the BatteryResponse type.
fail (data: string, code: number) => void No Called when an API call has failed. data indicates the error information, and code indicates the error code.
complete () => void No Called when an API call is complete.

BatteryResponse(deprecated)

Defines a response that returns the charging status and remaining power of the device.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Name Type Read-Only Optional Description
charging boolean No No Whether the battery is being charged. The value true indicates that the battery is being charged; false indicates the opposite. The default value is false.
Note: This API is no longer maintained since API version 6 except for lite wearables. You are advised to use batteryInfo.chargingStatus instead.
level number No No Current battery level in percent, which ranges from 0.00 to 1.00.
Note: This API is no longer maintained since API version 6 except for lite wearables. You are advised to use batteryInfo.batterySOC instead.