How to exchange ISO 7816 APDUs
Purpose: send application commands to an ISO-DEP smart card or tag. Platforms: Android, iOS. Status: stable.
Prerequisites: a configured host. On iOS, list every application identifier (AID) you select in
com.apple.developer.nfc.readersession.iso7816.select-identifiers and start a tag session (see
configure the Android manifest and iOS entitlements).
Send one command
SendAsync on the tag view opens a connection, sends one APDU and releases the connection:
if (tag.GetProtocol<INfcIso7816Tag>() is not { } apdu) {
return null; // The tag has no ISO 7816 interface.
}
var select = new NfcApduCommand(0x00, 0xA4, 0x04, 0x00, [.. applicationId], ExpectedResponseLength: 256);
var response = await apdu.SendAsync(select, cancellationToken);
applicationId is your application's AID; the library never preselects one.
Keep the selection for a sequence
Selection and authentication live on the native connection. Retain one connection for a sequence and dispose it when done:
var opened = await apdu.OpenConnectionAsync(cancellationToken);
if (!opened.IsSuccess) { ShowError(opened.Error!); return null; }
await using var connection = opened.Value;
var selected = await connection.SendAsync(select, cancellationToken);
if (!selected.IsSuccess) { ShowError(selected.Error!); return null; }
var answer = await connection.SendAsync(applicationCommand, cancellationToken);
A second open or concurrent command returns Busy; await each command.
Interpret the response
| Result | Meaning |
|---|---|
IsSuccess with Sw1/Sw2 |
The card answered. The status words belong to your protocol: 90 00 is not checked or required by the library. |
61xx or 6Cxx |
Returned unchanged. The library never sends GET RESPONSE or repeats the command with a corrected Le. |
CapacityExceeded |
The encoded command does not fit the native transceive limit. |
Unsupported (iso7816.extended.unsupported) |
An extended-length APDU on a device without extended-length support. |
OutcomeUnknown mutation |
Cancellation or tag loss after the command was sent. MayMutate defaults to true; set it to false only for genuinely read-only commands. |
Short APDUs carry up to 255 data bytes and Le up to 256. Larger values use extended encoding when the device
reports support. Inspect the limits with QueryCapabilitiesAsync before sending large commands.
Related
- Short APDU and extended-length limits:
QueryCapabilitiesAsyncand the contracts. - iOS session and AID configuration: configure the Android manifest and iOS entitlements.
- Why a connection is exclusive: ownership.