ios - 在 iOS 13 上读取 Mifare Ultralight NFC 标签时出现“标签连接丢失”错误
问题描述
我正在尝试在发现并连接到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
命令的示例。
解决方案
根据 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)
推荐阅读
- python - 如果输入不是用户需要再次输入的Python代码?
- c# - 根据给定的 DayOfWeek 列表查找下一个 DateTime
- math.net - 为什么 ByVal 函数会改变我的变量?
- android - startScanning() 在 NativeScript App 中找不到任何设备
- python - 在 python 问题上导入和播放音乐:我做错了什么?
- c - 标记元素并将它们存储在 n 叉树中
- mysql - 如何在单个查询中获得零件总数、通过零件数和失败零件数
- java - 如何注入实现相同接口的两个不同类的两个实例?
- linux - 为什么我可以创建比“可用内存”/“线程堆栈大小”更多的线程?
- java - 部署war web应用程序时如何在Jboss 7.1中配置jndi引用名称workmanger和timer