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?
我刚开始用Android NFC编码,我已经成功地将NDEF数据写入mifare classic标签中。问题是当应用程序读取ndef记录的有效负载时,它总是在文本开头包含字符'*en'。我认为这是语言的特性,但是如何得到真正的文本信息而没有这个特性呢?
This is the screenshot what app read from the tag, the actual text is 'Hello World'
这是应用程序从标签读取的屏幕截图,实际文本是" 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();
}
}
}
}
1 个解决方案
#1
14
What you're seeing is the raw data of an NDef text-record converted to UTF8.
您所看到的是将NDef文本记录转换为UTF8的原始数据。
The NDef text-record is build like this:
NDef的文本记录是这样构建的:
First byte: Control-Byte
第一个字节:控制字节
Bit 7: 0: The text is encoded in UTF-8 1: The text is encoded in UTF16
文本编码在UTF-8 1:文本编码在UTF16中。
Bit 6: RFU (MUST be set to zero)
位6:RFU(必须设置为零)
Bit 5..0: The length of the IANA language code.
位5 . .0:IANA语言代码的长度。
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.
接下来是语言代码,在RFC 3066中以US-ASCII(在您的例子中)存储。语言代码的长度是在控制字节中给出的。
And this is followed by the text in the format as specified by bit 7 of the control-byte.
然后是按照控制字节的位7指定的格式的文本。
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.
空方字符来自于将原始数据转换为UTF-8。我几乎可以肯定,在您的案例中,控制字节具有数值2。由于这个数值没有可打印的字符,所以它被从unicode-set中替换为不可打印的占位符字符。这通常显示为空的正方形。
#1
14
What you're seeing is the raw data of an NDef text-record converted to UTF8.
您所看到的是将NDef文本记录转换为UTF8的原始数据。
The NDef text-record is build like this:
NDef的文本记录是这样构建的:
First byte: Control-Byte
第一个字节:控制字节
Bit 7: 0: The text is encoded in UTF-8 1: The text is encoded in UTF16
文本编码在UTF-8 1:文本编码在UTF16中。
Bit 6: RFU (MUST be set to zero)
位6:RFU(必须设置为零)
Bit 5..0: The length of the IANA language code.
位5 . .0:IANA语言代码的长度。
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.
接下来是语言代码,在RFC 3066中以US-ASCII(在您的例子中)存储。语言代码的长度是在控制字节中给出的。
And this is followed by the text in the format as specified by bit 7 of the control-byte.
然后是按照控制字节的位7指定的格式的文本。
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.
空方字符来自于将原始数据转换为UTF-8。我几乎可以肯定,在您的案例中,控制字节具有数值2。由于这个数值没有可打印的字符,所以它被从unicode-set中替换为不可打印的占位符字符。这通常显示为空的正方形。