Question

I have a class, which creates conncection to NFC and two activites. Both of them creates an object of that class so they can connect to NFC. Earlier it worked somehow but now I've got problem - my application doesn't do anything onNewIntent, even on the first activity. Instead of it, I can see "New tag collected" from build-in app called "Tags" (Nexus S).

What should I do?

class:

public NFCForegroundUtil(Activity activity)
{
    super();
    this.activity = activity;
    mAdapter = NfcAdapter.getDefaultAdapter(activity
            .getApplicationContext());

    mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(
            activity, activity.getClass())
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndef2 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter ndef3 = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

    try
    {
        ndef2.addDataType("*/*");
    }
    catch (MalformedMimeTypeException e)
    {
        throw new RuntimeException("fail", e);
    }

    mFilters = new IntentFilter[] {ndef, ndef2, ndef3 };

    mTechLists = new String[][] { new String[] {
            // android.nfc.tech.NfcV.class.getName(),
            android.nfc.tech.NfcA.class.getName(),
            android.nfc.tech.IsoDep.class.getName() } };

    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

}

activity 1:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nfcForegroundUtil = new NFCForegroundUtil(this);

}

 @Override
protected void onNewIntent(Intent intent)
{

    super.onNewIntent(intent);
    Intent i = new Intent(this, NfcDisplayLabelActivity2.class);
    startActivity(i);

 }
Was it helpful?

Solution 5

I was seeing "New tag collected" from build-in app called "Tags" because my application didn't work properly.

When it works ok, it has higher priority than "Tags" and phone reads tags from my application. But when it works unproperly and phone collect a tag, "Tags" application is activated and "Tags" application talks to my device.

After repairing code, my app has higher priority and phone reads tags using my application.

OTHER TIPS

Go to settings -> Apps -> All -> Tags(in my case) -> disable

I had a similar problem when trying to open my app from an NFC tag. I had registered an intentfilter in my AndroidManifest.xml for the scheme "magicnfc" and yet it opened the Android OS Tags app instead of mine.

I discovered that the NFC intent (TECH_DISCOVERED in my case) had higher priority than a generic scheme-based intent filter. Because the Tags app registered TECH_DISCOVERED, it was getting opened instead of mine.

Luckily, apps can register for NDEF_DISCOVERED (a higher priority filter) and get opened instead of the Tags app.

That made my app open when I tapped the tag.

More info is here: http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

But I found that I had to override the function onNewIntent, with code like this:

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
    String uri = intent.getDataString();

    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    if (rawMsgs != null) {
        msgs = new NdefMessage[rawMsgs.length];
        for (int i = 0; i < rawMsgs.length; i++) {
            msgs[i] = (NdefMessage) rawMsgs[i];
        }
    }
}

For me, all I needed was:

String uri = intent.getDataString();

Good luck!

You can listen for all tags activated using the ACTION_TAG_DISCOVERED intent, rather than filtering for a specific one with the following code:

public NFCForegroundUtil(Activity activity)
{
    super();
    this.activity = activity;
    mAdapter = NfcAdapter.getDefaultAdapter(activity
            .getApplicationContext());

    mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(
            activity, activity.getClass())
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 
0);

    // See below
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}

From the NFCAdapter Documentation :

If you pass null for both the filters and techLists parameters that acts a wild card and will cause the foreground activity to receive all tags via the ACTION_TAG_DISCOVERED intent.

Your problem is when you initialise the intent i onNewIntent

The class should be itself, not the second class.

The right code should be :

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    Intent i = new Intent(this, NfcDisplayLabelActivity1.class);
    startActivity(i);
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top