Post

Replies

Boosts

Views

Activity

NFC CORE PROTOCOL ReadNDEF and WriteNDEF in the same session
In my application I want receive data from NFC Tag, modify this data and write it back to Tag.NFC PROTOCOL has 5 stages to do:-isAvailable-QueryStatus-ReadNDEF-WriteNDEF-WriteLockWhen I modified text payload after read, this modification cant see by WriteNDEF(myMessage), and into tag writes message which i initialised at QueryStatus stage.Important strings 08, 18 and 50After using command in string 50, i have modifyed and packed to message payload, but into tag writed other one from string 08 or 18. func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) { let tag = tags.first! var password: UInt16 = 0 var mess: String = "" var ToSend: String = "http://" //somehttp var textPayload = NFCNDEFPayload.wellKnownTypeURIPayload(string: ToSend) print("Payload -1 ->\(textPayload)") var myMessage = NFCNDEFMessage(records: [textPayload!]) //When session was finished, writed this one print("Packed! -1 ->\(myMessage)") // 3 session.connect(to: tag) { (error: Error?) in if error != nil { session.restartPolling() } } // 4 tag.queryNDEFStatus() { (status: NFCNDEFStatus, capacity: Int, error: Error?) in // myMessage = NFCNDEFMessage(records: [textPayload!]) //or this one, if it wiil be here if error != nil { session.invalidate(errorMessage: "Fail to determine NDEF status. Please try again.") return } if status == .readOnly { session.invalidate(errorMessage: "Tag isnt writable") } else if status == .readWrite { // 5 print ("READ-WRITE!") } else { session.invalidate(errorMessage: "Isnt NDEF.") } } //tag.queryNDEFStatus print("before read") tag.readNDEF(completionHandler: { (message: NFCNDEFMessage?, error: Error?) in print("inside read") var statusMessage: String if nil != error || nil == message { statusMessage = "Fail to read NDEF from tag" print(statusMessage) } else { statusMessage = "Found 1 NDEF message" print(statusMessage) let payload = message!.records.first mess = String(data: payload!.payload.advanced(by: 1), encoding: .ascii)! //some modification of readed text //and assignment to String toSend textPayload = NFCNDEFPayload.wellKnownTypeURIPayload(string: ToSend) // print("ToSend -2: \(ToSend)") print("PayLoad -2 ->\(textPayload)") myMessage = NFCNDEFMessage(records: [textPayload!]) // BUT NOT THIS ONE !! :( print("Packed! -2 -> \(myMessage)") //session.alertMessage = statusMessage } }) //tag.readNDEF print("before write") tag.writeNDEF(myMessage) { (error: Error?) in if error != nil { session.invalidate(errorMessage: "Update tag failed. Please try again.") print("Update tag failed. Please try again.") } else { print("Packed! -3 -> \(myMessage)") session.alertMessage = "Good Job-s!" print ("Good Job-s!") // 6 session.invalidate() } } //tag.writeNDEF } }
1
0
1.5k
Feb ’20