1.9.0-BatteryManager电池充电状态信息引入完成
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.lxh.battery_health
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.BatteryManager
|
||||
|
||||
class BatteryManagerHelper(private val context: Context) {
|
||||
|
||||
fun getBatteryData(): Map<String, Any?> {
|
||||
val bm = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
|
||||
|
||||
// 电流(µA)。部分机型充电为负、放电为正,部分相反
|
||||
val rawCurrentUa = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
|
||||
|
||||
// 电压 / 状态从 ACTION_BATTERY_CHANGED 广播里拿(比 BATTERY_PROPERTY_VOLTAGE 兼容性好)
|
||||
val intent = context.registerReceiver(
|
||||
null,
|
||||
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
|
||||
)
|
||||
val voltageMv = intent?.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1) ?: -1
|
||||
val status = intent?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
|
||||
val plugged = intent?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1
|
||||
val temperatureTenth = intent?.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) ?: -1
|
||||
|
||||
val isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
|
||||
status == BatteryManager.BATTERY_STATUS_FULL
|
||||
|
||||
// 符号规约:充电为正
|
||||
val normalizedUa = when {
|
||||
rawCurrentUa == Int.MIN_VALUE -> 0
|
||||
isCharging && rawCurrentUa < 0 -> -rawCurrentUa
|
||||
!isCharging && rawCurrentUa > 0 -> -rawCurrentUa
|
||||
else -> rawCurrentUa
|
||||
}
|
||||
|
||||
// 功率 W = V × A = (mV / 1000) × (µA / 1_000_000)
|
||||
val powerW = if (voltageMv > 0 && normalizedUa != 0) {
|
||||
(voltageMv / 1000.0) * (normalizedUa / 1_000_000.0)
|
||||
} else 0.0
|
||||
|
||||
return mapOf(
|
||||
"voltageMv" to voltageMv,
|
||||
"currentUa" to normalizedUa,
|
||||
"powerW" to powerW,
|
||||
"temperatureC" to if (temperatureTenth > 0) temperatureTenth / 10.0 else 0.0,
|
||||
"plugged" to plugged, // 0=未插, 1=AC, 2=USB, 4=WIRELESS
|
||||
"isCharging" to isCharging,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -255,26 +255,35 @@ private fun clearRefreshRate() {
|
||||
|
||||
// 充电状态通道
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHARGE_CHANNEL)
|
||||
.setMethodCallHandler { call, result ->
|
||||
if (call.method == "getChargeState") {
|
||||
val isCharging = getBatteryStatus()
|
||||
val chargeType = if (isCharging) {
|
||||
val batteryIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
val plug = batteryIntent?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1
|
||||
when (plug) {
|
||||
BatteryManager.BATTERY_PLUGGED_AC -> "AC"
|
||||
BatteryManager.BATTERY_PLUGGED_USB -> "USB"
|
||||
BatteryManager.BATTERY_PLUGGED_WIRELESS -> "WIRELESS"
|
||||
else -> "UNKNOWN"
|
||||
}
|
||||
} else {
|
||||
"NONE"
|
||||
.setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"getChargeState" -> {
|
||||
val isCharging = getBatteryStatus()
|
||||
val chargeType = if (isCharging) {
|
||||
val batteryIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
val plug = batteryIntent?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1
|
||||
when (plug) {
|
||||
BatteryManager.BATTERY_PLUGGED_AC -> "AC"
|
||||
BatteryManager.BATTERY_PLUGGED_USB -> "USB"
|
||||
BatteryManager.BATTERY_PLUGGED_WIRELESS -> "WIRELESS"
|
||||
else -> "UNKNOWN"
|
||||
}
|
||||
result.success(mapOf("isCharging" to isCharging, "chargeType" to chargeType))
|
||||
} else {
|
||||
result.notImplemented()
|
||||
"NONE"
|
||||
}
|
||||
result.success(mapOf("isCharging" to isCharging, "chargeType" to chargeType))
|
||||
}
|
||||
// 🆕 新增分支
|
||||
"getBatteryManagerData" -> {
|
||||
try {
|
||||
result.success(BatteryManagerHelper(applicationContext).getBatteryData())
|
||||
} catch (e: Exception) {
|
||||
result.error("BM_ERROR", e.message, null)
|
||||
}
|
||||
}
|
||||
else -> result.notImplemented()
|
||||
}
|
||||
}
|
||||
|
||||
// SAF 通道
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SAF_CHANNEL)
|
||||
|
||||
Reference in New Issue
Block a user