简体   繁体   中英

Writing NDEF to Mifare S50 with Raspberry Pi

I am trying to write NDEF data to an NFC Tag (Mifare S50 chip) so it can work with my iPhone, I'm using a Raspberry Pi 4 with a RC522 NFC module connected via GPIO and using SPI.

I tried a few approaches to this, and ended up with the code above which didn't seem to write anything to the tag, and it's indeed not readable by my phone, and I think I might be messing up somewhere.

Here's the code:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import ndef
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        message = [ndef.TextRecord("Hello"), ndef.TextRecord("World")]
        print("Approach a tag to the reader")
        reader.write(b''.join(ndef.message_encoder(message)))
        print("Written")
finally:
        GPIO.cleanup()

And this is the console output:

Traceback (most recent call last):
  File "Write.py", line 12, in <module>
    reader.write(b''.join(ndef.message_encoder(message)))
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 62, in write
    id, text_in = self.write_no_block(text)
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 78, in write_no_block
    data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii')))
AttributeError: 'bytes' object has no attribute 'encode'

There are two problems with this code.

1)You have created a "stream of bytes" of the ndef message but the SimpleMFRC522 package expects a Unicode String and encodes it to convert it to a "stream of bytes", so basically as it is already a "stream of bytes" it cannot be double encoded.

2)The Mifare S50 chip is a Mifare Classic family chip and these are a proprietary format and don't conform to the NFC Forum Standards and thus don't have a Standards defined way to store Ndef data on them (You need to do more than write a raw set of bytes to the memory to chip for it to be recognised as the data being the Ndef format of data).

For NFC standard compliant/compatible chips there are a number of defined types of how the Ndef data is stored on each type.

While NXP has defined a proprietary way to store Ndef data on a Mifare Classic chip, but the "SimpleMFRC522" is very simple indeed and does not have the code to write the Standard Ndef messages never mind the non standard proprietary methods.

I also note that while the iPhone hardware can read Mifare Classic cards at the low level, I'm not sure it has implemented the Ndef data format on top as well. I do know that some Android phones don't support the proprietary Mifare Classic Hardware at all.

I suggest you read https://www.nxp.com/docs/en/application-note/AN1305.pdf as this is the proprietary format definition on how to store NDEF data on these cards

Unfortunately the RC522 is very old and has limited capabilities and thus is not well supported by more capable Python NFC modules.

You might be better off with https://github.com/ondryaso/pi-rc522 as that at least allows you to write raw bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM