这是 TapTrack 的 iOS SDK,用于 TappyBLE NFC 阅读器。
pod 'TCMPTappy'
要搜索 TappyBLE NFC 阅读器,您可以创建一个 TappyBleScanner
并调用其 startScan()
方法。
请注意,这个扫描器是为了方便提供的实用工具。对于更健壮的搜索,建议应用程序使用 CoreBluetooth 的 CBCentralManager
实现自己的搜索器。在下面的示例中,将 TappyCentralManagerProvider
传递给了 TappyBleScanner
。然而,您可以在您的应用程序中传递自己的中心管理器。
// Create the scanner.
let scanner: TappyBleScanner = TappyBleScanner(centralManager: TappyCentralManagerProvider.shared().centralManager)
添加一个 Tappy 找到监听器
为 TappyBleScanner
检测到 TappyBLE 设备时创建监听器。
func tappyBleFoundListener(tappyBleDevice: TappyBleDevice) {
scanner.stopScan()
// The getTappyBle(centralManager:device:) method does an additional check to ensure that the
// detected device is indeed a TappyBLE device and not another device using the same Bluetooth
// module. If it finds that the device is not a TappyBLE reader, it will return nil.
let tappyBle = TappyBle.getTappyBle(centralManager: TappyCentralManagerProvider.shared().centralManager, device: tappyBleDevice)
// Upwrap the optional value returned by getTappyBle(centralManager:device:).
if let tappyBleFound = tappyBle {
// You can set TappyBLE status listeners for connection and disconnection events.
tappyBleFound.setStatusListener(listener: tappyStatusListener)
// Note that you must maintain a strong reference to the tappyBleFound object to access
// it outside this function.
tappyBleFound.connect()
} else {
NSLog(String(format: "Failed to initialize the TappyBleCommunicator with %@ with ID %@",
arguments: [tappyBleDevice.name(), String(describing :tappyBleDevice.deviceId)]))
}
}
您可以使用 setTappyFoundListener(listener:)
将此监听器附加到 TappyBleScanner
。请注意,您一次只能设置一个 tappyFoundListener
监听器。
scanner.setTappyFoundListener(listener: tappyBleFoundListener)
开始扫描
要扫描设备,请调用扫描器的 startScan()
方法。
注意:虽然允许这样做,但在设置 tappyFoundListener
之前扫描 TappyBLE
没有多大意义,因为您的应用程序不会得到任何已发现 TappyBLE
设备的提醒。
// startScan() returns a Bool indicating whether the scan started successfully.
let scanStarted: Bool = scanner.startScan()
if !scanStarted {
NSLog("Bluetooth scanning could not be initialized.")
}
您可以调用 scanner.stopScan()
来停止扫描。建议您在发现感兴趣的设备后立即停止扫描。这有助于节省电池并遵循苹果推荐的蓝牙使用方法。
创建并发送命令
您可以使用库提供的构造函数创建 TCMP 命令。
// TCMP command for scanning a single NDEF tag. Using the empty constructor sets the default
// scan parameters: scanning for Mifare(R) tags with no timeout.
let scanCommand: TCMPMessage = ScanTagCommand()
// TCMP command for writing an NDEF text record using default parameters (no timeout and not
// locking the tag to read-only).
let writeCommand: TCMPMessage = WriteNDEFTextCommand(text: "hello world")
// TCMP command for writing an NDEF text record using custom parameters.
let writeCommand2: TCMPMessage = WriteNDEFTextCommand(timeout: 0x05, lockTag: LockingMode.DONT_LOCK_TAG, text: "hello world")
然后,您可以使用 sendMessage(message:)
方法将命令发送到 TappyBLE。
tappyBle.sendMessage(message: scanCommand)
接收响应
您可以通过调用正确的命令族解析器来解决响应。以下示例显示了一个使用 BasicNFCCommandResolver
解析从 Tappy 收到的响应的响应监听器。每个命令族都有自己的解析器,按需使用。
请注意:命令响应解析器在无法解析消息时抛出,因此必须在 do-catch 块中调用解析器。
func responseListener(tcmpResponse: TCMPMessage) {
var response: TCMPMessage
do {
response = try BasicNFCCommandResolver.resolveResponse(message: tcmpResponse)
if let response = response as? NDEFFoundResponse {
NSLog("NDEF messsage found: \(response.ndefMessage)")
} else if let response = response as? BasicNfcApplicationErrorMessage {
NSLog("Basic NFC error response: \(response.errorDescription)")
} else if {
// ...
// Add additional tests for responses you are interested in
}
} catch {
NSLog("Response is not part of the Basic NFC Command Family.")
// No matter what command family is being used, the TappyBLE may return system
// errors, such as communication errors or application errors.
do {
response = try SystemCommandResolver.resolveResponse(message: tcmpResponse)
if let response = response as? SystemErrorResponse {
NSLog("System communication error: \(response.getErrorDescription())")
} else if let response = response as? SystemApplicationErrorResponse {
NSLog("System application error: \(response.errorDescription)")
}
} catch {
NSLog("Error: response not recognized.")
}
}
}
您可以使用 setResponseListener(listener:)
将此监听器附加到 TappyBle
。请注意,您一次只能设置一个 responseListener
。
tappyBle.setResponseListener(listener: responseListener)
支持的功能
-
针对Tappy读取器发送和接收的所有数据封装和解除封装
-
支持与Core Bluetooth配合使用TappyBLE(部署目标设置为iOS v 8.0)
- 扫描TappyBLE读取器
- 连接到TappyBLE读取器
- 以TCMP格式发送命令和接收响应
-
支持的命令家族
- 基本NFC命令家族
- 系统命令家族
- 类型4标签命令家族
- NTAG 21x命令家族