【问题标题】:"Tag connection lost" error when reading a Mifare Ultralight NFC tag on iOS 13在 iOS 13 上读取 Mifare Ultralight NFC 标签时出现“标签连接丢失”错误
【发布时间】:2020-06-10 13:50:19
【问题描述】:

在发现并连接到 Mifare Ultralight 标签(更具体地说,EnOcean PTM 215B)后,我正在尝试使用 NFCMifareTag.sendMifareCommand 方法读取它的页面。问题是我尝试发送的所有命令都会导致“标记连接丢失”错误,这很奇怪,因为我刚刚成功连接到它。

(简化的)代码如下:

// tag has been determined to be of type NFCMifareTag earlier
session.connect(to: tag) { (error: Error?) in
  if (error != nil) {
    return
  }

  print("Connected to the tag")

  let data: [UInt8] = [0x30, 0x04, 0xEE, 0x26] // READ page 4 + CRC
  let dataPacket = Data(bytes: data, count: data.count)

  tag.sendMifareCommand(
    commandPacket: dataPacket,
    completionHandler: { (response: Data?, error: Error?) in
      if nil != error {
        return // <-- "Tag connection lost" error
      }
      // Handle the data as the operation was successful
    }
  )
}

我会很感激任何关于这种行为可能是什么原因的指针和/或想法。如前所述,我尝试了各种不同的数据包,但都完全一样。我还尝试了多种不同的手机来消除硬件问题。该支持刚刚添加到 iOS 13 中,因此我在网上找不到任何可以使用 sendMifareUltralight 命令的示例。

【问题讨论】:

    标签: ios swift nfc ios13 mifare


    【解决方案1】:

    根据 API (CoreNFC/NFCMiFareTag) 将自动计算并插入 CRC。因此,在您的情况下,您只需发送[0x30, 0x04] 即可读取第 4 至第 7 页,读取命令 0x30 将读取 4 页,您将获得 16 个字节。

     /**
     * @method sendMiFareCommand:completionHandler:
     *
     * @param command           The complete MiFare command.  CRC bytes are calculated and inserted automatically to the provided packet data frame.
     * @param completionHandler Completion handler called when the operation is completed.  error is nil if operation succeeds. A @link NFCErrorDomain @link/ error
     *                          is returned when there is a communication issue with the tag. Successfully read data blocks will be returned from the NSData object.
     *
     * @discussion              Send native MIFARE command to a tag.  Support MIFARE UltraLight, Plus, and DESFire products.
     *                          Crypto1 protocol is not supported.  Command chainning is handled internally by the method and the full response composed of the
     *                          individual fragment is returned in the completion handler.
     */
    @available(iOS 13.0, *)
    func sendMiFareCommand(commandPacket command: Data, completionHandler: @escaping (Data, Error?) -> Void)
    

    【讨论】:

    • 确实可以在没有 CRC 的情况下工作。 Apple's developer portal 上的文档正好相反。我从来没有完全理解为什么 API 用户需要计算,但是对于 Apple,你永远不会理解其中的推理。无论如何,非常感谢你。我花了几周的时间用这个把头撞在墙上。
    • 同意,这对我们来说也是一个很大的痛苦。也很难找到任何示例或教程。
    • 谢谢@wint,这对我有用。
    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2019-10-10
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2012-11-30
    相关资源
    最近更新 更多