NfcApduRouter

A router which routes incoming NFC CommandApdus by ISO/IEC 7816-4 Application ID (AID) to multiple NfcApduService instances.

When a SELECT APPLICATION command APDU (INS = 0xA4, P1 = 0x04) is received, NfcApduRouter inspects the AID in the command payload and routes that command and all subsequent commands (as well as deactivate events) to the matching NfcApduService returned by buildServices. If no service is registered for the selected AID, 6A82 (Nfc.RESPONSE_STATUS_ERROR_FILE_OR_APPLICATION_NOT_FOUND) is returned.

Usage

Subclass NfcApduRouter and override buildServices to map each supported AID to its corresponding NfcApduService:

class TestAppNfcApduRouter : NfcApduRouter() {
override fun buildServices(): Map<ByteString, NfcApduService> {
return mapOf(
Nfc.NDEF_APPLICATION_ID to TestAppMdocNdefService(),
Nfc.ISO_MDOC_NFC_CCE_APPLICATION_ID to TestAppMdocNfcCceService(),
Nfc.ISO_MDOC_NFC_DATA_TRANSFER_APPLICATION_ID to MdocNfcDataTransferService()
)
}
}

On Android, NfcApduRouter extends android.nfc.cardemulation.HostApduService (via NfcApduRouterBase) so that a single OS HostApduService can handle multiple AIDs (which is required because CardEmulation.setPreferredService() only supports a single service per foreground activity). Register the subclass in AndroidManifest.xml:

<service
android:name=".TestAppNfcApduRouter"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/nfc_apdu_router" />
</service>

And declare all routed AIDs in the corresponding @xml/nfc_apdu_router resource:

<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:requireDeviceUnlock="false"
android:requireDeviceScreenOn="false">
<aid-group android:category="other">
<aid-filter android:name="D2760000850101" />
<aid-filter android:name="A0000002480401" />
<aid-filter android:name="A0000002480400" />
</aid-group>
</host-apdu-service>

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
protected lateinit var services: Map<ByteString, NfcApduService>

Functions

Link copied to clipboard
abstract fun buildServices(): Map<ByteString, NfcApduService>

Returns a service mapping from AID to NfcApduService.

Link copied to clipboard
open override fun deactivate(reason: NfcDeactivationReason)

Called when the NFC link has been deactivated or deselected.

Link copied to clipboard
open override fun processCommandApdu(commandApdu: CommandApdu): ResponseApdu?

Called when a CommandApdu has been received from a remote device.

Link copied to clipboard
expect open fun sendResponseApdu(responseApdu: ResponseApdu)

Sends a ResponseApdu back to the remote device.

Link copied to clipboard
open override fun start()

Starts the router and all registered NfcApduService handlers.

Link copied to clipboard
open override fun stop()

Stops the router and all registered NfcApduService handlers.