SwiftBluetooth
一个用于构建 BLE 应用程序的开发框架。
示例
运行示例项目前,请先克隆仓库,然后在 Example 目录中运行 pod install
。
用法
中心
初始化 SBCentralManager
let central = SBCentralManager()
//or
let central = SBCentralManager.shared
是否带监听器启动
central.start(listener: self)
//or
central.start()
属性
state
isConnected
isConnecting
connectedPeripherals
foundPeripherals
isBluetoothAvailable
......
实现SBCenterListener
func central(_ central: SBCentralManager, available: Bool) {
}
func central(_ central: SBCentralManager, didChangeState: CenterState) {
}
func central(_ central: SBCentralManager, didConnect device: Peripheral) {
}
func central(_ central: SBCentralManager, onDisconnecting device: Peripheral) {
}
func central(_ central: SBCentralManager, onConnecting device: Peripheral) {
}
func central(_ central: SBCentralManager, didDisconnect device: Peripheral, error: Error?) {
}
func central(_ central: SBCentralManager, peripheral: BLEPeripheral, didRSSIChanged RSSI: NSNumber) {
}
func central(_ central: SBCentralManager, device: BLEPeripheral, characteristic: CBCharacteristic, didReceive data: Result<Data>) {
}
日志配置
SBCentralManager.enableLog = true
扫描
// !!! Scan will fail beofre bluetooth available.
central.scan(serviceUUIDs: nil,
allowDuplicates: false,
filter: { (peripheral) -> (Bool) in
// You can filter peripheral by name or other properties.
// return peripheral.name.contains("your custom bluetooth name.")
return true
}, sorter: { (peripheral1, peripheral2) -> (Bool) in
// You can sort peripheral by rssi or other properties.
return peripheral1.rssi < peripheral2.rssi
}) { (result) in
switch result {
case .success(let discoverPeripheral, let foundPeripheralList):
break
case .cancelled:
print("Scan cancelled.")
case .failure(let error):
print("Scan error: \(error?.localizedDescription ?? "")")
}
}
central.stopScan()
连接
central.connect(peripheral: discoverPeripheral,
timeoutInterval: 10,
callback: { (result) in
switch result {
case .success(let peripheral):
print("Connect periphera:\(peripheral.name).")
case .cancelled:
print("Connect cancelled.")
case .failure(let error):
print("Connect error: \(error?.localizedDescription ?? "")")
}
})
central.cancelConnecting()
central.disConnect(peripheral: peripheral) { (result) in
switch result {
case .success(let peripheral):
print("Disconnect periphera:\(peripheral.name).")
case .cancelled:
print("Disconnect cancelled.")
case .failure(let error):
print("Disconnect error: \(error?.localizedDescription ?? "")")
}
}
外围设备
属性
name
uuid
rssi
......
服务
peripheral.discoverServices(serviceUUIDs: nil, callback: { (result) in
switch result {
case .success(let services):
print("DiscoverServices:\(services).")
case .cancelled:
print("DiscoverServices cancelled.")
case .failure(let error):
print("DiscoverServices error: \(error?.localizedDescription ?? "")")
}
})
特性
peripheral.discoverCharacteristics(characteristicUUIDs: nil, service: service) { (result) in
switch result {
case .success(let characteristics):
print("DiscoverCharacteristics:\(String(describing: characteristics?.count)).")
case .cancelled:
print("Characteristics cancelled.")
case .failure(let error):
print("Characteristics error: \(error?.localizedDescription ?? "")")
}
}
写入值
peripheral.writeValue(data: data,
characteristic: characteristic,
type: .withResponse)
读取RSSI
peripheral.readRSSI()
安装
SwiftBluetooth可通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile中
pod 'SwiftBluetooth'
作者
CatchZeng, [email protected]
许可协议
SwiftBluetooth遵循MIT许可协议。有关更多信息,请参阅LICENSE文件。