我正在向微控制器发送NDEF消息。写入和读取仅适用于一次写入/读取(但不能同时进行)。 它类似于:Write NDEF message multiple times to same tag?,但没有帮助。
我的代码是:
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();
如果我想写4次,第一次就可以了,之后最后3次是错误。 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
用writeNdefMessage()
替换getNdefMessage()
以便阅读它有用......
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
编辑:每次写入后添加Thread.sleep(2000)
,Log.e("Detected TAG", detectedTag.toString())
中添加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]