How to loop thru messages and records

Asked by Juan Trujillo on 2015-07-03

I'm assuming that I can have multiple NDEF messages stored in a tag. I'm also assuming that I can have multiple NDEF records stored in one NDEF message...

My questions is how do I can I loop thru the messages on a tag? And how do I loop thru the records in a message?

Based on the following test code, tag.ndef.message object should have everything I need...

def connected(tag):
  print tag
  if tag.ndef:
    print tag.ndef.message.pretty()
  else:
    "Sorry, no NDEF"

clf = nfc.ContactlessFrontend('tty:AMA0:pn53x')
print(clf)
clf.connect(rdwr={'on-connect': connected})

Thanks!

Question information

Language:
English Edit question
Status:
Solved
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen Tiedemann
Solved:
2015-07-06
Last query:
2015-07-06
Last reply:
2015-07-06

There should only be one NDEF message on a tag. Technically some tags support more in a proprietary way but that is not interoperable (can't be copied between different tags) and also dangerous as touching a tag should yield one reproducible result and not an arbitrary decision of the reader.

There can of course be multiple records in a message and the nfc.ndef.Message object that tag.ndef.message provides presents them as an iterable list.

for record in tag.ndef.message:
    print record.pretty()

Thanks Stephen Tiedemann, that solved my question.