Why is my HCE app not getting to my HCEservice?

2.1k views Asked by At

I'm trying to send a nine digit number to an NFC Reader, but my service doesn't seem to be starting. Any help would be greatly appreciated. The phone I'm testing with is a Samsung Galaxy S3 and I'm using Eclipse.

Here's the APDU command my NFC Reader sends.

F0A40400100F01020304

Here's the response I get, which means (I think) file not found.

6A 82

Here's how the APDU command is built.

private byte CLS = (byte)0x00;
private byte SELECT_INS = (byte)0xA4;
private byte P1 = (byte)0x04;
private byte P2 = (byte)0x00;
private byte[] aid = {0x0F, 0x01, 0x02, 0x03, 0x04};
private Long TIMEOUT = 1000L;
private byte OK = (byte)0x90;

Here's my AID.

F0010203040506

The issue is that it's not even reaching my HCEservice, so my processCommandApdu never gets called. My log cat shows this.

11-12 15:56:50.175: D/NativeNfcManager(1064): doReceiveData. reutrn..
11-12 15:56:50.175: D/HostEmulationManager(1064): notifyHostEmulationData
11-12 15:56:50.175: D/HostEmulationManager(1064): call findSelectAid - 1
11-12 15:56:50.175: D/NativeNfcManager(1064): mAppletSelectStatus=3
11-12 15:56:50.185: D/NativeNfcManager(1064): Waiting for an APDU...
11-12 15:56:50.195: D/NativeNfcManager(1064): doReceiveData. reutrn..
11-12 15:56:50.195: D/HostEmulationManager(1064): notifyHostEmulationData
11-12 15:56:50.195: D/HostEmulationManager(1064): call findSelectAid - 1
11-12 15:56:50.195: D/HostEmulationManager(1064): Dropping non-select APDU in          STATE_W4_SELECT
11-12 15:56:50.195: D/NativeNfcManager(1064): mAppletSelectStatus=3
11-12 15:56:50.205: D/NativeNfcManager(1064): Waiting for an APDU...
11-12 15:56:50.205: D/dalvikvm(10513): GC_CONCURRENT freed 994K, 53% free 8422K/17772K, paused 2ms+6ms, total 48ms
11-12 15:56:50.205: D/dalvikvm(10513): WAIT_FOR_CONCURRENT_GC blocked 39ms
11-12 15:56:50.215: W/SAMMLibraryCore(10513): Not AMS File(Invalid AMS End Marker)
11-12 15:56:50.215: W/SAMMLibrary(10513): Error on load SAMM File Info
11-12 15:56:50.225: D/NativeNfcManager(1064): doReceiveData. reutrn..
11-12 15:56:50.225: D/HostEmulationManager(1064): notifyHostEmulationData
11-12 15:56:50.225: D/HostEmulationManager(1064): call findSelectAid - 1
11-12 15:56:50.225: D/NativeNfcManager(1064): mAppletSelectStatus=3
11-12 15:56:50.225: W/SAMMLibraryCore(10513): Not AMS File(Invalid AMS End Marker)
11-12 15:56:50.225: W/SAMMLibrary(10513): Error on load SAMM File Info
11-12 15:56:50.225: D/NativeNfcManager(1064): Waiting for an APDU...

Here's the service I declare in my AndroidManifest.

 <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 lastly, here's my service is res/xml (a folder I created).

<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/servicedesc"
android:requireDeviceUnlock="false" >

<aid-group
    android:category="other"
    android:description="@string/aiddescription" >
    <aid-filter android:name="F0010203040506" />       
</aid-group>

</host-apdu-service>
1

There are 1 answers

1
Michael Roland On BEST ANSWER

In your host-apdu-service, you registered for the AID

F0010203040506

In order to communicate with your HCE service, the reader has to issue a SELECT (by AID/DF name) command for that AID. A valid SELECT command could look like this:

00 A4 0400 07 F0010203040506

The command that you (possibly?) build with the code

private byte CLS = (byte)0x00;
private byte SELECT_INS = (byte)0xA4;
private byte P1 = (byte)0x04;
private byte P2 = (byte)0x00;
private byte[] aid = {0x0F, 0x01, 0x02, 0x03, 0x04};

does not match the AID that you registered for. Instead this is data could be used to build a SELECT command for the AID 0F01020304 (that's not a properly formed AID btw.)

Even worse, the APDU command that you show above

F0A40400100F01020304

is neither a SELECT command nor a valid APDU command.

For selecting an Andorid HCE app, the CLA byte must be 0x00 (but is 0xF0 in your case). The Lc field is 0x10 (16 bytes) but the data field only contains 5 bytes.