"New Tag Collected" instead of reading tags of application - NFC android
Asked Answered
S

5

7

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);

 }
Secularism answered 27/9, 2012 at 12:0 Comment(0)
S
-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.

Secularism answered 15/11, 2012 at 11:50 Comment(2)
If you are still having problems, check out my answer.Unweighed
There really isn't any useful data in this answer. Simply stating "oh, I corrected my app and now it works" doesn't tell the community anything about how the app was corrected and what you were initially doing incorrectly.Rendering
S
7

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

Spoils answered 26/5, 2014 at 11:7 Comment(1)
This is not the solution. He should filter the proper tags in his tech-lists, or use adapter.enableForegroundDispatch(this, pendingIntent, null, null); as shown in a good answer below when the app is foregroundedAstro
U
5

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!

Unweighed answered 19/8, 2014 at 21:57 Comment(0)
R
3

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.

Rendering answered 16/5, 2015 at 17:23 Comment(0)
P
0

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);
 }
Pimental answered 10/12, 2020 at 16:32 Comment(0)
S
-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.

Secularism answered 15/11, 2012 at 11:50 Comment(2)
If you are still having problems, check out my answer.Unweighed
There really isn't any useful data in this answer. Simply stating "oh, I corrected my app and now it works" doesn't tell the community anything about how the app was corrected and what you were initially doing incorrectly.Rendering

© 2022 - 2024 — McMap. All rights reserved.