4

可能重复:
Android NDEF 记录负载上的奇怪字符

我正在尝试从 NFC 标签中读取一些纯文本。我的代码如下;

public void processReadIntent(Intent intent){

    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);

    NdefMessage msg = (NdefMessage) rawMsgs[0];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    Log.d("msg", msg.getRecords()[0].getPayload().toString());

    String PatientId=new String(msg.getRecords()[0].getPayload());     
    String UserName="nurse";
    String Password="nurse";

   Toast.makeText(getApplicationContext(), PatientId, Toast.LENGTH_LONG).show();

    //tv.setText(new String(msg.getRecords()[0].getPayload()));
}

但是,这里的问题是当我读取数据时,我可以看到我想要的数据在开始时有一个“en”。 例如:如果我在“john”中的实际数据,当我阅读时,我可以将其视为“enjohn”。 我知道'en'是语言标题。但是如何删除它?

我尝试过使用子字符串,但在那之后甚至不工作......

关于如何删除此语言标头的任何想法???

4

1 回答 1

8

可能,您在这里遇到同样的问题以及如何在此处正确读取 NFC 标签

摘自第二个链接的片段。

 try
{
        byte[] payload = record.getPayload();

        /*
     * payload[0] contains the "Status Byte Encodings" field, per the
     * NFC Forum "Text Record Type Definition" section 3.2.1.
     *
     * bit7 is the Text Encoding Field.
     *
     * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1):
     * The text is encoded in UTF16
     *
     * Bit_6 is reserved for future use and must be set to zero.
     *
     * Bits 5 to 0 are the length of the IANA language code.
     */

         //Get the Text Encoding
        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";

        //Get the Language Code
        int languageCodeLength = payload[0] & 0077;
        String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");

        //Get the Text
        String text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);

    return new TextRecord(text, languageCode);
}
catch(Exception e)
{
        throw new RuntimeException("Record Parsing Failure!!");
}
于 2013-01-30T15:40:54.133 回答