简体   繁体   English

从通过NFC接收的NDEF消息中获取文本

[英]Get text from NDEF message received over NFC

I need some help in handling data which was received by NFC. 在处理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. 我正在使用此代码通过NFC接收NDEF消息,并在烤面包中显示NDEF记录中包含的文本。

Now I want to fill the received data into an EditText field. 现在,我想将接收到的数据填充到EditText字段中。 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? 有人可以为此提供一些建议吗?

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
}

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字段的值的字符串表示形式使用equals()方法,而不检查返回值:

     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] ). 由于equals()方法不会更改对象的状态(并且实际上您还是丢弃了该字符串对象),因此仅当检查其返回值时才调用此方法(它会告诉您是否editText的字符串值) editText...getPayload()[0]的值匹配。 However, as getPayload()[0] is a byte value, the equals() method will always return false . 但是,由于getPayload()[0]是字节值,所以equals()方法将始终返回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. ndefMessage.getRecords()[0].getPayload()[0]为您提供该NDEF记录的有效负载的第一个字节。 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). 因此,这只是第一个字符(或者您似乎使用UTF-8编码,甚至可能只是第一个字符的一部分)。

  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. 当使用字节值ndefMessage.getRecords()[0].getPayload()[0]作为方法editText.setText(x) ,它将被视为指示资源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): 因此,您需要将NDEF记录的有效负载转换为字符串(使用默认的UTF-8字符集编码):

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

You can then assign that string value to editText : 然后可以将该字符串值分配给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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM