Write Ndef on a tag
thibww67 opened this issue · 6 comments
Please answer these questions as part of your new issue.
Which version of Python are you using?
Python 3.7.3
Which OS and version are you running?
Raspbian Lite on RPi
Is SPI enabled?
Yes
Are you trying to read/write a Mifare Classic card? (This is currently unsupported)
No
I'm trying to write a text as a Ndef message on a tag with my Explore-NFC. I see that we can read easily Ndef message but how can I write it properly ?
Should I encode my text with ndef library, and then write it with mifare.write_block ?
Thank you !
Hi @thibww67,
Yes, write_block()
is the correct way to go about this. First, if the tag is not already "formatted" as NDEF, you'll need to write the "capability container":
mifare.write_block(3, "\xe1\x10\x12\x00")
Once that's done, you can write the contents of your full NDEF message from block 4 on. If you're using ndeflib
, that should look something like this:
import ndef
BLOCK_SIZE = 4
STARTING_BLOCK = 4
record = ndef.TextRecord("testing123")
bytes_to_write = b''.join((ndef.message_encoder([record])))
blocks_to_write = len(bytes_to_write) / BLOCK_SIZE
for block in range(STARTING_BLOCK, STARTING_BLOCK + blocks_to_write):
mifare.write_block(block, bytes_to_write[block * BLOCK_SIZE:BLOCK_SIZE]
I wrote that mostly from memory, so let me know if you run into any issues.
Thank you for your quick answer !
I will try it soon and let you know if it works.
Now I will try to see if it is possible to use 2 Explore NFC on a single RPi, but I think it is not because of the software.
Yes, that what I thought, for my application I think I will use the Adafruit NFC Breakout Board, and with its library for python, I can use multiple modules on the SPI.
But thank you for your help !even if I will change my card I tried to use this librar, my code is very simple :
import nxppy
import ndef
import time
mifare = nxppy.Mifare()
#print card UIDs as they are detected
while True:
try:
#Select the first available tag and return the UID
uid = mifare.select()
print(uid)
# Read a Ndef message
ndef_data = mifare.read_ndef()
ndef_records = list(ndef.message_decoder(ndef_data))
print(ndef_records)
except nxppy.SelectError:
# SelectError is raised if no card is in the field.
pass
sleep(1)
And I have this error runing python3 Read.py :
nxppy.InitError: Nxppy: Unknown Error from Undefined Component
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "Read.py", line 5, in
mifare = nxppy.Mifare()
SystemError: <class 'nxppy.Mifare'> returned a result with an error set
I saw that this error was existing in an other issue but there is any solution ?
I reflashed it on a new raspbian Lite and it works perfectly !
Concerning the code you dropped, I had to correct some things, for example block_write can't write a message different of 4 bytes, so now I can right anything in the memory but it is not recognize as an Ndef, however the message is correctly encode. I keep working on it !
EDIT : I succeeded to write Ndef by giving a bytes command before the message, if it interests you, I can share the code :)