Convenient Combine wrapper for CoreBluetooth
CBCBluetooth
Requirements
- iOS 13 and above
Usage Example
Import dependenices:
import Combine
import CoreBluetooth
import CBCBluetooth
- Scanning and receiving single value from public services:
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)
- Connecting to particular peripheral by 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)
- Observe 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)
Installation
Cocoapods
CBCBluetooth is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CBCBluetooth'
Swift Package Manager
- Right click in the Project Navigator
- Select “Add Packages…”
- Search for
https://github.com/eugene-software/CBCBluetooth.git
Author
Eugene Software
License
CBCBluetooth is available under the MIT license. See the LICENSE file for more info.