NFC Send Mifare command and NDEF in same session NFCCore
Asked Answered
C

0

0

I'm trying to update a password protected ndef216 mifare tag. I send the authenticate command and then overwrite my pack&pwd values to verrify the authentication is successfull. then when i use mifaretag.writendef(payload) i get error 401 (ndefReaderSessionErrorTagUpdateFailure). i tried for days now but no success.

  func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
    let tag = tags.first!
    
    session.connect(to: tag) { (error: Error?) in
        
        if(error != nil) { print("Errors when connecting to session: \(String(describing: error))") }
        
        if case let NFCTag.miFare(mifareTag) = tag {

           Functions.PrintTagInfo(mifareTag)

           Functions.Authenticate(mifareTag, PACK: "0000", PWD: "AABBCCDD", callback: {
                    
                    Functions.SetPWDandPack(mifareTag, PACK: "0000",PWD: "AABBCCDD")
                    Functions.SetPWDandPack(mifareTag, PACK: "0000",PWD: "AABBCCDD")
                    Functions.SetPWDandPack(mifareTag, PACK: "0000",PWD: "AABBCCDD")

                        
                    Functions.SetPWDandPack(mifareTag, PACK: "0000",PWD: "AABBCCDD")

                    
                    mifareTag.queryNDEFStatus { (status, capacity, error) in
                                guard error == nil else {
                                    print(error!)
                                    session.invalidate()
                                    return
                                }

                                switch status {
                                case .readWrite:

                                        let url =  NFCNDEFPayload.wellKnownTypeURIPayload(url: URL(string: "https://test.com")!)!
                                       let message =  NFCNDEFMessage.init(records: [url])
                                        mifareTag.writeNDEF(message) { error in
                                            if(error != nil) {
                                                //self.console.text = "ERROR: \(error)"
                                                
                                            } else {
                                               // self.console.text = "SUCESS"

                                            }
                                            
                                            
                                            print(error)
                                            session.invalidate()
                                        }
                                    
                                case .readOnly:
                                    print("This tag can only be read.  :)")
                                    session.invalidate()
                                case .notSupported:
                                    print("This tag is not supported.  :(")
                                    session.invalidate()
                                @unknown default:
                                    print("I have no idea what just happened. 8O")
                                    session.invalidate()
                                }
                            }
               }) 
            }
        }
    }
}

im getting following output, i wrote a dedailed log function for debugging:

AUTHENTICATE_COMMAND data response:
sent:
Bin: 00011011 10101010 10111011 11001100 11011101 
HEX: 1B AA BB CC DD 
ASCII: G6q7zN0=
Bytes: [27, 170, 187, 204, 221] 
response:
Bin: 00000001 00000010 
HEX: 01 02 
ASCII: AQI=
Bytes: [1, 2] 


Authenticated sucessfully on Chip


WRITE_PWD_CMD data response:
sent:
Bin: 10100010 11100101 10101010 10111011 11001100 11011101 
HEX: A2 E5 AA BB CC DD 
ASCII: ouWqu8zd
Bytes: [162, 229, 170, 187, 204, 221] 
response:
Bin: 00001010 
HEX: 0A 
ASCII: Cg==
Bytes: [10] 
🟢ACK response = OK




WRITE_PACK_CMD data response:
sent:
Bin: 10100010 11100110 00000001 00000010 00000000 00000000 
HEX: A2 E6 01 02 00 00 
ASCII: ouYBAgAA
Bytes: [162, 230, 1, 2, 0, 0] 
response:
Bin: 00001010 
HEX: 0A 
ASCII: Cg==
Bytes: [10] 
🟢ACK response = OK



Capacity: 868 Bytes
This tag is read/write compatible!  :D
Optional(Error Domain=NFCError Code=401 "Stack Error" UserInfo={NSLocalizedDescription=Stack Error, NSUnderlyingError=0x282d0ca50 {Error Domain=nfcd Code=21 "Connection Closed" UserInfo={NSLocalizedDescription=Connection Closed}}})
User cancelled NFC reading dialog

The writeNDEF command is working fine on empty chips without password. The chip identifier is NOT empty.

Calcine answered 17/10, 2020 at 22:21 Comment(2)
it seems you are not the only one with this issue: developer.apple.com/forums/thread/122535Juliettejulina
I think instead of myFareTag.writeNDEF you need to use myFareTag.sendMyFareCommand and send a WriteRecord command to write your record.Juliettejulina

© 2022 - 2024 — McMap. All rights reserved.