BluetoothKit:使用 BLE 在 iOS/OSX 设备之间通讯
BluetoothKit 可以在iOS设备之间通过 BLE 轻松通讯。
背景
苹果在CoreBluetooth API上的表现非常出色,但是因为它涵盖了整个蓝牙4.0低电量规格(Bluetooth 4.0 LE specification),不需要考虑CoreBluetooth栈规格和内部工作原理的情况下,实现一些简单的任务上需要很多的工作量,比如在iOS设备之间来回传输数据。
BluetoothKit 尝试通过Swift来实现一个更简单、新式、基于闭包的API来应对这个挑战
特征
通用
- 使用枚举更简洁地定义了BLE的可行性
- BLE的可行性观察允许同时有多个观察者
中心设备
- 指定时间间隔扫描周围设备
- 指定时间间隔和间歇时间,持续扫描周围设备,直到被打断
- 连接周围设备,并带有timeout时间
- 接收任何大小的数据,不需要担心分块
周围设备
- 只需要调用一个函数就开始广播
- 向连接的中心设备发送任何大小的数据,不需要担心分块的问题
所需
- iOS 8.0及以上/OSX 10.10及以上
- Xcode 7.0及以上
安装
CocoaPods
CocoaPods是Cocoa项目一个一个依赖项管理器
整合Bluetooth需要CocoaPods 0.38.2及以上版本,这个版本还增加了对Xcode7、Swift2.0、嵌入框架的支持。你可以通过下面的命令来安装
1 |
$ gem install cocoapods |
为了用CocoaPods在你的Xcode项目中整合进BluetoothKit,在你的Podfile中指明
1 2 3 4 5 |
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! pod 'BluetoothKit', '~> 0.2.0' |
然后运行以下命令
1 |
$ pod install |
Carthage
Carthage是一个分布式依赖项管理器,自动将框架添加进你的Cocoa应用中
你可以使用Homebrew来安装Carthage,命令如下
1 2 |
$ brew update $ brew install carthage |
为了用CocoaPods在你的Xcode项目中整合进BluetoothKit,在你的Cartfile中指明
1 |
github "rasmusth/BluetoothKit" ~> 0.2.0 |
手动
将BluetoothKit加入你现有的项目中并且将Bluetooth作为嵌入二进制(embedded binary)进行添加
使用
下面你可以找到这个框架如何使用的一些例子。在这个仓库中你会找到一个example文件,演示了这个框架一些实际的使用。这个示例中还使用到了SnapKit 和 CryptoSwift,这两个都是非常好的项目。他们捆绑进了项目中并且不需要其他操作就能运行。
通用
确保在需要使用的文件中引入了Bluetooth
1 |
import BluetoothKit |
周围设备
初始化一个BKPeripheral对象,设置相关配置,包括用来你的app的唯一标示符UUIDs、用于广播的可选的本地名。你可以在OSX终端使用命令“uuidgen”来生成UUIDs。
1 2 3 4 5 6 7 8 9 10 11 12 |
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. } |
向连接的远处中心设备传输数据
1 2 3 4 5 6 |
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对象,设置相关配置,包括你在BKPerpheral对象上设备的UUIDs
1 2 3 4 5 6 7 8 9 10 11 12 |
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秒钟
1 2 3 4 5 6 |
central.scanWithDuration(3, progressHandler: { newDiscoveries in // Handle newDiscoveries, [BKDiscovery]. }, completionHandler: { result, error in // Handle error. // If no error, handle result, [BKDiscovery]. }) |
一次扫描3秒,中间间歇3秒
1 2 3 4 5 6 7 8 9 10 |
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. }) |
连接一个周围设备,连接timeout时间为3秒
1 2 3 4 |
central.connect(remotePeripheral: peripherals[indexPath.row]) { remotePeripheral, error in // Handle error. // If no error, you're ready to receive data! } |
许可证
BluetoothKit 遵循 MIT 开源许可证发布。