feat(sdk): 同步 iOS — upgradeFirmware 下发前校验电量 ≥40%,不足抛 BatteryTooLow
1:1 复刻 iOS:OTA 烧录前主动 getDeviceInfo 取实时电量,<40% 抛 DuooomiBleError.BatteryTooLow(current,required)、不发任何字节。 错误消息字符串与 iOS 逐字一致;新增 MIN_BATTERY_FOR_UPGRADE=40。 协程版线性实现(无 callback 拆分)。README 同步错误处理/升级段。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -452,6 +452,9 @@ class DuooomiBleSDK(context: Context, val config: DuooomiBleConfig) {
|
||||
// MARK: - High-Level APIs
|
||||
|
||||
companion object {
|
||||
/** OTA 烧录前置电量门槛(百分比)。低于此值拒绝下发,避免烧录中途断电变砖。 */
|
||||
internal const val MIN_BATTERY_FOR_UPGRADE = 40
|
||||
|
||||
private val SUPPORTED_EXTENSIONS = setOf("jpeg", "jpg", "png", "gif", "mp4", "webm", "webp")
|
||||
private val SUPPORTED_MIME_TYPES = setOf(
|
||||
"image/jpeg", "image/png", "image/gif", "image/webp",
|
||||
@@ -550,6 +553,13 @@ class DuooomiBleSDK(context: Context, val config: DuooomiBleConfig) {
|
||||
/**
|
||||
* OTA 固件烧录。完成后自动落 [UpgradeRecord] 到本地(复合主键 `(sn, firmwareId)`),
|
||||
* 等待下次 [fetchLatestFirmware] 触发 [tryReportPendingUpgrade] 对账上报。
|
||||
*
|
||||
* 下发前会**主动拉一次新鲜 [getDeviceInfo]** 校验实时电量,低于
|
||||
* [MIN_BATTERY_FOR_UPGRADE](40%)直接抛 [DuooomiBleError.BatteryTooLow],不发任何 OTA 字节。
|
||||
*
|
||||
* @throws DuooomiBleError.NotConnected 未连接
|
||||
* @throws DuooomiBleError.BatteryTooLow 电量 < [MIN_BATTERY_FOR_UPGRADE](未下发任何字节)
|
||||
* @throws DuooomiBleError.TransferFailed 烧录阶段失败;[getDeviceInfo] 的 Timeout / 解码错误同样透传
|
||||
*/
|
||||
suspend fun upgradeFirmware(
|
||||
sn: String,
|
||||
@@ -559,6 +569,12 @@ class DuooomiBleSDK(context: Context, val config: DuooomiBleConfig) {
|
||||
targetVersion: String
|
||||
) {
|
||||
ensureConnected()
|
||||
// OTA 前置电量校验:取实时电量(不用缓存,可能过期),低于门槛拒绝下发。
|
||||
val info = getDeviceInfo()
|
||||
if (info.powerlevel < MIN_BATTERY_FOR_UPGRADE) {
|
||||
BleLog.w("OTA blocked: battery ${info.powerlevel}% < $MIN_BATTERY_FOR_UPGRADE%", "Firmware")
|
||||
throw DuooomiBleError.BatteryTooLow(current = info.powerlevel, required = MIN_BATTERY_FOR_UPGRADE)
|
||||
}
|
||||
BleLog.i("OTA upgrade start: sn=$sn $fromVersion→$targetVersion (fw=$firmwareId)", "Firmware")
|
||||
transferFile(fileUri = fileUrl, commandType = CommandType.OTA_PACKAGE)
|
||||
BleLog.i("OTA upgrade completed", "Firmware")
|
||||
|
||||
Reference in New Issue
Block a user