我正在尝试从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'是语言标题。但是如何将其删除?
我已尝试使用子字符串,但在此之后甚至不能工作......
关于如何删除此语言标题的任何想法???
答案 0 :(得分:6)
您可能遇到同样的问题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!!");
}