Use my application for NFC payment when not set as default
Asked Answered
B

1

13

Hi I'm working on android application for NFC payments.

There is an option in Android settings to use an open application instead of the default one. For example when I have default application set to Android Pay and I open my app before the payment - I want to use my app for the payment instead of the default one. See image bellow.

enter image description here

I tested it, but unfortunately I paid with Android Pay instead of my app that was running in foreground.

I did not find any word in documentation, if I have to add something to manifest, register something etc..?

I have service with intent filter and meta data in my manifest and when the app is set as default for payment, it is working correctly:

<service
        android:name="com.example.MyWalletHceService"
        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>

Adding apduservice xml content:

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

<aid-group
    android:category="payment"
    android:description="@string/nfc_aid_desc">
    <!-- Following is a PPSE AID. You must always include this AID in order for payments to
         work-->
    <aid-filter android:name="@string/aid_number"/>
    <!-- Following AID list is specific to the application requirements.
       If your application supports the type of card that these AID represents,
       you must include those AID in your configuration file -->
    <aid-filter android:name="@string/aid_mc_filter"/> <!-- Mastercard DEBIT/CREDIT -->
</aid-group>

Bruch answered 24/1, 2018 at 11:31 Comment(5)
What's the contents of the apduservice XML file?Belligerency
@MichaelRoland I updated my question with apdu XML content.Bruch
All those string definitions exist, right? What's the value of those aid_*strings? And, your app does show up as a choice for setting the default payment app in the Tap & pay settings, right?Belligerency
@Sandak Is there documentation to integrate with the contactless NFC payment feature?Linen
@StepanSanda, could you share any simple guide/wiki for a basic NFC payments? I'm looking to develop a "wallet" for NFC payment and can't find simple naive examples.Edmea
B
9

So I finally found a solution!

You have to use setPreferredService(Activity, ComponentName) in your current foreground activity. It is recommended to to call it inside onResume() and also do not forget to call unsetPreferredService(Activity) once the activity is not in foreground (onPause() is good place to call it).

One more thing: setPreferredService(Activity, ComponentName) and unsetPreferredService(Activity) were added in API 21, so be sure not to call them from older API.

Bruch answered 6/2, 2018 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.