How to read serial number from NFC tag?

616 views Asked by At

There is how I read NFC tag info:

func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) {
    
    
    // Connect to the found tag and perform NDEF message reading
    let tag = tags.first!
    
    
    session.connect(to: tag, completionHandler: { (error: Error?) in
        
        tag.queryNDEFStatus(completionHandler: { (ndefStatus: NFCNDEFStatus, capacity: Int, error: Error?) in

            tag.readNDEF(completionHandler: { [self] (message: NFCNDEFMessage?, error: Error?) in
                
                for record in message!.records {
                    print("Type name: \(record.typeNameFormat)")
                    print("Payload size: \(record.payload)")
                    print("Type size: \(record.type)")
                    print("Identifier: \(record.identifier)")

                    print("Payload: " + String(data:record.payload, encoding: .utf8)!)
                    
                }
                
                
                session.invalidate()
            })
        })
    })
}

But identifier is always 0 bytes and empty. How I can read kind of unique identifier or serial number?

1

There are 1 answers

7
Andrew On

NFC tags don't have a unique identifier or serial number, they have an ID that is not guaranteed to be unique.

The Ndef record.identifier you are printing from the Ndef spec is:-

The optional payload identifier allows user applications to identify the payload carried within an NDEF record

It is an identifier to help apps determine what type of data is stored in the Ndef structure, it supplements the Payload Type.

To get the non unique ID try printing tag.identifier this might return a hex string. But because this is non unique, it should not be used for anything related to security (for some NFC devices the ID is a random hex number, or it can be programmed to be a specific hex number)