I want to write simple text data to my NXP MiFARE DesFire EV1 (NDEF Type 4 Tag). However, the writing process always fails with an IOExcetion
For writing I get the NFC-Tag I use the function write
:
private void write(String mimeType, String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = {createRecord(mimeType, text)};
NdefMessage message = new NdefMessage(records);
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(message);
ndef.close();
}
The result in the third line (Ndef ndef = Ndef.get(tag)
) is the following:
TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
From this I assumed, the Tag is formatted correclty (as NDEF).
Now, when calling ndef.connect()
it just says java.io.exception
without any additional information about the error. The other parts of the code, that get called is appended.
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
String serialId = Utility.bytesToHex(tag.getId());
Log.d("[WriteCard]", "Serial Number: " + serialId);
Toast.makeText(this, serialId, Toast.LENGTH_SHORT).show();
}
}
}
// When the write Button is clicked
public void onClick(View view) {
if (nfc_adapter == null) {
Toast.makeText(this, "No NFC", Toast.LENGTH_SHORT).show();
return;
}
int id = view.getId();
Intent intent = getIntent();
try {
write("type/1", spinner_location.toString(), tag);
}
catch(Exception e) {
Log.d("[WriteCard]", e.toString());
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
The NXP Tag Info App reports the following:
- IC Type: MiFARE DESFire EV1
- Type 4 Tag
- NFC data set access: Read&Write
Additional Info: The writing process with Android-Apps like NFC TagWriter by NXP or wakdev NFC Tools works without any problem, thus I assume, the Tag is working correctly.