Whats Going On
Whats Going On

Reputation: 1397

How to write data as multiple records on NFC tag in Android?

I followed this to write data to an NFC Tag.

This is my MainActivity and my NFCManager.

I successfully write data to the NFC tag.

But its placing data in Record 0 only. There are no other records created on the tag.

However, I want to place the data into multiple records like Record 0, Record 1, Record 2, Record 3.

So I modified MainActivity like this

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

    nfcMger = new NFCManager(this);

    v = findViewById(R.id.mainLyt);

    final EditText et0 = (EditText) findViewById(R.id.cnt0);
    final EditText et1 = (EditText) findViewById(R.id.cnt1);
    final EditText et2 = (EditText) findViewById(R.id.cnt2);
    final EditText et3 = (EditText) findViewById(R.id.cnt3);

    FloatingActionButton btn = (FloatingActionButton) findViewById(R.id.fab);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String ct0 = et0.getText().toString();
            String ct1 = et1.getText().toString();
            String ct2 = et2.getText().toString();
            String ct3 = et3.getText().toString();


            message =  nfcMger.createUriMessage(ct0, "id");
            message =  nfcMger.createUriMessage(ct1, "name");
            message =  nfcMger.createUriMessage(ct2, "role");
            message =  nfcMger.createUriMessage(ct3, "level");


            if (message != null) {

                dialog = new ProgressDialog(MainActivity.this);
                dialog.setMessage("Tag NFC Tag please");
                dialog.show();
            }
        }
    });

}

But when I use this and write data to the tag it stores only a Record 0 on the tag. There is no other record.

I only get the last saved data in Record 0:

My expectation is to write data to multiple records like this:

Can anyone suggest me how to insert the data to multiple records (Record 0, Record 1, Record 2, Record 3)?

Update

with reference of user@Michael Roland I have updated my code I got this error

enter image description here

Here I followed Method NdefRecord.createTextRecord(“en” , “string”) not working below API level 21 And I have given the NdefRecord method but its showing error when I Give same for the message = new NdefMessage(new NdefRecord[] {

so out side the message its not a error but I want it inside the message

thanks @Michael Roland for the Support

Upvotes: 2

Views: 2588

Answers (1)

Michael Roland
Michael Roland

Reputation: 40831

You only find one message containing one record on the tag because that's what you do in your code. Here you create one NDEF message containing one URI record:

message =  nfcMger.createUriMessage(ct0, "id");

Right on the next line you overwrite the previously stored message (referenced in message) with another NDEF message containing only one URI record:

message =  nfcMger.createUriMessage(ct1, "name");

And on the next two lines you do that same thing again:

message =  nfcMger.createUriMessage(ct2, "role");
message =  nfcMger.createUriMessage(ct3, "level");

Consequently, only the last line will effectively store an NDEF message in message.

You probably then store that message onto the tag1.

However, given the above screenshot, you would probably rather want to create one NDEF message containing 4 Text records. E.g. like this:

message = new NdefMessage(new NdefRecord[] {
        NdefRecord.createTextRecord("en", "Text 1"),
        NdefRecord.createTextRecord("en", "Text 2"),
        NdefRecord.createTextRecord("en", "Text 3"),
        NdefRecord.createTextRecord("en", "Text 4"),
});

Btw. since createTextRecord() is only available starting with API level 21, you might want to check out Method NdefRecord.createTextRecord("en" , "string") not working below API level 21 for lower API levels.

Also note that Text records are intended for storing human-readable textual data. For storing machine-readable information, other records (such as the NFC Forum External Type) would be more suitable.


1) Though, you seem to be rather reluctant to present a MCVE but believe that readers of your question would want to browse through tons of unnecessary code on GitHub to understand your problem. Note that that code might not even exist for the whole lifetime of the question and once that source code disappears, your question will be mostly useless for others.

Finally, note that if your really observed one record of type text/plain with the textual value "junior" on the tag, you certainly did not use the code from GitHub without further modifications since the code that you revealed would have created one (malformed?) URI record.

Upvotes: 3

Related Questions