英文:
NFC getNdefMessage returns null after tag writing on Android 13
问题
以下是要翻译的内容:
"I'm trying to write an app that writes a message/command to NFC tag and read NdefMessage back without disconnecting the field.
It works perfectly on all phones and Android OS version I have tried, except it doesn't work on any of the Android 13 phones, where reading NdefMessage after write returns null.
I will not post the whole code, it basically comes down to these few lines:
ndef.connect()
ndef.writeNdefMessage(message)
val ndefMessage = ndef.getNdefMessage() --> returns null on Android 13, ndefMessage on all other OS versions
Would appreciate all the help I can get.
I have tried using foreground dispatch, reader mode with various EXTRA_READER_PRESENCE_CHECK_DELAY values, tried sleeping the thread, reading message in a loop with some sleep until it will maybe finally read it, using separate background thread for the whole operation, disabling other NFC apps, payment apps... nothing works.
It's obviously some kind of change with Android 13, because I tried also on a phone with Android 12, it worked as expected, upgraded the phone to Android 13, and doesn't work after that.
Note that tag is definitely correctly written, because it reads correctly if I add close/connect in between and read the tag with re-entering the field, or just writing the message and reading the tag in other NFC apps."
英文:
I'm trying to write an app that writes a message/command to NFC tag and read NdefMessage back without disconnecting the field.
It works perfectly on all phones and Android OS version I have tried, except it doesn't work on any of the Android 13 phones, where reading NdefMessage after write returns null.
I will not post the whole code, it basically comes down to these few lines:
var message = NdefMessage(NdefRecord.createTextRecord("en", command))
ndef.connect()
ndef.writeNdefMessage(message)
val ndefMessage = ndef.getNdefMessage() --> returns null on Android 13, ndefMessage on all other OS versions
Would appreciate all the help I can get.
I have tried using foreground dispatch, reader mode with various EXTRA_READER_PRESENCE_CHECK_DELAY values, tried sleeping the thread, reading message in a loop with some sleep until it will maybe finally read it, using separate background thread for the whole operation, disabling other NFC apps, payment apps... nothing works.
It's obviously some kind of change with Android 13, because I tried also on a phone with Android 12, it worked as expected, upgraded the phone to Android 13, and doesn't work after that.
Note that tag is definitely correctly written, because it reads correctly if I add close/connect in between and read the tag with re-entering the field, or just writing the message and reading the tag in other NFC apps.
答案1
得分: 3
已解决。
在写入和后续读取之间,需要关闭连接,再次获取ndef,然后重新连接:
ndef.connect()
ndef.writeNdefMessage(message)
ndef.close()
ndef = Ndef.get(tag)
ndef.connect()
val ndefMessage = ndef.getNdefMessage()
英文:
Resolved.
Between write and subsequent read, it's necessary to close, get ndef again, and connect again:
ndef.connect()
ndef.writeNdefMessage(message)
ndef.close()
ndef = Ndef.get(tag)
ndef.connect()
val ndefMessage = ndef.getNdefMessage()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论