// 调用BatteryInfo.batterySOC获取电量值
let batterySoc = BatteryInfo.batterySOC;
// 调用BatteryInfo.chargingStatus获取充电状态枚举值
let batteryCharging = BatteryInfo.chargingStatus;
if (null == batterySoc) {
// Set the battery Soc as full when there is no battery hardware 设备无电池硬件时,电量默认为满电
batterySoc = mProgress;
}
if (batterySoc <= 0) {
// 确保电量值不为负数
batterySoc = Math.abs(batterySoc) * Constants.PERCENT_NUMBER;
}
this.checkBatteryStatus(batteryCharging, (result) => {
let batteryStatus = result;
// 检查电池的充电状态 将电量的值赋给mBatterySoc,
mBatterySoc.set(batterySoc);
// 电池状态赋给mBatteryCharging
// 0是false,1、2、3是true
mBatteryCharging.set(batteryStatus);
});
}
/**
* 传入参数charging,根据charging的值来赋boolean值给batteryStatus,charging为0时表示电池未充电
表示电池充电状态的枚举:
NONE 0 表示电池充电状态未知
ENABLE 1 表示电池充电状态为使能状态
DISABLE 2 表示电池充电状态为停止状态
FULL 3 表示电池充电状态为已充满状态
*
* @param {number} charging - the battery charging status
* @param {object} callback - Function callback
*/
private checkBatteryStatus(charging, callback) {
Log.showInfo(TAG, `checkBatteryStatus charging: ${charging}`);
let batteryStatus;
switch (charging) {
case Constants.NONE:
batteryStatus = false;
break;
case Constants.DISABLE:
case Constants.ENABLE:
case Constants.FULL:
batteryStatus = true;
break;
default:
batteryStatus = false;
break;
}
callback(batteryStatus);
}
```
## Wificomponent
注:官方暂无WifiInfo模块的接口文档,只有“import wifi from '@ohos.wifi'”模块的相关文档,二者部分属性的枚举值相同,可以稍做参考。