导入依赖
import Combine
import CoreBluetooth
import CBCBluetooth
let manager = CBCCentralManagerFactory.create()
let service = "SOME-SERVICE-UUID-STRING"
let characteristic = "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)
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)
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)
CBCBluetooth 可通过 CocoaPods 获得。 要安装它,只需将以下行添加到你的 Podfile 中
pod 'CBCBluetooth'
https://github.com/eugene-software/CBCBluetooth.git
Eugene Software
CBCBluetooth 使用 MIT 许可证。 详情请参阅 LICENSE 文件。