diff --git a/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/IsoDepExtensions.kt b/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/IsoDepExtensions.kt index 82910f1a..35b1fa78 100644 --- a/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/IsoDepExtensions.kt +++ b/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/IsoDepExtensions.kt @@ -2,13 +2,24 @@ package com.tangem.sdk.nfc import android.nfc.tech.IsoDep import com.tangem.Log +import java.io.IOException -internal fun IsoDep.connectInternal() { +internal fun IsoDep.connectInternal(onError: () -> Unit) { Log.nfc { "connectInternal" } - this.connect() + try { + this.connect() + } catch (e: IOException) { + Log.nfc { "connectInternal error $e" } + onError() + } } -internal fun IsoDep.closeInternal() { +internal fun IsoDep.closeInternal(onError: () -> Unit) { Log.nfc { "closeInternal" } - this.close() + try { + this.close() + } catch (e: IOException) { + Log.nfc { "closeInternal error $e" } + onError() + } } diff --git a/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcManager.kt b/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcManager.kt index 165a33de..7f8fc83b 100644 --- a/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcManager.kt +++ b/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcManager.kt @@ -14,7 +14,6 @@ import androidx.lifecycle.LifecycleOwner import com.tangem.Log import com.tangem.common.extensions.VoidCallback import com.tangem.common.nfc.ReadingActiveListener -import kotlinx.coroutines.plus /** * Helps use NFC, leveraging Android NFC functionality. @@ -75,16 +74,28 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener, DefaultLife } override fun onStart(owner: LifecycleOwner) { - enableReaderModeIfNfcEnabled() reader.listener = this } - override fun onStop(owner: LifecycleOwner) { + override fun onResume(owner: LifecycleOwner) { + super.onResume(owner) + enableReaderModeIfNfcEnabled() + } + + override fun onPause(owner: LifecycleOwner) { + super.onPause(owner) disableReaderMode() + } + + override fun onStop(owner: LifecycleOwner) { reader.stopSession(true) reader.listener = null } + override fun onStartSession() { + enableReaderModeIfNfcEnabled() + } + override fun onDestroy(owner: LifecycleOwner) { activity?.unregisterReceiver(mBroadcastReceiver) activity = null @@ -116,7 +127,11 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener, DefaultLife private fun ignoreTag(tag: Tag?) { Log.nfc { "NFC tag is ignored" } nfcAdapter?.ignore(tag, IGNORE_DEBOUNCE_MS, null, null) - IsoDep.get(tag)?.closeInternal() + IsoDep.get(tag)?.closeInternal( + onError = { + Log.nfc { "ignoreTag close failure" } + }, + ) } private companion object { diff --git a/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcReader.kt b/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcReader.kt index 1105785e..fe975556 100644 --- a/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcReader.kt +++ b/tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcReader.kt @@ -48,6 +48,7 @@ class NfcReader : CardReader { scope?.launchWithLock(readerMutex) { Log.nfc { "start NFC session, thread ${Thread.currentThread().id}" } nfcTag = null + listener?.onStartSession() listener?.readingIsActive = true } } @@ -62,6 +63,7 @@ class NfcReader : CardReader { override fun resumeSession() { scope?.launchWithLock(readerMutex) { Log.nfc { "resume NFC session, thread ${Thread.currentThread().id}" } + listener?.onStartSession() listener?.readingIsActive = true } } @@ -73,24 +75,32 @@ class NfcReader : CardReader { return@launchWithLock } IsoDep.get(tag)?.let { isoDep -> - connect(isoDep) - nfcTag = NfcTag(TagType.Nfc, isoDep) + connect( + isoDep, + onSuccess = { + nfcTag = NfcTag(TagType.Nfc, isoDep) + }, + onError = { + nfcTag = null + }, + ) } } } - private suspend fun connect(isoDep: IsoDep) { + private suspend fun connect(isoDep: IsoDep, onSuccess: (IsoDep) -> Unit, onError: () -> Unit) { Log.nfc { "connect" } if (isoDep.isConnected) { Log.nfc { "already connected close and reconnect" } - isoDep.closeInternal() + isoDep.closeInternal(onError) delay(CONNECTION_DELAY) - isoDep.connectInternal() + isoDep.connectInternal(onError) } else { - isoDep.connectInternal() + isoDep.connectInternal(onError) Log.nfc { "connected" } } isoDep.timeout = ISO_DEP_TIMEOUT_MS + onSuccess(isoDep) } override fun stopSession(cancelled: Boolean) { diff --git a/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt b/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt index 37d4b9ea..28cb613f 100644 --- a/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt +++ b/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt @@ -1089,6 +1089,7 @@ class TangemSdk( accessCode = accessCode, preflightReadFilter = CardIdPreflightReadFilter.initOrNull(cardId), ) + cardSession?.start(onSessionStarted = callback) } diff --git a/tangem-sdk-core/src/main/java/com/tangem/common/nfc/CardReader.kt b/tangem-sdk-core/src/main/java/com/tangem/common/nfc/CardReader.kt index 074c420e..180fd80c 100644 --- a/tangem-sdk-core/src/main/java/com/tangem/common/nfc/CardReader.kt +++ b/tangem-sdk-core/src/main/java/com/tangem/common/nfc/CardReader.kt @@ -58,4 +58,6 @@ interface CardReader { interface ReadingActiveListener { var readingIsActive: Boolean + + fun onStartSession() }