使用 Swift 或 Objective-C 向 ACR122U 发送命令

Sending commands to ACR122U with Swift or Objective-C

我正在编写一个 macOS 应用程序,它将提供使用 ACR122u usb reader 读取和写入数据到 nfc 标签的功能。我很难找到如何在此环境中向设备发送命令的示例。我遇到了 this 问题,但似乎无法将其适应 Swift 或 Objective C。任何帮助将不胜感激。

这是我目前所知道的,但我似乎无法理解如何发送字节数组。

func _getSerialNumber(device: USBDevice) throws -> String {
    
    // get device interface from our pointer
    guard let deviceInterface = device.deviceInterfacePtrPtr?.pointee?.pointee else {
        throw NSError(domain: domain, code: 404, userInfo: [NSLocalizedDescriptionKey: "device not found"])
    }
    
    // create usb device request
    let length: Int = 5
    var requestPtr: [UInt8] = [UInt8](repeating: 0, count: Int(length))
    let type = USBmakebmRequestType(direction: kUSBIn, type: kUSBStandard, recipient: kUSBDevice)
    var request = IOUSBDevRequest(bmRequestType: type,
                                      bRequest: UInt8(kUSBRqGetDescriptor),
                                      wValue: 0,
                                      wIndex: 0,
                                      wLength: UInt16(length),
                                      pData: &requestPtr,
                                      wLenDone: 255)
    
    let kr: Int32 = deviceInterface.DeviceRequest(device.deviceInterfacePtrPtr, &request)
    if kr == -536870163 {
        throw NSError(domain: domain, code: Int(kr), userInfo: [NSLocalizedDescriptionKey: "unable to send request"])
    }
    if kr != kIOReturnSuccess {
        throw NSError(domain: domain, code: Int(kr), userInfo: [NSLocalizedDescriptionKey: "unknown error occurred"])
    }
    
    guard let string = String(bytes: requestPtr, encoding: .utf8) else {
        throw NSError(domain: domain, code: 500, userInfo: [NSLocalizedDescriptionKey: "unable to decode device response"])
    }
    return string
}

事实证明,使用 CryptoTokenKit 比我之前发现的更合适。您将 reader 初始化为 TKSmartCardSlot,并将卡片初始化为 TKSmartCard。然后通过传输函数将字节数组传输到 TKSmartCard。如果有人需要有关此工作流程的更多信息,请发送私信。

https://developer.apple.com/documentation/cryptotokenkit/tksmartcardslot