BluetoothKit 0.4.0

BluetoothKit 0.4.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2016 年 12 月
SwiftSwift 版本3.0
SPM支持 SPM

Rasmus Taulborg Hummelmose 维护。



  • Rasmus Taulborg Hummelmose

BluetoothKit

使用 BLE 在 iOS 设备之间轻松通信。

背景

苹果在 CoreBluetooth API 方面做得相当不错,但它封装了整个 Bluetooth 4.0 LE 规范,要在不担心规范和 CoreBluetooth 栈内部工作的前提下完成简单的任务(如 iOS 设备之间发送和接收数据),可能需要做大量工作。

BluetoothKit 通过提供一个更简单、更现代、基于闭包的 API,所有这些都是用 Swift 实现的,来尝试解决这些问题。

功能

通用

  • 使用枚举实现更简洁的 Bluetooth LE 可用性定义。
  • Bluetooth LE 可用性观察,允许同时有多个观察者。

中心

  • 在给定的时间间隔内搜索远程外围设备。
  • 在指定的时间间隔内连续搜索远程外围设备,直到被中断,中间有延迟。
  • 在指定的时间间隔作为超时连接到远程外围设备。
  • 接收任何大小的数据,无需担心分块。

外围

  • 仅通过一个函数调用即可开始广播。
  • 向连接的远程中心发送任何大小的数据,无需担心分块。

要求

  • iOS 8.0+ / OSX 10.10+
  • Xcode 7.0+

安装

手动

将 BluetoothKit 项目添加到现有项目中,并将 BluetoothKit 添加为目标(s)的嵌入式二进制文件。

使用

以下是一些使用框架的示例。在仓库中,您将找到一个示例项目,该项目演示了框架在实际中的使用。该示例项目使用 SnapKitCryptoSwift,这两个都是非常好的项目。它们被包含在项目中,应该可以轻松构建。

常用

请确保在需要使用它的文件中导入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许可证发布。