Updated on 2026-08-14

This commit is contained in:
Tangem 2020-05-12 15:30:17 +03:00
parent b32d9b00d9
commit 8c24ae2010
4 changed files with 18 additions and 9 deletions

View file

@ -166,6 +166,8 @@ class CardSession(
* @param error An error that will be shown.
*/
private fun stopWithError(error: Exception) {
if (!isBusy) return
reader.closeSession()
isBusy = false
@ -175,9 +177,12 @@ class CardSession(
error.localizedMessage
}
if (error !is TangemSdkError.UserCancelled) {
Log.e("tag", "Finishing with error: $errorMessage")
Log.e(tag, "Finishing with error: $errorMessage")
viewDelegate.onError(errorMessage)
} else {
Log.i(tag, "User cancelled NFC session")
}
}
fun send(apdu: CommandApdu, callback: (result: CompletionResult<ResponseApdu>) -> Unit) {

View file

@ -33,7 +33,7 @@ class TlvDecoder(val tlvList: List<Tlv>) {
*/
inline fun <reified T> decodeOptional(tag: TlvTag): T? =
try {
decode<T>(tag)
decode<T>(tag, false)
} catch (exception: TangemSdkError.DecodingFailedMissingTag) {
null
}
@ -47,14 +47,18 @@ class TlvDecoder(val tlvList: List<Tlv>) {
*
* @return [Tlv] value converted to a nullable type [T].
*
* @throws [TaskError.MissingTag] exception if no [Tlv] is found by the Tag.
* @throws [TangemSdkError.DecodingFailedMissingTag] exception if no [Tlv] is found by the Tag.
*/
inline fun <reified T> decode(tag: TlvTag): T {
inline fun <reified T> decode(tag: TlvTag, logError: Boolean = true): T {
val tlvValue: ByteArray = tlvList.find { it.tag == tag }?.value
?: if (tag.valueType() == TlvValueType.BoolValue && T::class == Boolean::class) {
return false as T
} else {
Log.e(this::class.simpleName!!, "Tag $tag not found")
if (logError) {
Log.e(this::class.simpleName!!, "TLV $tag not found")
} else {
Log.v(this::class.simpleName!!, "TLV $tag not found")
}
throw TangemSdkError.DecodingFailedMissingTag()
}

View file

@ -61,7 +61,6 @@ class DefaultSessionViewDelegate(private val reader: NfcReader) : SessionViewDel
readingDialog?.setOnCancelListener {
reader.readingCancelled = true
reader.closeSession()
Log.i(this::class.simpleName!!, "readingCancelled is set to true")
}
readingDialog?.show()
}

View file

@ -26,7 +26,7 @@ class NfcReader : CardReader {
if (field == null) {
field = value
// if tag is received, call connect first before transceiving data
connect()
if (value != null) connect()
}
if (value == null) field = value
}
@ -46,6 +46,7 @@ class NfcReader : CardReader {
private var callback: ((response: CompletionResult<ResponseApdu>) -> Unit)? = null
override fun openSession() {
Log.i(this::class.simpleName!!, "NFC reader is starting NFC session")
readingActive = true
readingCancelled = false
manager?.disableReaderMode()
@ -85,7 +86,7 @@ class NfcReader : CardReader {
return
}
if (rawResponse != null) {
Log.i(this::class.simpleName!!, "Nfc response is received")
Log.i(this::class.simpleName!!, "NFC response is received")
data = null
}
rawResponse?.let { callback?.invoke(CompletionResult.Success(ResponseApdu(it))) }
@ -103,7 +104,7 @@ class NfcReader : CardReader {
isoDep?.close()
isoDep?.connect()
isoDep?.timeout = 240000
Log.i(this::class.simpleName!!, "Nfc session is started")
Log.i(this::class.simpleName!!, "NFC tag is connected")
}
private fun onNfcVDiscovered(nfcV: NfcV) {