CoreNFC 写入 iCode Slix2 标签似乎不稳定

问题描述 投票:0回答:1

我正在使用

CoreNFC
上的
iOS
来刷新一些 iCode Slix2 NFC 标签。

我遇到的问题是,有时我会成功地完成我的写入,但大多数时候我都会遇到以下错误:

Error Domain=NFCError Code=100 "Tag connection lost" UserInfo={NSLocalizedDescription=Tag connection lost}

Error Domain=NFCError Code=102 "Tag response error" UserInfo={NSLocalizedDescription=Tag response error, ISO15693TagResponseErrorCode=15}

Error Domain=NFCError Code=401 "Stack Error" UserInfo={NSLocalizedDescription=Stack Error, NSUnderlyingError=0x3017960d0 {Error Domain=nfcd Code=21 "writeNdefData:toTag:nLengthOptimization:error::403" UserInfo={Line=403, NSLocalizedDescription=Connection Closed, NSDebugDescription=writeNdefData:toTag:nLengthOptimization:error::403, Method=writeNdefData:toTag:nLengthOptimization:error:}}}

作为我的一篇文章的示例,它是完全擦除标签。

    func erase(tag: NFCISO15693Tag) async throws {
        print("🏷️", "ERASING TAG")
        alertMessage = "ERASING TAG..."
        
        let sysInfo = try await getSystemInfo(from: tag)
        let totalBlocks = sysInfo.totalBlocks - 1
        let blockSize = sysInfo.blockSize
        var blockNumber: UInt8 = 0
        
        func writeNextBlock() async throws {
            if blockNumber < totalBlocks {
                
                alertMessage = "ERASING TAG \(blockNumber)/\(totalBlocks)"
                
                let blankData = Data(repeating: 0x00, count: blockSize)
                try await write(data: blankData, to: tag, at: blockNumber)
                
                // Move to the next block
                blockNumber += 1
                try await writeNextBlock()
            } else {
                // All blocks have been erased
                alertMessage = "Tag erased successfully!"
            }
        }
        
        // Start writing to the first block
        try await writeNextBlock()
    }

    func write(data: Data, to tag: NFCISO15693Tag, at block: UInt8) async throws {
        print("🏷️", "Writing data:", data, data.hexEncodedString(.upperCase), "at block:", block)
        
        try await tag.writeSingleBlock(requestFlags: [.highDataRate], blockNumber: block, dataBlock: data)
    }
@MainActor
    private func eraseTag() {
        
        session = NFCTagReaderSession(pollingOption: [.iso15693], delegate: self, queue: nil)
        session?.alertMessage = "Hold your iPhone near a new NFC tag."
        session?.begin()
    }

func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
        
        guard tags.count == 1 else {
            session.invalidate(errorMessage: "Can not write to more than one tag.")
            return
        }
        
        let currentTag = tags.first!
        guard case .iso15693(let iso15693Tag) = currentTag else {
            session.invalidate(errorMessage: "Tag is not iso15693")
            return
        }
        
        Task {
            do {
                try await session.connect(to: currentTag)
                print("Connected: ========\(currentTag.isAvailable)========")
                
                try await session.erase(tag: iso15693Tag)
                
                session.invalidate()
                
            } catch {
                print("❗️", error)
                session.invalidate(errorMessage: error.localizedDescription)
            }
        }
    }

很多时候,这不会完成写入标签的 79 个块的一半,但偶尔会成功。

我有时在读取时也会遇到错误,但是如果我使用 NFC Tools 应用程序,它会比我的应用程序读取更多芯片。我需要一些额外的参数或设置吗?或者也许我有线程问题?

如何找到

ISO15693TagResponseErrorCode=15
的含义?

ios swift nfc core-nfc
1个回答
0
投票

你可以尝试降低书写频率,虽然不知道有没有帮助。

func erase(tag: NFCISO15693Tag) async throws {
  print("🏷️", "ERASING TAG")
  alertMessage = "ERASING TAG..."
  
  let sysInfo = try await getSystemInfo(from: tag)
  let totalBlocks = sysInfo.totalBlocks - 1
  let blockSize = sysInfo.blockSize
  var blockNumber: UInt8 = 0
  
  func writeNextBlock() async throws {
    if blockNumber <= totalBlocks {
      alertMessage = "ERASING TAG \(blockNumber)/\(totalBlocks)"
      
      let blankData = Data(repeating: 0x00, count: blockSize)
      do {
        try await write(data: blankData, to: tag, at: blockNumber)
        
        try await Task.sleep(nanoseconds: 100_000_000)
        
        blockNumber += 1
        try await writeNextBlock()
      } catch {
        print("Write error: \(error), retrying...")
        try await Task.sleep(nanoseconds: 500_000_000)
        try await writeNextBlock()
      }
    } else {
      alertMessage = "Tag erased successfully!"
    }
  }
  
  try await writeNextBlock()
}
© www.soinside.com 2019 - 2024. All rights reserved.