可能重复:
Strange character on Android NDEF record payload
我正试着从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”是语言标题。但我该如何移除它呢??
我试过用substring,但在那之后就不起作用了…
知道如何删除这个语言标题吗????

最佳答案

或许,你也有同样的问题here以及如何正确阅读nfc标签here
从第二个链接获取的片段。

 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!!");
}

07-28 12:28
查看更多