RxHeartRateMonitors is a lightweight layer on top of RxSwift, RxBluetoothKit and Core Bluetooth to interact with BTLE Heart Rate Monitors.
It allows you to communicate with Heart Rate Monitors in a seamless way.
-
Connect to BTLE heart rate monitors avoiding the complexities of CoreBluetooth.
-
No need to parse raw data.
-
No need to care about services and characteristics.
-
Extensible. If you want to connect to other types of devices, like speedometers, you can create your own central.
See some demonstration code bellow. Also, make sure you checkout the example app. It contains the usual code to interact with monitors: list, connect, disconnect, auto-connect.
Read this medium post I wrote about RxHeartRateMonitors
Found a bug? open an issue on GitHub.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Important:
-
Specify a bundle identifier, Team and provisioning profiles to run.
-
You will need an actual device to connect to HR monitors.
Swift 4.0
RxHeartRateMonitors is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RxHeartRateMonitors'
import RxHeartRateMonitors
let central = HeartRateMonitorCentral()
central.state
.map{"Bluetooth is \($0.isOn ? "ON" : "OFF")"}
.asDriver(onErrorDriveWith: .never())
.drive(self.stateLabel.rx.text)
.disposed(by: disposeBag)
private func bindMonitors(){
self.central.monitors
.bind(to: self.table.rx.items) { (table, row, heartRateMonitor) in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = heartRateMonitor.name
return cell
}
.disposed(by: disposeBag)
}
self.heartRateMonitor
.connect()
.subscribe()
.disposed(by: disposeBag)
self.heartRateMonitor
.disconnect()
.subscribe()
.disposed(by: disposeBag)
self.heartRateMonitor
.heartRate
.map{$0.description}
.asDriver(onErrorJustReturn: "N/A")
.drive(self.heartRateLabel.rx.text)
.disposed(by: disposeBag)
self.heartRateMonitor
.monitoredState
.asDriver(onErrorJustReturn: .disconnected)
.map{$0.description}
.drive(self.stateLabel.rx.text)
.disposed(by: disposeBag)
let monitor : Observable<HeartRateMonitor> = central.whenOnlineConnectToFirstAvailablePeripheral()
Leandro Perez, leandromperez@gmail.com
RxHeartRateMonitors is available under the MIT license. See the LICENSE file for more info.