Home > Enterprise >  how to set up an nfc reader app that won’t open on its own if the app is closed
how to set up an nfc reader app that won’t open on its own if the app is closed

Time:06-07

I’m doing this application that should read some nfc tags.

It is a kind of treasure hunt and who scans all the tags wins.

I structured the application so that there is an initial login and a set of data is saved in a database so that you can keep track of the players who scanned the tags.

I followed a tutorial to create an activity that can read tags. (Only one line of text should be read from the tags)

This is the code I put in the manifest

<uses-permission android:name="android.permission.NFC"/>

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>

This is the activity code

public class scanActivity extends AppCompatActivity {

private TextView scanView;
private PendingIntent pendingIntent;
private IntentFilter[] readfilters;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);

    scanView=findViewById(R.id.scanView);
    try {
        Intent intent= new Intent(this,getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter textFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED,"text/plain");
        readfilters = new IntentFilter[] {intentFilter, textFilter};
    } catch (IntentFilter.MalformedMimeTypeException e) {
        e.printStackTrace();
    }


    readTag(getIntent());
}
private void enableRead(){
    NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this,pendingIntent,readfilters,null);
}

private void disableRead(){
    NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}

@Override
protected void onResume() {
    super.onResume();
    enableRead();
}

@Override
protected void onPause() {
    super.onPause();
    disableRead();
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    readTag(getIntent());
}

private void readTag(Intent intent) {
    Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    scanView.setText("");
    if(messages != null){
        for (Parcelable message:messages){
            NdefMessage ndefMessage = (NdefMessage) message;
            for (NdefRecord record : ndefMessage.getRecords()){
                switch (record.getTnf()){
                    case NdefRecord.TNF_WELL_KNOWN:
                        scanView.append("WELL KNOWN ");
                        if (Arrays.equals(record.getType(),NdefRecord.RTD_TEXT)){
                            scanView.append("TEXT: ");
                            scanView.append(new String(record.getPayload()));
                            scanView.append("\n");
                        }
                }
            }
        }
    }
}
}

Currently when I approach a tag, the phone tries to open a task, but it ends up reopening the application and it does not even open the task with the textview on which the content of the tag should be written.

I would need to make sure that the application does not reopen again when I approach a tag, but simply switch to the next activity, keeping the previous ones.

Also, I would like to prevent the app from opening itself when approaching a tag.

If the above requests are impossible. I need at least to be able to create an activity that reads the tags without opening other activities.

I thank in advance for those who tried to help me <3.

CodePudding user response:

To prevent the App from being automatically opened automatically when approaching the Tag then remove the intent-filter entries in your manifest

If you don't want the Activity paused and resumed when a Tag is detected then don't use the older NFC API of enableForegroundDispatch use the newer and better API of enabledReaderMode as this will deliver the Tag data to a new Thread in your current activity without pausing and resuming your current activity.

Java example of enableReaderMode

  • Related