[英]Id on Nfc tag not unique?
我在android手機上讀取nfc標簽,我寫給標簽的值是這樣寫的:
@Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
return readText(ndefRecord);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported Encoding", e);
}
}
}
return null;
}
private String readText(NdefRecord record) throws UnsupportedEncodingException {
byte[] payload = record.getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code
return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); // Get the Text
}
我應該嘗試讀取NfcTag的唯一ID:
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
mNfcId = tag.getId().toString();
但這在下次閱讀時給出了不同的結果! 如何從nfc標簽讀取唯一的只讀標簽?
出於隱私原因,有些標簽沒有唯一ID。 其中大多數包含NFC芯片的旅行證件和信用卡都在其中。
另外:不要指望可以讀取的ID的唯一性。 這些很容易被偽造。 同樣,許多NFC標簽制造商也不保證ID的全球唯一性。
除了Nils在他的帖子中已經解釋過的內容之外,在字節數組上使用toString()
不會為您提供有關字節數組內容的有用信息。 而是使用如下方法將UID字節數組轉換為字符串表示形式:
public static String convertByteArrayToHexString (byte[] b) {
if (b != null) {
StringBuilder s = new StringBuilder(2 * b.length);
for (int i = 0; i < b.length; ++i) {
final String t = Integer.toHexString(b[i]);
final int l = t.length();
if (l > 2) {
s.append(t.substring(l - 2));
} else {
if (l == 1) {
s.append("0");
}
s.append(t);
}
}
return s.toString();
} else {
return "";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.