"Tag connection lost" error when reading a Mifare Ultralight NFC tag on iOS 13

Roo*_*nen 1 nfc ios mifare swift ios13

I'm trying to read a page of a Mifare Ultralight tag (more specifically EnOcean PTM 215B) using NFCMifareTag.sendMifareCommand method after it has been discovered and connected onto. The problem is that all commands I've tried to send cause a "Tag connection lost" error which is quite odd as I've just successfully connected to it.

The (simplified) code looks like:

// 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
    }
  )
}
Run Code Online (Sandbox Code Playgroud)

I'd appreciate any pointers and/or ideas on what could be the reason for this behavior. As mentioned, I've tried various different data packets but all work exactly the same. I've also tried multiple different phones to eliminate hardware problems. The support was just added in iOS 13 and as such I couldn't find any examples online that would use the sendMifareUltralight command.

win*_*int 5

根据 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)
Run Code Online (Sandbox Code Playgroud)

  • 没有 CRC 确实可以工作。[Apple 开发者门户](https://developer.apple.com/documentation/corenfc/nfcmifaretag/3043838-sendmifarecommand?language=swift) 上的文档说明了完全相反的情况。我一直不太明白为什么 API 用户需要计算,但对于 Apple,你永远不会理解其中的推理。不管怎样,非常感谢你。我花了几个星期的时间用头撞墙。 (2认同)