1

I want to display to the user the ID of the public transit card (IsoDep, NfcB), what I managed to do successfully with the following guide.

Now I'm trying to emulate the card with Host Card Emulation with the following guide.

I made a new class named MyHosApduService.java with the following code:

public class MyHostApduService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
       ...
    }
    @Override
    public void onDeactivated(int reason) {
       ...
    }
}   

Declared the service at the AndroidManifest.xml with the following code:

<service android:name=".MyHostApduService" 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/apduservice"/>
        </service>

And apduservice.xml with the following code:

<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/servicedesc"
    android:requireDeviceUnlock="false">
    <aid-group android:description="@string/aiddescription"
        android:category="other">
        <aid-filter android:name="0x00DDA611"/>
    </aid-group>
</host-apdu-service>

From here, how do I send the correct data to the service and trigger the service correctly?

Michael Roland
  • 36,432
  • 10
  • 81
  • 168

1 Answers1

0

The AID that you chose and registered in the AID filter is not valid. The AID value must consist of hexadecimal digits without a "0x" prefix. More over it must consist of at least 5 bytes and up to 16 bytes. Valid formats for AIDs are defined in ISO/IEC 7816-4. See this answer for further details.

Note that the ID (anticollision identifier / tag identifier / UID / etc.) that you get through

byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

or

byte[] id = tag.getId();

is not a smartcard application identifier (AID). Android HCE does not allow you to choose that ID. Instead, you can only emulate smartcard applications that are identified by an AID. This means that the smartcard reader would search for a specific HCE application on your phone by issuing a SELECT command matching the name (i.e. the AID) of your application.

Therefore, you first need to chose a valid AID for your application, e.g. F0112233445566. On the reader side, you can then send a SELECT APDU for addressing your application

00 A4 04 00  07  F0112233445566  00

You might also want to read

Michael Roland
  • 36,432
  • 10
  • 81
  • 168
  • Thanks for the fast response! From [one of the answers you have attached](https://stackoverflow.com/a/27997843/9035154) I found out the following: In real-world HCE scenarios(my case), you often design your HCE app to interact with an existing reader infrastructure. ..... 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. .... From what I understood, I can use the AID of the card I am trying to emulate? If so is it the same as [Application Data](https://imgur.com/a/0zRn4)? – David Kuramshin Jan 28 '18 at 17:00
  • No, the AID is not the same as the application data (or any other field shown in the screenshot). The screenshot doesn't seem to reveal any useful information that would give any clue about the actual application specification. Also note that you will typically need a lot more than the AID in order to emulate the smartcard application (such as knowledge about the application protocol and e.g. secret keys). – Michael Roland Jan 28 '18 at 23:41
  • So can you briefly explain what I have to do in order my app to work? – David Kuramshin Feb 09 '18 at 14:26
  • There's a lot more information necessary than you provided above to estimate what's needed to get your app to "work". – Michael Roland Feb 09 '18 at 14:50
  • Will a detailed explanation of what planned help? – David Kuramshin Feb 10 '18 at 18:58