1 year ago

#102915

test-img

G B

Android NFC Host Card Emulation sample

I've read a lot of old threads about this, but I can't make a HCE sample application run on a current Android device (Xiaomi smartphones or Samsung tabiets running Android 11).

The samples I found are all similar, I'm following this tutorial for example: https://medium.com/the-almanac/how-to-build-a-simple-smart-card-emulator-reader-for-android-7975fae4040f

So far I have this in manifest:

<uses-permission android:name=”android.permission.NFC” />

(I also tried with android.permission.BIND_NFC_SERVICE but it doesn't make any difference)

The service definition is:

<service
android:name=".HostCardEmulatorService"
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>

The apduservice.xml:

<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="F000109944"/>
</aid-group>
</host-apdu-service>

And the service implementation:

class HostCardEmulatorService: HostApduService() {
override fun onDeactivated(reason: Int) {
    TODO("not implemented") //To change body of created 
    // functions use File | Settings | File Templates.
}

override fun processCommandApdu(commandApdu: ByteArray?, 
                                extras: Bundle?): ByteArray {
    TODO("not implemented") //To change body of created 
    // functions use File | Settings | File Templates.
}
}

On the other side I have a custom NFC card reader, which is trying to select the specified AID on the emulated card. The phone / tablet is recognized as a card, but the response is not coming from the application. I tried to return 90 00 from the processCommandApdu method, but the reader gets other errors, ranging from 0x6999 to 0x6f00

I suppose the card service is not running (I wrote a constructor and I can see that it is never called), and not registered to handle NFC request from my reader, but I don't know how to activate it. Do I have to get NFC or BIND_NFC_SERVICE permissions when starting the main activity of the app? I tried to, but NFC is already granted and requesting BIND_NFC_SERVICE has no effect.

Also, if I run a NFC reader app on the phone and the card emulation on the tablet, another app is trying to handle the reader request, and comes into the foreground.

android

nfc

hce

0 Answers

Your Answer

Accepted video resources