Device APIs

getBatteryInfo

Get battery level and charging state of the device.

Overview

my.getBatteryInfo asynchronously obtains the battery level and the charging state of the current device. No input parameters are required.

Sample Code

my.getBatteryInfo({
  success: (res) => {
    my.alert({
      content: "Battery: " + res.level + "%, Charging: " + res.isCharging,
    });
  },
  fail: (err) => {
    my.alert({ content: "Inquiry failed: " + JSON.stringify(err) });
  },
  complete: () => {
    console.log("Complete callback");
  },
});

Parameters

PropertyTypeRequiredDescription
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (executed whether the call succeeds or fails).

Success Callback Function

The incoming parameter is an Object with the following attributes:

PropertyTypeDescription
levelNumberBattery level of the current device (0–100).
isChargingBooleanWhether the device is currently charging.