我已經從示例中製作了一個應用程序,用於在Android上編寫NFC代碼。我的標籤是恩智浦NTAG216。編寫NFC代碼的Android導致「未知內容」類型
但是我有一個問題:寫我的應用標記後,恩智浦的TagWriter應用顯示的數據爲「未知內容」是這樣的:
然而,當我寫的值作爲「平原文本」採用恩智浦TagWriter,我得到:
這裏是我的onNewIntent代碼:
@Override
protected void onNewIntent(Intent intent) {
// Tag writing mode
String zawartosc = "Data from field";
if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefRecord record = NdefRecord.createMime(zawartosc, zawartosc.getBytes(Charset.forName("UTF-8")));
NdefMessage message = new NdefMessage(new NdefRecord[]{record});
if (writeTag(message, detectedTag)) {
Toast.makeText(this, "Success", Toast.LENGTH_LONG)
.show();
}
}
}
以及編寫標籤:
public boolean writeTag(NdefMessage message, Tag tag) {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
Toast.makeText(getApplicationContext(),
"Unwritable",
Toast.LENGTH_SHORT).show();
return false;
}
if (ndef.getMaxSize() < size) {
Toast.makeText(getApplicationContext(),
"Text is too short",
Toast.LENGTH_SHORT).show();
return false;
}
ndef.writeNdefMessage(message);
return true;
} else {
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
return true;
} catch (IOException e) {
return false;
}
} else {
return false;
}
}
} catch (Exception e) {
return false;
}
}
有沒有辦法以某種方式改變內容類型相同的是NXP TagWriter寫?稍後,如果它是未知類型,則會遇到問題。