nfcpy / nfcpy

A Python module to read/write NFC tags or communicate with another NFC device.
European Union Public License 1.1
578 stars 146 forks source link

Formatting a blank NFC card #202

Open mb-zaba opened 2 years ago

mb-zaba commented 2 years ago

Hello, I try to format a blank NFC card to the NDEF format, but it doesn't seem to work and I'm really confused why. I use a ACS ACR122U reader. The cards seem to be of Type 2, I think. Here is my program :

import nfc
import ndef
import sys

class NFCtool:
    def __init__(self, device):
        print("Place the NFC Card on the device.")
        self.clf = nfc.ContactlessFrontend(device)
        self.tag = self.clf.connect(rdwr={'on-connect': lambda tag: False})

    def read_nfc(self):
        records = self.tag.ndef
        print("Readable: " + records.is_readable())
        print("Writeable: " + records.is_writeable())
        # data = records.text.split('\n')
        self.clf.close()
        return records

    def write_nfc(self):
        record = ndef.UriRecord("https://zaba.dev/")
        print(record.iri)
        print(record.uri)
        formatting = self.tag.format()
        print(formatting)
        test = b''.join(ndef.message_encoder([record]))
        self.clf.close()
        return "Data written."

if __name__ == '__main__':
    if not isinstance(sys.argv[1], str):
        raise Exception("Missing argument")
    nfct = NFCtool(sys.argv[1])
    # print(nfct.read_nfc())
    print(nfct.write_nfc())

this is the output :


Place the NFC Card on the device.
https://zaba.dev/
https://zaba.dev/
False
Data written.```
nehpetsde commented 2 years ago

Low-level formatting can only be supported for recognized card types; generally Type 2 cards with existing NDEF management data can be formatted to blank state. You'll really need to find out what card you have and maybe use a vendor app like NXP Tag Writer to low-level format.