连接到NFCTagReaderSession NFCISO7816Tag标签并在iOS 13中发送命令

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

我正在开发NFCTagReaderSession,这是iOS 13中新提供的,我很想连接会话标签并发送apdu命令进行通信。

[当我调用connect属性时,它看起来很像15秒的连接时间,直到它连接时(发出哔哔声),它显示一条错误消息

NFCError Code = 201“会话超时”。

每次连接卡时都在呼叫tagReaderSession:didInvalidateWithError,并且我无法发送apdu命令。

我尝试连接并发送apudu命令的代码

var nfcSession: NFCTagReaderSession?  
nfcSession = NFCTagReaderSession(pollingOption: [.iso14443],      delegate: self, queue: DispatchQueue.main)
nfcSession?.alertMessage = "Hold your iPhone near an NFC."
nfcSession?.begin()

// MARK: - NFCTagReaderSessionDelegate

public func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
    print("tagReaderSessionDidBecomeActive")
}

public func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
    print( "tagReaderSession:didInvalidateWithError - \(error)" )
}

public func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {

let tag = tags.first!
var nfcTag7816: NFCISO7816Tag
switch tags.first! {
 case let .iso7816(tag):
     nfcTag7816 = tag
   @unknown default :
        session.invalidate(errorMessage: "Tag not valid.")
             return
   }

 session.connect(to: tags.first!) { (error: Error?) in
     if error != nil {
        session.invalidate(errorMessage: "Connection error. Please try again.")
           return
    }
    else {
        let myAPDU = NFCISO7816APDU(instructionClass:0, instructionCode:0xB0, p1Parameter:0, p2Parameter:0, data: Data(), expectedResponseLength:16)
         nfcTag7816.sendCommand(apdu: myAPDU) { (response: Data, sw1: UInt8, sw2: UInt8, error: Error?)
                     in

        guard error != nil && !(sw1 == 0x90 && sw2 == 0) else {
               session.invalidate(errorMessage: "Applicationfailure")
            return
            }
          }
       }
   }
}

连接时发现错误:

tagReaderSession:didInvalidateWithError - Error Domain=NFCError Code=201 "Session timeout" UserInfo={NSLocalizedDescription=Session timeout}

请向我建议发生此错误的确切原因,以便我可以根据解决方案更改代码

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

我也在尝试同样的方法,并在20秒后收到错误会话超时。有人找到解决方案吗?

© www.soinside.com 2019 - 2024. All rights reserved.