Updated on 2026-08-14

This commit is contained in:
Tangem 2020-05-14 09:10:32 +00:00
commit 0d8ef8b8f3
4 changed files with 22 additions and 4 deletions

View file

@ -77,7 +77,16 @@ class CardSession(
runnable.run(this) { result ->
when (result) {
is CompletionResult.Success -> stop()
is CompletionResult.Failure -> stopWithError(result.error)
is CompletionResult.Failure -> {
if (result.error is TangemSdkError.ExtendedLengthNotSupported) {
if (session.environment.terminalKeys != null) {
session.environment.terminalKeys = null
startWithRunnable(runnable, callback)
return@run
}
}
stopWithError(result.error)
}
}
callback(result)
}

View file

@ -17,10 +17,10 @@ data class SessionEnvironment(
var pin1: ByteArray = DEFAULT_PIN.calculateSha256(),
var pin2: ByteArray = DEFAULT_PIN2.calculateSha256(),
var card: Card? = null,
val terminalKeys: KeyPair? = null,
var terminalKeys: KeyPair? = null,
var encryptionMode: EncryptionMode = EncryptionMode.NONE,
var encryptionKey: ByteArray? = null,
val cvc: ByteArray? = null,
var cvc: ByteArray? = null,
var cardFilter: CardFilter = CardFilter(),
val handleErrors: Boolean = true
) {

View file

@ -16,6 +16,10 @@ sealed class TangemSdkError(val code: Int) : Exception(code.toString()) {
* (e.g. a user detaches card from the phone's NFC module) while the NFC session is in progress.
*/
class TagLost : TangemSdkError(10001)
/**
* This error is returned when NFC driver on an Android device does not support sending more than 261 bytes.
*/
class ExtendedLengthNotSupported : TangemSdkError(10002)
class SerializeCommandError : TangemSdkError(20001)

View file

@ -75,7 +75,7 @@ class NfcReader : CardReader {
val rawResponse: ByteArray?
try {
Log.i(this::class.simpleName!!, "Sending data to the card")
Log.i(this::class.simpleName!!, "Sending data to the card, size is ${data?.size}")
rawResponse = isoDep?.transceive(data)
} catch (exception: TagLostException) {
callback?.invoke(CompletionResult.Failure(TangemSdkError.TagLost()))
@ -83,6 +83,11 @@ class NfcReader : CardReader {
return
} catch (exception: Exception) {
Log.i(this::class.simpleName!!, exception.localizedMessage ?: "Error tranceiving data")
// The messages of errors can vary on different Android devices,
// but we try to identify it by parsing the message.
if (exception.message?.contains("length") == true) {
callback?.invoke(CompletionResult.Failure(TangemSdkError.ExtendedLengthNotSupported()))
}
isoDep = null
return
}