Search code examples
javaandroidnfcmifare

How to display NDEF message after NDEF discovered/activity launched?


Have read through a number of questions similar to mine - I can only apologise if I have missed the solution to my woes, but I really can't work this out!

I have managed to get my nfc activity working - tapping the tag launches the correct activity of my app - but the ndef message is not displayed and I can't figure out why...

The tag is a Mifare Ultralight with a Plain Text message encoded. (mimetype: plain/text)

Here's my code, thank you all for your help, this forum should have a 'donate a beer' button!

package com.example.prototype02;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;

public class nfcActivity extends Activity {
    private static final String TAG = "NFCReadTag";
    private NfcAdapter mNfcAdapter;
    private IntentFilter[] mNdefExchangeFilters;
    private PendingIntent mNfcPendingIntent;

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

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);


        IntentFilter nfcIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        nfcIntent.addDataScheme("text/plain");      
        mNdefExchangeFilters = new IntentFilter[] { nfcIntent };

    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);      
        if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            NdefMessage[] messages = null;
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(mNfcAdapter.EXTRA_NDEF_MESSAGES);
            if (rawMsgs != null) {
                messages = new NdefMessage[rawMsgs.length];
                for (int i = 0; i < rawMsgs.length; i++) {
                    messages[i] = (NdefMessage) rawMsgs[i];
                }}
                else if (rawMsgs == null){
                    Toast.makeText(getApplicationContext(), "No NDEF Message Read", Toast.LENGTH_LONG).show();
                }
            if(messages[0] != null) {
                String result="";
                byte[] payload = messages[0].getRecords()[0].getPayload();
                for (int b = 1; b<payload.length; b++) { // skip SOH
                    result += (char) payload[b];
                }
                Toast.makeText(getApplicationContext(), "Safe Location Registered - " + result, Toast.LENGTH_SHORT).show();
            }
        }
        else Toast.makeText(getApplicationContext(), "Intent Error...", Toast.LENGTH_LONG).show();

        }
    }

Solution

  • If your activity is started by the NFC intent, then onNewIntent() will not be called (it will only be called when you scan the tag when the activity is already in the foreground). Try doing something like calling onNewIntent(getIntent()) in onCreate().