0

I have developped 2 Android applications. The first one, to write into an NFC tag, and the second to read the contents I have written .

This is what i did in the first application (WriteNFC)

private NdefRecord createRecord1(String data)
{ 
byte[] payload = data.getBytes(Charset.forName("UTF-8"));
byte[] empty = new byte[] {};
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, empty, empty, payload);
}
private NdefRecord createRecord2(String data)
{ 
byte[] payload = data.getBytes(Charset.forName("UTF-8"));
byte[] empty = new byte[] {};
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, payload, empty, empty);
}

And in the second application (ReadNFC)

NdefRecord cardRecord = msg.getRecords()[1];//Extract the second Record
String url_data = new String(cardRecord.getType());//Read data type

When I read with my own application (ReadNFC), of course I had on screen only the payload of the second Record, which I stored through "Record Type". But with a third-party application, especially that natively installed ("tag") -shown in photo-, It display correctly the first record, and for the second it's an empty field. How can I hide this field. Otherwise, how can I force the other third-party applications to not read the second record?

enter image description here

mOmO
  • 186
  • 4
  • 12

3 Answers3

3

You simply cannot do that. Android will read the complete NDEF messages (i.e. all records) and pass it on in the Intent to an app.

NFC guy
  • 10,151
  • 3
  • 27
  • 58
0

Upps, it is no wonder this is happening, look at your code. First

return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, empty, empty, payload);

then

return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, payload, empty, empty);

So the 3rd party apps are really showing the correct data, you have a bug in your record creation.

The native NDEF support in android is somewhat crude (byte-array based), so I've written a library which helps in creating records - which you might find interesting. The above issue might be simple to resolve, but there are many other much more complex record types, so some help might come in handy ;-)

Edit: So if this is the preferred result, rather create an Unknown Record and put your 'secret' data as the payload - the will not be any good way for any 3rd party app to display that data - whereas the ID of the absolute URI Record certainly can be displayed by any NDEF-reading app (like mine?)

ThomasRS
  • 8,215
  • 5
  • 33
  • 48
  • There is no bug in my app dear Thomas. I simply choose to put payload as the Record Type, and empty in payload field. [ as you see, I'm using getType() and not getPayload() ]. In this way the third-party application will dispaly an empty field. It's not what exactly I'm looking for, but at least it display an empty field instead of the second Record! – mOmO Dec 11 '12 at 14:39
  • I already have used Unknown Record. And indeed the third-party application doesn't show the data of the second record. BUT, when we use a TNF_UNKNOWN Record, the type field should be empty. And as a result, the third-party application display a specific message : "Unknown tag type". Same thing when we use TNF_EXTERNAL_TYPE! – mOmO Dec 12 '12 at 15:11
0

This third party app was bothersome to me, so I had to use foregroundDispatch to read the tag contents manually, there you have the freedom to read or not read anything you want. This snippet is from the OnResume(). `

mNfcAdapter.enableForegroundDispatch(this, pendingIntent,
                intentFiltersArray, techListsArray);

            Toast.makeText(getApplicationContext(), "TAG disscovered",
                    Toast.LENGTH_LONG).show();
            Parcelable[] rawMsgs = getIntent()
            .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

            if (rawMsgs != null) {
                NdefMessage[] msgs = new NdefMessage[rawMsgs.length];


                for (int i = 0; i < rawMsgs.length; i++) {

                    msgs[i] = (NdefMessage) rawMsgs[i];
                    setText=new String(msgs[i].getRecords()[0].getPayload());
                }
                mInfoText.setText(setText);
            }

        }`

Here I get payload of the 1st record.

Moon Moon
  • 51
  • 1
  • 6