index.uts 1.1 KB
Newer Older
雪洛's avatar
雪洛 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import batteryInfo from '@ohos.batteryInfo';
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';

export const getBatteryInfoSync : GetBatteryInfoSync = function () : GetBatteryInfoResult {
  return {
    level: batteryInfo.batterySOC,
    isCharging: batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.ENABLE || batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.FULL,
  };
}

export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
  const batteryInfoResult : GetBatteryInfoSuccess = {
    errMsg: "getBatteryInfo:ok",
    level: batteryInfo.batterySOC,
    isCharging: batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.ENABLE || batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.FULL,
  }
  try {
    options.success && options.success(batteryInfoResult)
  } catch (e) {
    console.error(e)
  }
  try {
    options.complete && options.complete(batteryInfoResult)
  } catch (e) {
    console.error(e)
  }
}