0

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]
Community
  • 1
  • 1
user3623498
  • 69
  • 1
  • 1
  • 8
  • 1
    From you log it appears that wait just 80ms before the next write starts. You may try to wait a bit before the next attempt to give the chip time to complete the internal writing prozess. – corvairjo Aug 27 '15 at 07:42
  • added a `Thread.sleep(1000)` after every `writeNdefMessage()` same issue... – user3623498 Aug 27 '15 at 07:48
  • @corvairjo edited OP with your advice – user3623498 Aug 27 '15 at 07:55
  • I am not sure I understand you. Your logcat shows now "write OK", while it it had been "error write Tag..." before. – corvairjo Aug 27 '15 at 16:06
  • @corvairjo I added a `Thread.sleep(2000)` after every write... That's why it's writing ok. But I want to do it without waiting two seconds. – user3623498 Aug 28 '15 at 06:23

0 Answers0