CBCBluetooth
需求
- iOS 13 及以上版本
使用示例
导入依赖项
import Combine
import CoreBluetooth
import CBCBluetooth
- 扫描并接收来自公开服务的单个值
let manager = CBCCentralManagerFactory.create()
let service = UUID(uuidString:"SOME-SERVICE-UUID-STRING")
let characteristic = UUID(uuidString:"SOME-CHARACTERISTIC-UUID-STRING")
manager.startScan(with: [service])
.flatMap {
$0.discoverServices(with: [service])
}
.flatMap {
$0.discoverCharacteristics(with: [characteristic])
}
.flatMap {
$0.readValue()
}
.sink { completion in
print(completion)
} receiveValue: { response in
print(response.data)
}
.store(in: &cancellables)
- 通过 UUID 连接到特定的外围设备
let manager = CBCCentralManagerFactory.create()
let peripheralUUID = UUID(uuidString: "SOME-PERIPHERAL-UUID-STRING")!
manager.getPeripherals(with: [peripheralUUID])
.first()
.flatMap {
$0.connect()
}
.sink { completion in
print(completion)
} receiveValue: { peripheral in
print(peripheral)
}
.store(in: &cancellables)
- 观察 RSSI 值
let manager = CBCCentralManagerFactory.create()
let peripheralUUID = UUID(uuidString: "SOME-PERIPHERAL-UUID-STRING")!
manager.getPeripherals(with: [peripheralUUID])
.first()
.flatMap {
$0.connect()
}
.flatMap {
$0.observeRSSI()
}
.sink { completion in
print(completion)
} receiveValue: { rssi in
print(rssi)
}
.store(in: &cancellables)
安装
Cocoapods
CBCBluetooth 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行
pod 'CBCBluetooth'
Swift 包管理器
- 在项目导航器中右键点击
- 选择 "添加包..."
- 搜索
https://github.com/eugene-software/CBCBluetooth.git
作者
Eugene 软件
许可协议
CBCBluetooth 在 MIT 许可协议下提供。更多信息请参见 LICENSE 文件。