NFC getNdefMessage 在 Android 13 上标签写入后返回 null。

huangapple go评论55阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年2月24日 17:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定