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