1

So right now I'm building a simple app to emulate a smartcard using Android HCE (Host-based Card Emulation). The app only returns {90,00} byte array for every APDU command it receives. Here is the code:

public class MyHostApduService extends HostApduService {

    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        byte[] response = new byte[2];
        response[0] = (byte)0x90;
        response[1] = 0x00;
        return response;
    }

    //Rest of the code...
}

And then I tried to connect my smartphone (I'm using Sony Xperia M2) to a smartcard reader (ACR122U-A9) using CardTerminal.connect() method from javax.smartcardio.CardTerminal like this

Card card = terminal.connect("*");

It worked for a real smart card, but it doesn't want to connect my phone. There is a beep sound, but the LED turned off (it doesn't turned green like when it detects a real smartcard), and when I remove the smartphone, I got CardException, and the LED light goes back to red. Sometimes the reader looks like connected to the phone (the LED turned green), but the phone doesn't seem to receive the APDU. I also tried to connect using scScriptor.exe from springcard, with same result.

Is there something I miss on the code? Or probably a technical issue?

EDIT: I installed the apk file on my friend's galaxy s iii, and it's working, so I suspect that this is a phone problem

user1749623
  • 63
  • 1
  • 2
  • 7

1 Answers1

1

If i understood this right and your problem is on the Android Client, you might have missed to define the right AIDs for your application in the Manifest and in the xml-file (see also https://developer.android.com/guide/topics/connectivity/nfc/hce.html) You might also just have the wrong AIDs registered - log your apdu-commands to see which aid the reader might go for.

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>

apduservice.xml (fill in your AIDs in aid-filter tag):

 <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="F0010203040506"/>
        <aid-filter android:name="F0394148148100"/>
    </aid-group>
</host-apdu-service>
phil
  • 825
  • 9
  • 13
  • Well, I already add that xml files, and follow every step and requirement on android developer page about hce, and still not working. – user1749623 Sep 18 '14 at 01:13
  • Permissions all setted right? Can you ensure that the terminal is sending a command? Tryed Logging? If you have an Android 4.4 phone with NFC enabled and all permissions set, this code should work fine. I'm using a Nexus 5 – phil Sep 24 '14 at 11:50