1
votes

I have been developing an app which uses NFC Tags to do some magic.

All has been well until recently when I changed some code thats not related to any of the NFC code which previously has been working.

When I launch my application via NFC tap, all works and I will recieve future NFCTag's in onNewIntent() as I tap whilst the application is running.

When I launch my application via icon and attempt to tap while my application is running, my onNewIntent() method is called, but when I try to get the NFCTag as an extra from the intent it returns null.

Am I correct in thinking that even though it is null, I have set the ForegroundDispatch correctly since my onNewIntent() is called?

Heres the code...

protected void onResume() {
    if(this.mNfcAdapter==null) {
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter nfcFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        nfcFilter.addDataType("application/application.myorg.myapp");
    } catch (MalformedMimeTypeException e) {
        Log.e(TAG, "Error Setting FD for NFC", e);
    }
    String[][] mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] {nfcFilter}, mTechLists);
}

protected void onPause() {
    super.onPause();
    mNfcAdapter.disableForegroundDispatch(this);
    Log.d(TAG, "Activity is pausing");
}

protected void onNewIntent(Intent intent) {
    Log.d(TAG, "NFC TAP WHILE ACTIVE");
    Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if(tag!=null) {
       //NEVER CALLED WHEN LAUNCHED VIA ICON (NOT NFC)
       Log.d(TAG, "TAG IS NOT NULL");
    }
}

The MIME Type I set in the IntentFilter is the same I have in my Manifest.

EDIT

My Manifest

<activity
   android:name="org.mypackage.myapp.MainActivity"
   android:label="@string/app_name" >
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />

          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
     <intent-filter>
          <action android:name="android.nfc.action.NDEF_DISCOVERED" />                                       
          <data android:mimeType="application/org.mypackage.myapp" />
          <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
</activity>

What my Tag looks like

+---------------------------------------------------+
| MIME:application/org.mypackage.myapp | StringData |
+---------------------------------------------------+
| EXT:android:com:pkg | org.mypackage.myapp         |
+---------------------------------------------------+
1
Could you also show the relevant part of the manifest (i.e. the activity section for that activity)?Michael Roland
Moreover, what NDEF message does your tag contain?Michael Roland
Hi Michael, updated question :)Stu Whyte

1 Answers

4
votes

The problem is how you retrieve the intent in your onNewIntent() method. Currently you are using getIntent() to get the intent from which you try to retrieve the EXTRA_TAG. Unless you use setIntent(...) to change it (what you obviously don't do in the relevant part of the code), this will return the intent that initially started your activity. The NFC discovery intent is passed to the onNewIntent() method in the parameter Intent intent. So using this should do the trick:

protected void onNewIntent(Intent intent) {
    Log.d(TAG, "NFC TAP WHILE ACTIVE");
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if (tag != null) {
       Log.d(TAG, "TAG IS NOT NULL");
    }
}

Moreover, you might want to check if the intent you received actually has the intent action that you expect (e.g. ACTION_NDEF_DISCOVERED).


One more thing: You can safely set mTechLists to null when you register for NDEF_DISCOVERED intents. The tech list is only used for TECH_DISCOVERED intent filters.