I\'m trying to do Host card emulation on an Android device using this example using ACR1281U NFC tag reader.
This is the kind of application I want
The AID is a "name" that you assign to your smartcard application (in the case of HCE: the Android app that emulates the card application). A reader application uses this "name" to address your card (HCE) application with a SELECT (by DF name/AID) APDU command (see ISO/IEC 7816-4). You can use any value you want as long as it conforms to ISO/IEC 7816-4.
In your specific case, the reader example application uses the AID F0010203040506
private static final byte[] AID_ANDROID = { (byte)0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Therefore, to be interoperable with that example, you need to register your HCE service for the AID F0010203040506
.
Typically, you first define a "name" for your HCE app:
<host-apdu-service ...>
<aid-group ...>
<aid-filter android:name="F0010203040506"/>
</aid-group>
</host-apdu-service>
Later, reader applications can use that name to select your HCE app and to then communicate with it (in Java e.g. using Java Smart Card IO):
Card card = ...;
CardChannel c = card.getBasicChannel();
// SELECT by AID (F0010203040506)
ResponseAPDU resp = c.transmit(new CommandAPDU(
0x00, 0xA4, 0x04, 0x00, new byte[] { (byte)0xF0, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06 }));
assert resp.getSW() == 0x9000;
// Send application-specific command (what such a command could look like depends on your application)
resp = c.transmit(new CommandAPDU(
0x80, 0x10, 0x00, 0x00, new byte[] { (byte)0x12, (byte)0x34 }));
This depends on your application scenario.
In your closed-loop scenario, where you are under full control of both the HCE side and the reader side, you can choose an arbitrary (note that there are some rules for AIDs) AID and assign it to your HCE app. You can later use that AID in your reader application to address the HCE app.
In real-world HCE scenarios, you often design your HCE app to interact with an existing reader infrastructure. Consequently, your HCE app will implement some given specification. In that case, such a specification will dictate the AID (or AIDs) that your HCE app needs to use in order to be discoverable by the existing reader infrastructure. An example for such a specification is the EMV specification for contactless payment systems.
Sometimes there is the need that an application is addressable through multiple "names" (AIDs). Reasons could be:
The rules for smartcard application identifiers (AIDs) are defined in ISO/IEC 7816-4. An AID has at least 5 bytes and may consist of up to 16 bytes (see this answer on AID size restrictions). Based on the first 4 bits, AIDs are divided into different groups. The most relevant groups defined in ISO/IEC 7816-4 are:
'A'
: internationally registered AIDs'D'
: nationally registered AIDs'F'
: proprietary AIDs (no registration)For (inter)nationally registered AIDs, the AID is split into two parts, a 5-byte mandatory RID (registered application provider identifier), and an optional PIX (proprietary application identifier extension) of up to 11 bytes.
For proprietary AIDs (F...
), you can use any arbitrary value.
F00000000A0101
work while F0010203040506
did not work?I don't know and you did not provide sufficient information to diagnose this. E.g. where there any messages in adb log when you tried to select F0010203040506
?
Anyways, both AIDs are valid and should work. One possibility could be that you already had another HCE application installed on your device that registered for that AID. In that case, two applications would have listened for the same name which is not possible.