I'm sending a NDEF message to a microcontroller. Write and read are working fine for only one write/read (but not both in the same time). It's similar like this: Write NDEF message multiple times to same tag? but that didn't helped.
My code is:
new Thread(new Runnable() {
@Override
public void run() {
Ndef ndef = Ndef.get(detectedTag);
try {
ndef.connect();
} catch (IOException e) {
Log.e("connect", "Connection error");
e.printStackTrace();
}
try {
ndef.writeNdefMessage(getNdefMessage("Hey"));
Log.e("write", "write ok");
} catch (Exception e) {
Log.e("write", "error write "+e.getMessage());
}
try {
ndef.writeNdefMessage(getNdefMessage("Hey"));
Log.e("write", "write ok");
} catch (Exception e) {
Log.e("write", "error write "+e.getMessage());
}
try {
ndef.writeNdefMessage(getNdefMessage("Hey"));
Log.e("write", "write ok");
} catch (Exception e) {
Log.e("write", "error write "+e.getMessage());
}
try {
ndef.writeNdefMessage(getNdefMessage("Hey"));
Log.e("write", "write ok");
} catch (Exception e) {
Log.e("write", "error write "+e.getMessage());
}
finally {
try {
ndef.close();
} catch (IOException e) {
Log.e("write", "ndef close");
}
}
}
}).start();
If I want to write 4 times, first one is ok, after that last 3 are errors. logcat:
08-27 10:11:45.029 12850-12850/com.example.me.testnfc E/write﹕ write ok
08-27 10:11:45.299 12850-12850/com.example.me.testnfc E/write﹕ error write Tag is not ndef
08-27 10:11:45.379 12850-12850/com.example.me.testnfc E/write﹕ error write Tag is not ndef
08-27 10:11:45.459 12850-12850/com.example.me.testnfc E/write﹕ error write Tag is not ndef
Replacing writeNdefMessage()
with getNdefMessage()
for read it works...
Logcat:
08-27 10:25:26.584 24701-24701/com.example.me.testnfc E/Read﹕ Read ok
08-27 10:25:26.646 24701-24701/com.example.me.testnfc E/Read﹕ Read ok
08-27 10:25:26.709 24701-24701/com.example.me.testnfc E/Read﹕ Read ok
08-27 10:25:26.783 24701-24701/com.example.me.testnfc E/Read﹕ Read ok
EDIT: added a Thread.sleep(2000)
after every write, and a Log.e("Detected TAG", detectedTag.toString())
in onNewIntent()
Logcat:
08-27 10:50:57.988 15948-15948/com.example.me.testnfc E/Detected TAG﹕ TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
08-27 10:51:00.461 15948-16128/com.example.me.testnfc E/write﹕ write ok
08-27 10:51:02.221 15948-15948/com.example.me.testnfc E/Detected TAG﹕ TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
08-27 10:51:02.533 15948-16128/com.example.me.testnfc E/write﹕ write ok
08-27 10:51:04.312 15948-15948/com.example.me.testnfc E/Detected TAG﹕ TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
08-27 10:51:04.620 15948-16128/com.example.me.testnfc E/write﹕ write ok
08-27 10:51:06.405 15948-15948/com.example.me.testnfc E/Detected TAG﹕ TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
08-27 10:51:06.692 15948-16128/com.example.me.testnfc E/write﹕ write ok
08-27 10:51:08.480 15948-15948/com.example.me.testnfc E/Detected TAG﹕ TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.Ndef]