1

I have been detecting NFC enabled card in my app to read tag value. Following is my code

OnCreate()

 nfcAdapter = NfcAdapter.getDefaultAdapter(AuthDriverCard.this);
    piTap = PendingIntent.getActivity(
            this, TAP_REQUEST_CODE, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    nfcIntentFilter = new IntentFilter[]{techDetected, tagDetected, ndefDetected};

OnResume

 if (nfcAdapter != null) {
        nfcAdapter.enableForegroundDispatch(AuthDriverCard.this, piTap, nfcIntentFilter, null);
    }

OnPause

 if (nfcAdapter != null) {
        nfcAdapter.enableForegroundDispatch(AuthDriverCard.this, piTap, nfcIntentFilter, null);
    }

OnNewIntent()

  protected void onNewIntent(Intent intent) {
    // my logic here
   }

This code works fine almost every time. But some time card is not detecting and i found that OnNewIntent() is not firing.

What could be the problem. Do i need to set intent filter in manifest also ? I haven't set it in manifest so far but it worked for me without any problem. Is there any problem in my java code ?

Please suggest

NOTE - Restart(after killing app) app solve the problem. card detection works after that.

Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83

1 Answers1

0

You need to add the following in intent-filter section of Manifest file, if you haven't added already:

<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>

Eg:

<application
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activities.LauncherActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>

            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
Adarsh Rotte
  • 634
  • 3
  • 8
  • where to add this. I have already added this filters in `nfcAdapter.enableForegroundDispatch()` function call – Rajeev Kumar Jun 19 '20 at 10:04
  • @RajeevKumar I've updated the answer with more details. – Adarsh Rotte Jun 19 '20 at 10:44
  • 1
    but i m using foreground dispatch method. I am not opening any pre-desired activity on tag detection. I hv already a activity in foreground and need to show data on this activity if card detects through foreground dispatch channel. Please go through my question and content over there. My question in `Foreground Dispatch` not working sometime. I need to restart app to make it work again. why ? – Rajeev Kumar Jun 19 '20 at 11:07