Skip to content
Advertisement

Using NFC to open my application causes a separate version of it to be opened instead of the one that was open when RFID was found

I am attempting to create an application centered around NFC paired with multiple RFIDs. For this reason I am programming the RFIDs with NDefRecords that open a specific activity in my application. The NdefMessage doing this is as follows

NdefRecord mimeRecord =
NdefRecord.createMime("application/com.example.rfidprogrammer.testactivity","Some text".getBytes(StandardCharsets.US_ASCII));
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { mimeRecord});

And my manifest for the activity I am opening uses the following intent filter:

<activity 
android:name=".TestClass"
android:exported="true">
   <intent-filter>
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <data android:mimeType = 
         "application/com.example.rfidprogrammer.testactivity"/>
   </intent-filter>
</activity>

The code that is writing to the RFID I am using looks like this:

private String writeTagData(Tag tag) {
        StringBuilder sb = new StringBuilder();
        //get takes given tag and creates a Ndef based on information
        Ndef ndef = Ndef.get(tag);
        if(ndef != null){
            try {
                ndef.connect();
                ndef.writeNdefMessage(ndefMessage);
                ndef.close();
            } catch (IOException | FormatException e) {
                e.printStackTrace();
            }
        }else{
            //if the Ndef failed NdefFormatable will attempt, if this fails the rfid device is not
            //  recognized by this device
            NdefFormatable formatable = NdefFormatable.get(tag);
            try {
                formatable.connect();
                formatable.format(ndefMessage);
                formatable.close();
            } catch (IOException | FormatException e) {
                e.printStackTrace();
            }
        }
        byte[] id = tag.getId();
        sb.append("tID (hex): ").append(toHex(id)).append('n');
        sb.append("tID (reversed hex): ").append(toReversedHex(id)).append('n');
        sb.append("tID (dec): ").append(toDec(id)).append('n');
        sb.append("tID (reversed dec): ").append(toReversedDec(id)).append('n');
        Log.v("Write MODEn",sb.toString());
    return sb.toString();
    }

I am successfully writing this to the tag as when I then attempt to scan the RFID I am taken to the correct activity but if my application is already open, a different version of my application with a different logo is shown in the recent apps section. Is there a way for me to use Mimetypes, NdefRecords, and NFC to open the desired activity in the version of the application currently running? Can this be done without having to account/process for the intent to open a specific activity in your application?

Advertisement

Answer

You don’t seem to be doing any foreground detection, really manifest entries are only used to start your App via NFC if not already running.

If your App is running you should use one of the 2 foreground detection API’s, the old an not so reliable enableForegroundDispatch or the newer and better enableReaderMode

Here is an example of how use the better enableReaderMode

You can use both of these in conjunction with Manifest filters to handle NFC when your App is and is not running when the NFC Tag is presented.

Also note that your Manifest filter won’t trigger for the NdefFormatable of your write method, your Manifest filter need to filter for ACTION_TECH_DISCOVERED and android.nfc.tech.NdefFormatable. See Here for details

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement