0
votes

I need some help in handling data which was received by NFC. I'm using this code to receive an NDEF message over NFC and display the text contained in the NDEF record in a toast.

Now I want to fill the received data into an EditText field. Right now it just shows the received data for a while (as a toast). I've tried to change the code, but I wasn't successful:

void parseNdefMessage(Intent intent) {
    Parcelable[] ndefMessageArray = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);
    NdefMessage ndefMessage = (NdefMessage) ndefMessageArray[0];
    Toast.makeText(this, new String(ndefMessage.getRecords()[0].getPayload()), Toast.LENGTH_LONG).show();
    Toast.makeText(
        getApplicationContext(),"Here is my text", 
        Toast.LENGTH_LONG).show();
    editText.getText().toString().equals(ndefMessage.getRecords()[0].getPayload()[0]);
    //editText = (EditText) findViewById(R.id.editText);
    //String text = editText.getText().toString();
    editText.setText(ndefMessage.getRecords()[0].getPayload()[0]); //my attempt to set my received data to "editText" field
}

Could anyone give some advice about this?

2

2 Answers

3
votes

Maybe the data changed: why dont you try:

void parseNdefMessage(Intent intent) {
    Parcelable[] ndefMessageArray = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);
    NdefMessage ndefMessage = (NdefMessage) ndefMessageArray[0];
    String msg = new String(ndefMessage.getRecords()[0].getPayload());
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    Toast.makeText( getApplicationContext(),"Here is my text", 
        Toast.LENGTH_LONG).show();

    //editText = (EditText) findViewById(R.id.editText);
    //String text = editText.getText().toString();
    editText.setText(msg); //my attempt to set my received data to "editText" field
}
0
votes

Your code does not make much sense.

  1. You use the equals() method on the string representation of the value of the editText field without checking the return value:

    editText.getText().toString().equals(ndefMessage.getRecords()[0].getPayload()[0]);
    

    As the equals() method does not change the state of the object (and in fact you throw away that string object anyways), calling this method makes only sense if you check its return value (which would tell you if the string value in editText matches the value of ...getPayload()[0]). However, as getPayload()[0] is a byte value, the equals() method will always return false. Hence, you can drop this line of code.

  2. ndefMessage.getRecords()[0].getPayload()[0] gives you the first byte of the payload of that NDEF record. Thus, this is only the first character (or as you seem to use UTF-8 encoding possibly even only a part of the first character).

  3. When you use the byte value ndefMessage.getRecords()[0].getPayload()[0] as parameter for the method editText.setText(x), it will be treated as an integer indicating a resource ID. This does not make any sense.

So instead, you would want to convert the payload of the NDEF record into a string (using the default UTF-8 charset encoding):

String text = new String(ndefMessage.getRecords()[0].getPayload());

You can then assign that string value to editText:

editText.setText(text);

This is just the same as you already do when setting the message of the toast:

Toast.makeText(this,
               new String(ndefMessage.getRecords()[0].getPayload()),   <--- HERE 
               Toast.LENGTH_LONG).show();