NFC Tag Data Reader with PyNFC

  • Share this:

Code introduction


This function uses the PyNFC library to read data from NFC tags. It first connects to the first available NFC device, then scans for all available tags. If a supported MIFARE tag is found, it reads the data from the tag and returns it. If the tag type is not supported or no tags are found, it returns None.


Technology Stack : PyNFC

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_tag_reader(tag):
    from nfc import NfcReader
    from nfc.tag import Tag

    def read_tag_data(tag):
        # Read data from the tag
        data = tag.read()
        return data

    # Create a NFC reader instance
    reader = NfcReader()

    # Connect to the first available NFC device
    reader.connect()

    # Iterate over the available tags
    for tag in reader.scan():
        # Check if the tag is supported
        if tag.type == 'MIFARE':
            # Read the data from the tag
            data = read_tag_data(tag)
            return data
        else:
            print("Tag type not supported: ", tag.type)

    reader.close()
    return None                
              
Tags: