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 |
+---------------------------------------------------+