index.uts 1.1 KB
Newer Older
1 2 3 4 5 6
// 引用 iOS 原生平台 api
import { UIDevice } from "UIKit";

import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';

/**
lizhongyi_'s avatar
lizhongyi_ 已提交
7
 * 导出 获取电量方法
8 9 10 11 12 13 14 15 16
 */
export const getBatteryInfo : GetBatteryInfo = function (options) {

    // 开启电量检测
    UIDevice.current.isBatteryMonitoringEnabled = true

    // 返回数据
    const res : GetBatteryInfoSuccess = {
        errMsg: "getBatteryInfo:ok",
lizhongyi_'s avatar
lizhongyi_ 已提交
17
        level: Math.abs(Number(UIDevice.current.batteryLevel * 100)),
18 19 20 21 22 23 24 25 26 27 28 29 30
        isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
    };
    options.success?.(res);
    options.complete?.(res);
}

export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {

    // 开启电量检测
    UIDevice.current.isBatteryMonitoringEnabled = true

    // 返回数据
    const res : GetBatteryInfoResult = {
lizhongyi_'s avatar
lizhongyi_ 已提交
31
        level: Math.abs(Number(UIDevice.current.batteryLevel * 100)),
32 33 34
        isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
    };
	return res;
lizhongyi_'s avatar
lizhongyi_ 已提交
35
}