Skip to content

Commit 65c2d25

Browse files
committed
Fix NTAG213/215 misdetection as 64-byte MIFARE Ultralight on ACR1552
GET_VERSION failures and out-of-range page reads corrupt ACR1552 reader state, causing subsequent commands to return "card removed" errors. Add card.Reconnect() before each memory probe attempt to reset reader state, allowing NTAG213 (page 16) and NTAG215 (page 45) detection to succeed after failed higher page probes.
1 parent 090567e commit 65c2d25

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

internal/core/card.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,20 @@ func detectCardType(card *scard.Card, cardInfo *Card) (confident bool) {
516516
if contains(atr, "03060300") && !ccDetectionFoundNDEF && !getVersionSucceeded {
517517
logging.Debug(logging.CatCard, "Starting memory probe detection", nil)
518518

519+
// Helper to reset reader state before each probe.
520+
// GET_VERSION failures and failed out-of-range page reads on some readers
521+
// (e.g., ACR1552) can leave the reader in a corrupted state where subsequent
522+
// commands return "card removed" errors. Reconnecting clears this state.
523+
reconnect := func() {
524+
if err := card.Reconnect(scard.ShareShared, scard.ProtocolAny, scard.ResetCard); err != nil {
525+
logging.Debug(logging.CatCard, "Reader reconnect failed", map[string]any{
526+
"error": err.Error(),
527+
})
528+
}
529+
}
530+
519531
// Try page 135 (only NTAG216 has this - it has 231 pages)
532+
reconnect()
520533
if _, err := readNTAGPage(card, 135); err == nil {
521534
logging.Debug(logging.CatCard, "Memory probe: page 135 readable, detected NTAG216", nil)
522535
cardInfo.Type = "NTAG216"
@@ -526,6 +539,7 @@ func detectCardType(card *scard.Card, cardInfo *Card) (confident bool) {
526539
}
527540

528541
// Try page 45 (NTAG215 has 135 pages, NTAG213 has 45, Ultralight has 16)
542+
reconnect()
529543
if _, err := readNTAGPage(card, 45); err == nil {
530544
logging.Debug(logging.CatCard, "Memory probe: page 45 readable, detected NTAG215", nil)
531545
cardInfo.Type = "NTAG215"
@@ -535,6 +549,7 @@ func detectCardType(card *scard.Card, cardInfo *Card) (confident bool) {
535549
}
536550

537551
// Try page 16 (NTAG213 has 45 pages, Ultralight only has 16)
552+
reconnect()
538553
if _, err := readNTAGPage(card, 16); err == nil {
539554
logging.Debug(logging.CatCard, "Memory probe: page 16 readable, detected NTAG213", nil)
540555
cardInfo.Type = "NTAG213"

0 commit comments

Comments
 (0)