Android HCE AID Routing: How Your Phone Picks Which App Answers
When a reader sends SELECT AID, Android picks one HCE service to answer. Here is how the routing table, AID groups, categories and conflict rules decide which.
Introduction
When you tap an Android phone on a reader, exactly one app answers, even if several apps can emulate a card. Android decides using the Application ID (AID) the reader asks for, a routing table in the NFC controller, and a short list of conflict rules. This guide walks through that path for host card emulation, which Android has supported since version 4.4.
What the reader actually sends
The first thing a reader usually sends is a SELECT AID command. It is an APDU carried over ISO-DEP, and it holds an AID of up to 16 bytes that names the application the reader wants. Per the Android HCE guide, Android extracts that AID, resolves it to an HCE service and forwards the APDU to that service.
From then on the service receives every APDU from the reader, until the reader sends another SELECT AID that resolves to a different service, or until the NFC link breaks. In both cases the service's onDeactivated() callback runs.
Two layers: the controller table, then Android
Routing happens twice. First, the NFC controller keeps a routing table of rules, each pairing an AID with a destination: the host CPU or a secure element. When SELECT AID arrives, the controller checks its table. On a match, that APDU and all following ones go to the matching destination.
If the AID isn't in the table, the controller uses its default route. Android devices are required to make sure the AIDs your app registers reach the host. You never build this table yourself. Android configures it from the installed services and the user's preferences. Android also doesn't support the ISO 7816-4 idea of partial AID matches on HCE devices.
Second, once the APDU reaches the host, Android resolves the AID to a service. Apps that keep their card in a secure element declare an off-host service instead. Android never starts or binds to those, because the secure element handles the transaction.
AID groups and categories
A service registers AIDs in groups. For every AID in a group, Android guarantees one of two outcomes: all of them route to your service, or none do. Half a group never goes to another app.
Each group also has a category, either payment or other. Only one payment-category group can be enabled at a time. Closed-loop cards, such as a stored-value card that works at a single merchant, belong in other, where groups can stay active all the time. A payment-category app can handle a tap only if the user made it the default payment app (the Wallet role on Android 15 and later) or it is in the foreground and has called setPreferredService.
Who wins a conflict
Several services can register the same AID. Android resolves it in this order:
- If the user's default wallet app registered the AID, that app is invoked.
- Otherwise, the service that registered the AID is invoked.
- If more than one service registered it, Android asks the user.
A foreground app can override all of that. The CardEmulation reference describes setPreferredService: call it in onResume() and unsetPreferredService in onPause(), because the OS doesn't persist the preference. The call can fail if the user has blocked foreground apps from overriding the default payment service.
AIDs usually come from apduservice.xml in the manifest, written in hex with an even number of characters. Apps can also call registerAidsForService, which replaces the earlier list for that category. Some devices accept an AID prefix such as A000000004*, and supportsAidPrefixRegistration() tells you whether yours does.
When the wrong app answers
If a tap reaches the wrong app, work through this list:
- Is the AID group split? Move the AIDs that belong together into one group.
- Is another app the default wallet? It beats your service for any AID it registered.
- Is your group in
paymentwhen it should beother? - Are you relying on prefix matching on a device that doesn't offer it?
- Did you forget to call
setPreferredServiceagain after the activity resumed?
NFCore reads, writes and inspects NFC tags on iOS and Android. It isn't a tool for tracing HCE routing, so for these checks use a second reader and watch which service logs the SELECT AID.
Conclusion
Android answers a tap in two steps: the NFC controller's routing table sends the AID to the host or a secure element, and Android then picks one service by wallet default, sole registrant or user choice, with a foreground app able to override. Keep related AIDs in one group, pick the right category, and set the preferred service on every resume.
Frequently Asked Questions
How does Android decide which HCE app answers a tap?
The reader sends SELECT AID. Android resolves that AID to a service: the default wallet app if it registered the AID, then the only service that registered it. If several did, it asks the user. A foreground app using setPreferredService overrides this.
Can two apps register the same AID?
Yes. Android allows it and settles the conflict at tap time using the order above. If more than one service registered the AID and none is preferred, the user is asked.
Does Android support AID prefix matching?
ISO 7816-4 partial matches aren't supported on Android HCE devices, but some devices let an app register an AID prefix. Call supportsAidPrefixRegistration() to check.
Can an AID group be split between two apps?
No. Either every AID in the group routes to the service or none does.