Reading a Random NFC Tag with PyNFC

  • Share this:

Code introduction


This function uses the PyNFC library to read a random NFC tag from an NFC reader.


Technology Stack : PyNFC, NfcReader, Tag

Code Type : NFC Tag Reader

Code Difficulty : Intermediate


                
                    
import random
from nfc import NfcReader
from nfc.tag import Tag

def read_random_tag():
    # Initialize the NFC reader
    reader = NfcReader('usb:nxp_pn532')  # Assuming the reader is connected via USB and is a PN532
    reader.connect()
    
    # Read a random tag from the reader
    tag = reader.read()
    
    # Close the reader connection
    reader.close()
    
    # Return the tag
    return tag

# Code information