İsmet Alkan
İsmet Alkan

Reputation: 5447

Android NFC Activity onResume onNewIntent

I developed a NFC Tag reader/writer by making some changes on this example.

I have "type X" and "type Y" cards. Type X cards are Ndef read/writeable, one is Mifare Classic, one is Mifare Ultralight. Type Y cards are Ndef Formatable, one of them is protected(bus card), and the other one is not.

I get three sounds from the device (Galaxy S3), one of them is the "beautiful-sound", saying "It's easy to write and read from this card, allright". The other one is the "good-sound", saying "This card is tough, but I got some information for you.". The last one is the "ugly-sound", saying "I see you're showing me an NFC Tag, but I don't care."

When I got the application on the screen and running, I got beautiful-sound with Type X cards, can read/write and I got good-sound with Type Y cards and I can at least get their tech info.

However, When I got the application running on background and I'm at the main menu of the device, I got beautiful sound with Type X cards and everything works great and when I tap Type Y cards I get ugly-sound and the app doesn't show up on the screen, nothing happens.

Below you can see parts of my code, I think I'm doing something wrong with onResume or onNewIntent operations. Any idea what can be wrong according to the code, or is there anything else that might cause this kind of thing? Thanks My onStart:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    setContentView(R.layout.main);
    findViewById(R.id.write_tag).setOnClickListener(mTagWriter);
    mNote = ((EditText) findViewById(R.id.note));
    mTechNotes = ((TextView) findViewById(R.id.tech_notes));
    mNote.addTextChangedListener(mTextWatcher);

    // Handle all of our received NFC intents in this activity.
    mNfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Intent filters for reading a note from a tag or exchanging over p2p.
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    try {
        ndefDetected.addDataType("text/plain");
    } catch (MalformedMimeTypeException e) { }
    mNdefExchangeFilters = new IntentFilter[] { ndefDetected, tagDetected };

    // Intent filters for writing to a tag

    mWriteTagFilters = new IntentFilter[] { tagDetected };
}

My onResume:

protected void onResume() {
    super.onResume();
    mResumed = true;
    // Sticky notes received from Android
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        NdefMessage[] messages = getNdefMessages(getIntent());
        byte[] payload = messages[0].getRecords()[0].getPayload();
        setNoteBody(new String(payload));
        setTechNotesBody(getTagInfo(getIntent()))
        setIntent(new Intent()); // Consume this intent.
    } else if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
        setTechNotesBody(getTagInfo(getIntent()));
        setIntent(new Intent());
    }
    enableNdefExchangeMode();
}

My onPause:

protected void onPause() {
    super.onPause();
    mResumed = false;
    mNfcAdapter.disableForegroundNdefPush(this);
}

My onNewIntent:

protected void onNewIntent(Intent intent) {
    // NDEF exchange mode
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        NdefMessage[] msgs = getNdefMessages(intent);
        promptForContent(msgs[0], getTagInfo(intent));
    } else if(!mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        promptForContent(null, getTagInfo(intent));
    }

    // Tag writing mode
    if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        writeTag(getNoteAsNdef(), detectedTag);
    }
}

Upvotes: 0

Views: 3599

Answers (1)

MrChaz
MrChaz

Reputation: 1085

If you've not got a filter in the manifest then your application won't be launched to handle the tag.

Upvotes: 2

Related Questions