在Android NDEF记录的有效载荷奇怪的字符载荷、字符、奇怪、有效

2023-09-04 05:34:54 作者:我不怕毕业,我怕的是离别

我刚开始与Android NFC编码,我已经成功地读写NDEF数据到MIFARE经典标记。的问题是,当应用程序从读NDEF记录中的有效载荷,它总是​​包含字符*恩'在文本的开头。我认为这是语言字符,但如何才能获得真正的短信没有这种人物?

这是从什么标签读取应用程序的截图,实际文本的'Hello World

这里是code读

  @覆盖
公共无效onNewIntent(意向意图){
    Log.i(前景派遣,意图发现的标签:+意图);
   // mText.setText(发现标签NDEF+ ++ mCount +意图:+意图);

    如果(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
        Parcelable [] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

        如果(rawMsgs!= NULL){
            NdefMessage []封邮件=新NdefMessage [rawMsgs.length]

            的for(int i = 0; I< rawMsgs.length;我++){
                封邮件[I] =(NdefMessage)rawMsgs [I]
            }

            NdefMessage味精=封邮件[0];

            尝试 {
            mText.setText(新字符串(msg.getRecords()[0] .getPayload(),UTF-8));
            }赶上(例外五){
                e.printStackTrace();
            }
        }
    }
}
 

解决方案

你看到的是一个NDEF文字记录的原始数据转换为UTF-8。

在NDEF文字记录是建立这样的:

第一个字节:控制字节

  

位7:0:该文本是EN codeD的UTF-8 1:文字是EN $ C $光盘   UTF16

     

位6:保留(必须设置为零)

     

位5..0:IANA的语言code的长度

workbench如何在实体上创建一条线 我想在实体上添加线载荷

这是其次是语言code,存储在US-ASCII(EN你的情况),在RFC 3066中定义的语言 - code的长度是由于在控制字节。

这之后是在格式的文本所指定的位控制字节的7。

在空方的性格来自于你的转换原始数据转换为UTF-8。我几乎可以肯定,该控制字节你的情况的数值为2,既然有这个数值就被替换为UNI code-集非打印占位符不打印字符。这通常显示为空方。

I just started coding with Android NFC, i've successfully read and write NDEF data into mifare classic tag. The problem is when app read the payload from ndef record, it always contain character '*en' at the beginning of the text. I think it is language character, but how to get the real text message without that character?

This is the screenshot what app read from the tag, the actual text is 'Hello World'

Here is the code to read

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
   // mText.setText("Discovered tag NDEF " + ++mCount + " with intent: " + intent);

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMsgs = intent.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];
            }

            NdefMessage msg = msgs[0];

            try {
            mText.setText(new String(msg.getRecords()[0].getPayload(), "UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

解决方案

What you're seeing is the raw data of an NDef text-record converted to UTF8.

The NDef text-record is build like this:

First byte: Control-Byte

Bit 7: 0: The text is encoded in UTF-8 1: The text is encoded in UTF16

Bit 6: RFU (MUST be set to zero)

Bit 5..0: The length of the IANA language code.

This is followed by the language code, stored in US-ASCII (en in your case) as defined in RFC 3066. The length of the language-code is given in the control-byte.

And this is followed by the text in the format as specified by bit 7 of the control-byte.

The empty square character comes from your conversion of raw data into UTF-8. I'm almost sure that the control-byte in your case has the numeric value 2. Since there is no printable character for this numeric value it gets replaced with the non-printable placeholder character from the unicode-set. This is usually displayed as an empty square.