IsoDepTransport
An ApduTransport over an Android IsoDep tag (ISO 14443-4, the layer passport chips use).
It carries bytes only: it does not change, log or keep them. Every blocking IsoDep call runs on Dispatchers.IO, close included, so it is safe to use from the main thread.
Setting up the reader (the app's part)
This library declares only the NFC permission. The app declares <uses-feature android:name="android.hardware.nfc" /> (required or optional is its choice). Then, each with its source:
Turn on reader mode with
NfcAdapter.enableReaderModeand the flagsFLAG_READER_NFC_A or FLAG_READER_NFC_B or FLAG_READER_SKIP_NDEF_CHECK. A passport chip speaks ISO-DEP over NFC-A or NFC-B. Without the last flag the platform runs its own NDEF check on the card before the app gets it, sending commands the SDK did not ask for (the platform documentation ofFLAG_READER_SKIP_NDEF_CHECK).Consider
FLAG_READER_NO_PLATFORM_SOUNDS, so the app gives its own feedback when the read ends instead of the platform's sound when the card is found (the platform documentation).Consider the extra
EXTRA_READER_PRESENCE_CHECK_DELAY. By default the platform checks every 125 ms that the tag is still there, pausing the check only during an exchange (the platform's NFC service source). Some chips drop the link under these checks, and a longer delay is a known remedy: other readers use values from about 500 to 2,000 ms. This is a judgement for the app to test with its own documents, not a rule.Keep reader mode on until the transport is closed. Turning it off, or the activity pausing (which turns it off), can make the platform refuse every later command (the platform's NFC service source).
Use the fresh
Tagof each discovery and pass it to open. ATagfrom an earlier discovery is out of date, and the platform refuses it.Do not rely on
NfcAdapter.ignoreto keep a card from being found again: the platform documentation says it does not work reliably with tags that have a random ID, which passport chips use.Close the transport with close on every path: success, error and cancellation.
After a result that says the key was wrong, do not read again with the same key: ask the person to check it. The SDK only uses the MRZ or CAN key the app gives it, sends no write command, and never retries an authentication by itself.
Errors
A failed exchange throws ApduTransportException. Android gives no failure code: it throws an exception type, and the SDK reports what that type says and nothing more.
NfcTagLost when Android throws
TagLostException. Android throws it both when the tag left the field and when the chip did not answer within the time limit (the platform's NFC service source marks a response timeout as a lost target), so the SDK cannot tell these apart, and it never reports NfcTimeout. The exception'scausesays how long the failing call took and the time limit, so whoever reads it can tell "about the whole limit" from "at once"; the platform's own exception is the cause of that cause.NfcTagLost too when Android throws
SecurityException, which it documents as "the tag object is reused after the tag has left the field", and for any otherIOExceptionorIllegalStateException, for which Android gives no reason the SDK can rely on.NfcUnavailable with reason
BUSYwhen Android refuses to connect because another technology of the same tag is connected.NfcUnavailable with reason
NOT_SUPPORTEDwhen Android cannot connect to ISO-DEP on this tag (UnsupportedOperationException).NfcUnavailable with reason
REQUEST_REFUSEDwhen Android refuses the time limit (IllegalArgumentExceptionfromsetTimeout).NfcUnavailable with reason
COMMAND_TOO_LONGwhen Android refuses a command as longer than its limit ("Transceive length exceeds supported maximum"), the same reason as on iPhone.NfcUnavailable with reason
CLOSEDwhen the transport was closed with close.
After the first failure the transport is done: every later transceive throws the same error again without sending anything to the tag. To read again, close it and open a new one on the next discovered Tag.
Commands go one at a time; calls to transceive are serialized. Cancelling the coroutine does not interrupt a call already blocked in the platform, and neither does close: in the platform the exchange and the close wait for each other, so the blocked call ends when the chip answers or the time limit passes. Cancelling a read can take up to the time limit. No second command is sent while the first is blocked.
Interruptions
The document moves away: the blocked call fails with NfcTagLost and the transport is done.
Your app cancels the coroutine while a command is out: the call still ends in the platform, its answer is dropped and the transport closes itself, because the chip's secure messaging state has moved on. The cancellation is thrown; every later transceive throws NfcUnavailable
CLOSED.Your app calls close from another thread while a command is out: the blocked call ends as above and throws NfcUnavailable
CLOSED; its answer is dropped.The activity pauses or reader mode is turned off mid-read: the SDK cannot see the activity. The platform then refuses the next command, and the transport fails fast with that error and stays done. In
onPause, cancel the read and calldisableReaderMode; the read'sfinallycloses the transport. Start a new read only with theTagof a new discovery afteronResumeturns reader mode on again.A second card enters the field: reader mode reports it as a new
Tag. Ignore it while a read runs; one transport, oneTag, one read at a time.
A typical read, which closes the transport on every path:
override fun onTagDiscovered(tag: Tag) {
readJob = scope.launch {
val result = IsoDepTransport.open(tag).use { transport -> ChipReader().read(transport, options) }
// show result
}
}
override fun onPause() {
super.onPause()
readJob?.cancel()
NfcAdapter.getDefaultAdapter(this)?.disableReaderMode(this)
}Experimental: see ExperimentalTesseraChipApi.
Since
1.0.0
Constructors
Wraps isoDep, which may be connected or not. The first transceive connects it if needed and sets the time limit, once; the transport never connects again.
Functions
Closes the tag connection. After this, transceive throws NfcUnavailable with reason CLOSED, a command blocked in the platform throws it too when it returns, and nothing more is sent. Safe to call more than once and from any thread, the main thread included: it returns at once and hands the platform's close, which talks to the tag and can wait for a blocked exchange up to the time limit, to the I/O dispatcher. When the platform's connect failed, nothing is connected and the platform is not called: every call on a tag the platform has lost postpones the platform's own check that the tag is gone.