You are getting odd additional characters when displaying the message since you try to display the whole raw NDEF record as text string (using some odd decoding method):NdefMessage[] messages = getNdefMessages(intent);edtUser.setText(displayByteArray(messages[0].toByteArray()));这有几个问题.首先,您通常希望使用与编写文本相同的编码对文本进行解码.例如,如果您使用There are several problems with this. First of all, you would typically want to decode the text using the same encoding that you used to write the text. For instance, if you usedString text = "...";byte[] bytes = text.getBytes(Charset.forName("US-ASCII"));要获得给定文本字符串的US-ASCII编码字节数组,您还希望使用相同的US-ASCII编码再次将字节转换为文本字符串:to get a byte array in US-ASCII encoding for a given text string, you would also want to use that same US-ASCII encoding to translate the bytes into a text string again:byte[] bytes = ...;String text = new String(bytes, "US-ASCII");第二,您将整个NDEF消息解释为文本字符串.但是,您存储在标签上的文本通常仅包含在NDEF记录的有效载荷之内.在您的情况下,前缀十"表示您使用的语言指示为"en"的NFC论坛文本记录(类型名称"T")(对于英语).因此,您需要在NDEF消息中搜索文本"记录:Second, you are interpreting the whole NDEF message as a text string. However, the text that you stored on the tag is typically only contained inside the payload of an NDEF record. In your case, the prefix "Ten" suggests that you used an NFC Forum Text record (type name "T") with the language indication "en" (for English). You would, therefore, want to search the NDEF message for the Text record:for (NdefRecord r : messages[0].getRecords()) { if (r.getTnf() == NdefRecord.TNF_WELL_KNOWN) { if (Arrays.equals(r.getType(), NdefRecord.RTD_TEXT)) {找到文本记录后,就可以解码其文本有效负载.有效负载由状态字节,语言字段和实际文本组成:Once you found the Text record, you can decode its text payload. The payload consists of a status byte, a language field and the actual text: byte[] payloadBytes = ndefRecord.getPayload(); boolean isUTF8 = (payloadBytes[0] & 0x080) == 0; //status byte: bit 7 indicates encoding (0 = UTF-8, 1 = UTF-16) int languageLength = payloadBytes[0] & 0x03F; //status byte: bits 5..0 indicate length of language code int textLength = payloadBytes.length - 1 - languageLength; String languageCode = new String(payloadBytes, 1, languageLength, "US-ASCII"); String payloadText = new String(payloadBytes, 1 + languageLength, textLength, isUTF8 ? "UTF-8" : "UTF-16"); edtUser.setText(payloadText); } }} 这篇关于读取NFC标签时出现奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 21:33