测试已测试 | ✓ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016 年 12 月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Rasmus Taulborg Hummelmose 维护。
使用 BLE 在 iOS 设备之间轻松通信。
苹果在 CoreBluetooth API 方面做得相当不错,但它封装了整个 Bluetooth 4.0 LE 规范,要在不担心规范和 CoreBluetooth 栈内部工作的前提下完成简单的任务(如 iOS 设备之间发送和接收数据),可能需要做大量工作。
BluetoothKit 通过提供一个更简单、更现代、基于闭包的 API,所有这些都是用 Swift 实现的,来尝试解决这些问题。
将 BluetoothKit 项目添加到现有项目中,并将 BluetoothKit 添加为目标(s)的嵌入式二进制文件。
以下是一些使用框架的示例。在仓库中,您将找到一个示例项目,该项目演示了框架在实际中的使用。该示例项目使用 SnapKit 和 CryptoSwift,这两个都是非常好的项目。它们被包含在项目中,应该可以轻松构建。
请确保在需要使用它的文件中导入BluetoothKit框架。
import BluetoothKit
准备并启动一个BKPeripheral对象,该对象包含您应用程序的独特UUIDs,以及一个可选的本地名称,该名称将被广播。您可以使用“uuidgen”命令在OSX终端生成UUIDs。
let peripheral = BKPeripheral()
peripheral.delegate = self
do {
let serviceUUID = NSUUID(UUIDString: "6E6B5C64-FAF7-40AE-9C21-D4933AF45B23")!
let characteristicUUID = NSUUID(UUIDString: "477A2967-1FAB-4DC5-920A-DEE5DE685A3D")!
let localName = "My Cool Peripheral"
let configuration = BKPeripheralConfiguration(dataServiceUUID: serviceUUID, dataServiceCharacteristicUUID: characteristicUUID, localName: localName)
try peripheral.startWithConfiguration(configuration)
// You are now ready for incoming connections
} catch let error {
// Handle error.
}
向已连接的远程中心发送数据。
let data = "Hello beloved central!".dataUsingEncoding(NSUTF8StringEncoding)
let remoteCentral = peripheral.connectedRemoteCentrals.first! // Don't do this in the real world :]
peripheral.sendData(data, toRemoteCentral: remoteCentral) { data, remoteCentral, error in
// Handle error.
// If no error, the data was all sent!
}
准备并启动一个BKCentral对象,该对象包含配置BKPeripheral对象时使用的UUIDs。
let central = BKCentral()
central.delegate = self
central.addAvailabilityObserver(self)
do {
let serviceUUID = NSUUID(UUIDString: "6E6B5C64-FAF7-40AE-9C21-D4933AF45B23")!
let characteristicUUID = NSUUID(UUIDString: "477A2967-1FAB-4DC5-920A-DEE5DE685A3D")!
let configuration = BKConfiguration(dataServiceUUID: serviceUUID, dataServiceCharacteristicUUID: characteristicUUID)
try central.startWithConfiguration(configuration: configuration)
// Once the availability observer has been positively notified, you're ready to discover and connect to peripherals.
} catch let error {
// Handle error.
}
扫描3秒钟的外设。
central.scanWithDuration(3, progressHandler: { newDiscoveries in
// Handle newDiscoveries, [BKDiscovery].
}, completionHandler: { result, error in
// Handle error.
// If no error, handle result, [BKDiscovery].
})
每次扫描3秒钟,中间延迟3秒钟。
central.scanContinuouslyWithChangeHandler({ changes, discoveries in
// Handle changes to "availabile" discoveries, [BKDiscoveriesChange].
// Handle current "available" discoveries, [BKDiscovery].
// This is where you'd ie. update a table view.
}, stateHandler: { newState in
// Handle newState, BKCentral.ContinuousScanState.
// This is where you'd ie. start/stop an activity indicator.
}, duration: 3, inBetweenDelay: 3, errorHandler: { error in
// Handle error.
})
在3秒钟的连接尝试超时时间内连接外设。
central.connect(remotePeripheral: peripherals[indexPath.row]) { remotePeripheral, error in
// Handle error.
// If no error, you're ready to receive data!
}
BluetoothKit受MIT许可证发布。