NfcChipRead
Reads a passport or ID card chip on Android, from start to end: the SDK owns the link to the chip and every way the read can stop; your app owns the screen. This is the main way to read a chip on Android; IsoDepTransport stays the low-level path, where your app owns the link.
The shortest read
Start it with the screen that shows it. The read turns reader mode on for that screen, runs, and turns reader mode off at its ending, on every ending:
val read = NfcChipRead.start(activity, options, observer)
// later, if the person leaves the screen: read.cancel()start with an Activity checks that NFC can be used, turns on reader mode with the SDK's flags and NfcReadSettings.presenceCheckDelayMillis, and ends the read as ChipReadEnding.NotStarted when it cannot: NFC missing or off, the Activity finishing or destroyed (NfcUnavailable.Reason.ACTIVITY_DESTROYED), or reader mode refused. It never turns reader mode off and on again while a document may be on the phone: it is switched on once, here, and off once, at the ending, before the observer hears the ending. Call it on the main thread, with the Activity that shows the read; the observer is called on the main thread by default (NfcReadSettings.observerOn). The SDK cannot ask an Activity whether it is resumed (the platform has no public call for it): the platform keeps the reader-mode request and applies it when the Activity resumes, so a read started from onCreate waits for that.
Reader mode is the read's alone. The platform tells the SDK nothing when reader mode is switched off or taken over: no callback, and no call that tells whether an Activity has it on. If your app calls NfcAdapter.disableReaderMode for the read's Activity, or turns on its own reader mode there, the read does not end: no document can reach it, it keeps reporting io.lightine.tessera.emrtd.ChipReadState.WaitingForDocument, and it stays so until NfcReadSettings.waitForDocumentMillis passes (by default 2 minutes; NfcReadSettings.NO_LIMIT never), the Activity is destroyed, or your app calls cancel. To stop a read, call cancel: it ends the read with ChipReadEnding.Cancelled and turns reader mode off itself.
Configuration change. The read is for one Activity. The platform turns reader mode off and on with that Activity's pause and resume, and a pause in the middle of a read drops the link the way a moved document does (see below). When the Activity is destroyed, for example by a rotation, the read ends at once as ChipReadEnding.EndedBySystem with NfcUnavailable.Reason.ACTIVITY_DESTROYED, because reader mode ended with the Activity and a new Activity cannot take the read over without turning reader mode off and on. That is the safest behaviour to promise: no read is left running without a screen, and the key is cleared. To read through a rotation, keep the read screen out of it (android:configChanges for the screen's orientation and size, or a fixed orientation), or use the lower-level form below. The read holds the Activity weakly, and lets it go at the ending, so a finished read never keeps an Activity alive; do not keep the read in a ViewModel past the screen.
The lower-level form
Your app can run reader mode itself: start the read with an adapter, hand it to NfcReaderMode.enable, show the updates, and act on the one ending.
// In a ViewModel, or anything else that outlives a configuration change:
val read = NfcChipRead.start(options, NfcAdapter.getDefaultAdapter(context), observer)
// In the read screen, once per read, while the screen is shown (onCreate or onResume):
NfcReaderMode.enable(activity, read)?.let { reason ->
// The read has already ended with NotStarted(reason): it cannot get a document.
showWhy(reason) // for example "turn NFC on"
}
// When the person leaves the read screen for good, or in ViewModel.onCleared:
read.cancel()
NfcReaderMode.disable(activity)Your app and the screen
NfcReaderMode.enable replaces any reader mode your app had turned on for the same Activity, as the Activity-owning start does: see NfcReaderMode.
Call NfcReaderMode.enable once for each read. The platform turns reader mode off when the Activity pauses and on again, with the same read, when it resumes: do not call it again in
onResume, and never turn reader mode off while a document may be on the phone. After a configuration change, call it once for the new Activity with the same read.A pause in the middle of a read (a dialog, another app, the screen locking) drops the link the way a moved document does: the read reports io.lightine.tessera.emrtd.ChipReadState.LinkLost and goes on when the screen is back and the document is found again, within NfcReadSettings.waitForReturnMillis. Your app may also cancel in
onPause; the SDK does not watch the Activity.One read at a time in the app: start while another read has not ended ends at once with ChipReadEnding.NotStarted and NfcUnavailable.Reason.READ_IN_PROGRESS. A read waiting for a document waits until NfcReadSettings.waitForDocumentMillis passes, which by default is 2 minutes (NfcReadSettings.NO_LIMIT is never): cancel a read your app no longer shows, or no other read can start. Switching reader mode off yourself (
NfcAdapter.disableReaderMode) does not end the read, and the SDK cannot see it: call cancel.
The key
The read takes the access key in options over from start until its ending: the SDK clears it (io.lightine.tessera.emrtd.ChipAccessKey.clear) when the read ends, whatever the ending is, and not earlier, so a restart after a lost link still has it. You do not clear it, and you do not use it for a second read: a new read needs a new key, which also keeps the authentication attempts with it to one read per action of the person. A read started with a key that was already used (the same options again, for example) ends at once, before any reader or sheet, with ChipReadEnding.KeyRejected whose error is a io.lightine.tessera.types.errors.ChipAccessDenied with reason KEY_CLEARED. The low-level io.lightine.tessera.emrtd.ChipReader.read does not clear the key: there you own it.
What the SDK does
Checks the MRZ key's check digits before anything else: a key that does not add up ends with ChipReadEnding.KeyRejected and nothing is sent (see io.lightine.tessera.emrtd.ChipReadOptions.allowCheckDigitMismatch).
Ends with ChipReadEnding.NotStarted when the device has no NFC (
NOT_SUPPORTED), NFC is off (DISABLED), or NFC is on but its option for reading tags is off (TAG_READING_DISABLED, known from Android 15, API level 35, on).When no document is found within NfcReadSettings.waitForDocumentMillis (2 minutes by default), ends with ChipReadEnding.Failed and an io.lightine.tessera.types.errors.NfcTimeout with limit
WAIT_FOR_DOCUMENT, 0 attempts: the app set that limit, so it is not ChipReadEnding.EndedBySystem.While no document is found, the observer gets io.lightine.tessera.emrtd.ChipReadState.WaitingForDocument again every 2 seconds, with the time waited. A tag that is not an ISO 14443-4 chip (a transit card, for example) is skipped, and the read keeps waiting.
When a document is found, waits NfcReadSettings.settleDelayMillis before the first command, connects, and reads. Each command has its own time limit: NfcReadSettings.commandTimeoutBeforeAccessMillis for the commands before the key is used; from the first command that uses it on (BAC's EXTERNAL AUTHENTICATE, PACE's first GENERAL AUTHENTICATE), NfcReadSettings.commandTimeoutMillis for the commands that make the chip do public-key work and NfcReadSettings.readTimeoutMillis for the others, such as reading a file. The platform's time limit is changed only when the next command needs another. A link that cannot be set up sends the read back to waiting, with no attempt used; after 3 such failures in a row the read ends with ChipReadEnding.Failed and io.lightine.tessera.types.errors.NfcNoAnswer when no try got past the first commands, the same ending as on iPhone.
When the chip does not answer before the key is used (found, but with too little power to answer), reports io.lightine.tessera.emrtd.ChipReadState.NoAnswerYet and tries the same
Tagagain after a short pause, with no restart used, while the person moves the document: about one try every 2 seconds, for at most NfcReadSettings.waitForReturnMillis from the first unanswered try; then the read ends with ChipReadEnding.Failed and, when no try got past those commands, io.lightine.tessera.types.errors.NfcNoAnswer.A
Tagthe platform can no longer connect to is let go at once and never called again: each call on it would put off the platform's own check that the tag is gone, and with it the search for the document. The read then waits for the platform to find the document again, within the same time bound, and reports the state it holds (NoAnswerYetorLinkLost) again every 2 seconds.When the link is lost, follows NfcReadSettings.linkLoss: by default it waits for the document again and goes on, at most twice. The platform hands over a new
Tagwhen the document comes back; when it does not, because the document never left the field, the SDK tries the earlierTagagain every second, until the platform can no longer connect to it. The Activity-owning form does not try while its Activity is paused (it has no reader mode then); the wait for the return keeps counting.When NFC is switched off during the read, ends with ChipReadEnding.EndedBySystem and NfcUnavailable.Reason.DISABLED (or
TAG_READING_DISABLEDfor the option for reading tags), instead of waiting for a document that cannot come: at once when a command or a connect fails, and within 2 seconds while the read waits for a document.A fault the SDK does not expect (an exception from the platform's NFC code with no meaning the SDK knows) ends the read with ChipReadEnding.Failed and NfcUnavailable.Reason.UNEXPECTED_FAULT; it never reaches the app as an exception.
Closes the link on every path, and the observer is never blocked by it: the read starts the close right before it hands the ending to the observer, and the platform's close, which talks to the tag and can take up to the command time limit, runs in the background. The next link, of this read or a later one, waits for that close (at most the command time limit), so the two never race on the tag.
Tags found while a command runs, or after the ending, are ignored: never two reads at once.
Threads
start, cancel and onTagDiscovered return at once, from any thread. The read runs on the I/O dispatcher. The observer's calls never overlap and come in order, with the one ending last, and by default (NfcReadSettings.observerOn is ObserverThread.MAIN) they come on the main thread, handed over through the main looper in the order the SDK made them; none comes before start returns. With ObserverThread.CALLER they come from the read's own thread, or from the thread that calls start or cancel for the updates and the ending those cause: then io.lightine.tessera.emrtd.ChipReadState.Starting, and the ending of a read that cannot start, can come before start returns. One exception to the thread: a read that owns the Activity's reader mode (start(activity, ...)) hands over its ending on the main thread with either value, because it turns reader mode off there first. An exception the observer throws is ignored, so it never reaches the main thread.
The read lets go of the observer when it hands over the ending (with ObserverThread.MAIN, once the main thread has run that call).
Experimental: see ExperimentalTesseraChipApi.
Since
1.0.0
Properties
Functions
Ends the read with ChipReadEnding.Cancelled (by the app), whatever it is doing; it changes nothing once the read has ended. This is the way to stop a read: switching reader mode off behind the SDK's back (NfcAdapter.disableReaderMode, or your own reader mode on the same Activity) cannot be seen by the SDK, so the read goes on waiting for a document that can no longer arrive, until NfcReadSettings.waitForDocumentMillis passes (2 minutes by default), or the Activity of an Activity-owning read is destroyed. In the callback form, also call NfcReaderMode.disable afterwards.
Reader mode found a tag. Returns at once, from the platform's binder thread: the read's own coroutine takes the tag. A tag found while a command runs, or after the ending, is ignored.