I am trying to build a HCE NFC Android Application for payment using AID Visa.
When launching, aid seem not to be recognized at manifest and its ressource file declaration.
Indeed, OS ApduServiceInfo service throws a "Not adding <aid-group> with empty or invalid AIDs" message
My Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-feature android:name="android.hardware.nfc" android:required="true"/> <uses-feature android:name="android.hardware.nfc.hce" android:required="true"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.NFC"/> <application android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:theme="@style/Theme.MyHCE" android:roundIcon="@mipmap/ic_launcher_round" android:fullBackupContent="@xml/backup_rules" android:supportsRtl="true" tools:targetApi="31" android:dataExtractionRules="@xml/data_extraction_rules"> <activity android:name=".views.MainActivity" android:exported="true" android:label="@string/app_name" android:launchMode="singleInstance" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".services.MyHostApduService" android:exported="true" android:permission="android.permission.BIND_NFC_SERVICE"> <intent-filter> <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/ <category android:name="android.intent.category.DEFAULT"/> </intent-filter> <meta-data android:name="android.nfc.cardemulation.host_apdu_service" android:resource="@xml/apduservice" /> </service>
My host-apdu xlm ressource file:
<?xml version="1.0" encoding="utf-8"?> <host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android" android:description="@string/hce_host_apdu_service_description" android:requireDeviceUnlock="false" android:apduServiceBanner="@raw/my_hce_banner"> <aid-group android:description="@string/aid_group_description_payment" android:category="payment"\> <aid-filter android:name="325041592E5359532E4444463031" android:description="@string/aid_description_ppse"/> <aid-filter android:name="315041592E5359532E44444630" android:description="@string/aid_description_pse"/> <aid-filter android:name="A0000000031010" android:description="@string/aid_description_visa"/> </aid-group\> </host-apdu-service>
My Host Adpu Service Java code:
public class MyHostApduService extends HostApduService { @Override public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) { vibrate(); // personal function that makes the device vibrate logMessage("APDU command detected"); // function that logs a tagged message Toast.makeText(getApplicationContext(), "APDU detected", Toast.LENGTH_SHORT); // to be developped... return new byte[0]; } @Override public void onDeactivated(int reason) { logMessage("desactivation"); Toast.makeText(getApplicationContext(), "desactivation", Toast.LENGTH_SHORT); } }
Here are depencencies declared to gradle/app
dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'}
OS recognizes the app as I can selected it as my prefered payment app from NFC settings menu But when launching, AID seem not to be taken as if I haven't declare them at the manifest and its ressource file.
Of course, APDU commands are not routed to my app when I use it on a POS device... :-(
I have deeply read Android NFC HCE documentation https://developer.android.com/guide/topics/connectivity/nfc/hce but cannot fix the problem
I runs the app on API 29 to 33 (Android 10 to 13) on Samsung S9 and A54. Maybye the android documentation is not up to date and some security features have been introduced since and require some more declaration ?
Someone could help ?
It seems that your "host-apdu xls ressource file" is missing a data field. Usually a Credit Card reader is not asking for an AID but tries to read the "directory" by selecting "PPSE 2PAY.SYS.DDF01" and this is the starting point for the communication. This string is the general filter you need to append to your xls file (the data behind name is just the hex encoded data of the string):
Below you find my xls file a project on my own and it is much than you need it as I tested my app for Visa-, Master- and American Express card: