Question

I'm building an Android application that is going to read NFC tags using the ndeftools library.

At the moment when I read a tag within my application it minimizes and is handled by the built in Android tag reader. Is it possible to stop the application from pausing and just have it show the contents in a TextView.

Note: TagReaderActivity is just an extended NfcReaderActivity

Here's my current code:

import org.ndeftools.Message;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seaf.nfc.TagReaderActivity;

public class DemoNFCActivity extends TagReaderActivity {

    protected LinearLayout linearLayout;

    private static final String DEMO = DemoNFCActivity.class.getName();

    protected TextView feature;
    protected TextView state;
    protected TextView data;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        this.setContentView(R.layout.layout_demo_nfc);

        feature = (TextView) findViewById(R.id.textView1);
        state = (TextView) findViewById(R.id.textView2);
        data = (TextView) findViewById(R.id.textView3);

        super.onCreate(savedInstanceState);

    }


    @Override
    protected void readNdefMessage(Message message) {

//      [INSERT SOLUTION HERE] :D
//      
//      if (data != null) {
//          
//          data.setText(message.toString());
//          data.setBackgroundColor(getResources().getColor(R.color.green_light));
//          
//      }

    }

    @Override
    protected void onNfcFeatureFound() {

        super.onNfcFeatureFound();

        if (feature != null) {

            feature.setText(getString(R.string.nfc_feature_true));
            feature.setBackgroundColor(getResources().getColor(R.color.green_light));

        }
    }

    @Override
    protected void onNfcFeatureNotFound() {

        super.onNfcStateDisabled();

        if (feature != null) {

            feature.setText(getString(R.string.nfc_feature_false));
            feature.setBackgroundColor(getResources().getColor(R.color.red_light));

        }

    }

    @Override
    protected void onNfcStateEnabled() {

        super.onNfcStateEnabled();

        if (state != null) {

            state.setText(getString(R.string.nfc_state_true));
            state.setBackgroundColor(getResources().getColor(R.color.green_light));

        }

    }

    @Override
    protected void onNfcStateDisabled() {

        super.onNfcStateDisabled();

        if (state != null) {

            state.setText(getString(R.string.nfc_state_false));
            state.setBackgroundColor(getResources().getColor(R.color.red_light));

        }

    }

}
Was it helpful?

Solution

The NFC subsystem calls first onPause(), then onNewIntent() then onResume(). You should not need to work around this, since the whole process is very brief.

It seems the issue here is that your foreground mode is not activated. See the onCreate() method in DefaultNfcReaderActivity, and note the call to

setDetecting(true);

This is so because it is not necessarily so that you want to accept incoming NFC traffic at all times.

OTHER TIPS

You would need to register for the foreground dispatch system (or optionally the reader mode system if your app is Android 4.4+ only). See the Android NFC documentation for further information on how to use the foreground dispatch system.

With regard to the ndeftools library, the NfcDetectorActivity should do (or be adaptable to) what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top