fix: NFC读卡后先回调再退出,不能先stopScan

stopScan()会cancel自己所在的协程,导致后面的
withContext(Main){callback}永远执行不到。
改为先回调,再return@launch退出循环。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-28 21:26:23 +09:30
parent 8afffc5fec
commit 638f999f67

View File

@@ -82,13 +82,12 @@ class FiseNfcController @Inject constructor() : NfcController {
// 非空且非全零才算有效卡号(全零=无卡贴近) // 非空且非全零才算有效卡号(全零=无卡贴近)
if (nfcId.isNotEmpty() && nfcId.any { it != '0' }) { if (nfcId.isNotEmpty() && nfcId.any { it != '0' }) {
Timber.d("NFC控制: 读到卡号 $nfcId,停止轮询") Timber.d("NFC控制: 读到卡号 $nfcId")
// 读到有效卡号后立即停止轮询,避免重复回调 // 先回调再退出循环(不能先 stopScan会 cancel 自己的协程)
stopScan()
// 切回主线程回调
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
callback(nfcId) callback(nfcId)
} }
// 回调完成后退出轮询,不再读卡
return@launch return@launch
} }
} }