Skip to content

Commit b7f4324

Browse files
committed
improve compatibility
1 parent c94ce3f commit b7f4324

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.8
2+
3+
* Improve compatibility
4+
15
## 0.1.7
26

37
* Downgrade to Flutter 3.24

android/src/main/kotlin/im/nfc/ccid/Ccid.kt

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,42 @@ class Ccid(
129129

130130
private fun receiveRawMessage(expectedSeq: Byte): CcidRdrToPcMessage {
131131
var retries = 3
132-
var bytesRead: Int
132+
var bytesRead = 0
133+
var message: CcidRdrToPcMessage? = null
133134
val buffer = ByteArray(bulkIn.maxPacketSize)
134-
do {
135-
bytesRead = usbDeviceConnection.bulkTransfer(bulkIn, buffer, buffer.size, USB_TIMEOUT)
136-
} while (bytesRead <= 0 && retries-- > 0)
137-
138-
if (bytesRead < HEADER_SIZE) {
139-
throw CcidException("Incorrect header")
140-
}
141-
if (buffer[0] != MESSAGE_TYPE_RDR_TO_PC_DATABLOCK) {
142-
throw CcidException("Unexpected message type")
135+
var lastException: CcidException? = null
136+
while (retries > 0) {
137+
try {
138+
bytesRead = usbDeviceConnection.bulkTransfer(bulkIn, buffer, buffer.size, USB_TIMEOUT)
139+
if (bytesRead <= 0) {
140+
throw CcidException("Failed to read data")
141+
}
142+
if (bytesRead < HEADER_SIZE) {
143+
throw CcidException("Incorrect header")
144+
}
145+
if (buffer[0] != MESSAGE_TYPE_RDR_TO_PC_DATABLOCK) {
146+
throw CcidException("Unexpected message type")
147+
}
148+
message = CcidRdrToPcMessage.parseHeader(buffer)
149+
if (message.seq != expectedSeq) {
150+
throw CcidException("Unexpected sequence number ${message.seq}, expected $expectedSeq")
151+
}
152+
lastException = null
153+
break
154+
} catch (e: CcidException) {
155+
lastException = e
156+
retries--
157+
if (retries > 0) {
158+
Thread.sleep(100)
159+
}
160+
}
143161
}
144-
val message = CcidRdrToPcMessage.parseHeader(buffer)
145-
if (message.seq != expectedSeq) {
146-
throw CcidException("Unexpected sequence number ${message.seq}, expected $expectedSeq")
162+
163+
if (lastException != null) {
164+
throw lastException
147165
}
148166

149-
val dataBuffer = ByteArray(message.length)
167+
val dataBuffer = ByteArray(message!!.length)
150168
var bytesBuffered = bytesRead - HEADER_SIZE
151169
System.arraycopy(buffer, HEADER_SIZE, dataBuffer, 0, bytesBuffered)
152170

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ccid
22
description: "A Flutter plugin for smart card reader using CCID protocol with PC/SC-like APIs."
3-
version: 0.1.7
3+
version: 0.1.8
44
homepage: https://github.com/nfcim/ccid
55

66
environment:

0 commit comments

Comments
 (0)