-
Notifications
You must be signed in to change notification settings - Fork 377
Migrating to 5.x
paweljaneczek edited this page Jun 22, 2018
·
2 revisions
Guide for migrating to RxBluetothKit 5
RxBlutoothKit starting from version 5.0.0 changes a lot in API. Here is a list of main changes that occured:
-
BluetoothManagerrenamed toCentralManager- with this change we have unified naming withCoreBluetooth. This change also gives us possibility to add easilyPeripheralManagersupport. - Changed returns from
Observable<>toSinglewhere applies. - Removed
BluetoothManager.listenOnRestoredStateand instead of this added newCentralManager.init(queue: DispatchQueue, options: [String: AnyObject]?, onWillRestoreState: OnWillRestoreState?)initializer - removed support for calling
CentralManager.scanForPeripheralswhen scanning is ongoing. Before we were doing queuing or sharing of new call but we had numerous issues with such approach. From now on, if there is ongoing scanObservablewill immediately finish withBluettothError.scanInProgresserror. -
BluetoothManager.rx_staterenamed toCentralManager.observeState. In addition to itCentralManager.observeStateis no more starting with current state value. -
BluetoothManager.connectrenamed toCentralManager.establishConnection. RemovedBluetoothManager.cancelPeripheralConnectionandPeripheral.cancelConnection. We have changed way of connecting to peripheral in more reactive way.CentralManager.establishConnectionis now also canceling connection on it's dispose. Due to that chanes it is now not possible to callCentralManager.establishConnectionon device that is already connected (the only exception of this behaviour is for devices that we get from state restoration). -
CentralManager.retrieveConnectedPeripheralsandCentralManager.retrievePeripheralsare now returning[Peripheral]instead ofObservable<[Peripheral]>. -
BluetoothManager.monitorConnectionrenamed toCentralManager.observeConnect. -
BluetoothManager.monitorDisconnectionrenamed toCentralManager.observeDisconnect. - all
Peripheral.monitorWritemethods renamed toPeripheral.observeWrite. - all
Peripheral.monitorValueUpdatemethods renamed toPeripheral.observeValueUpdate. -
Peripheral.monitorNameUpdaterenamed toPeripheral.observeNameUpdate. -
Peripheral.monitorServicesModificationrenamed toPeripheral.observeServicesModification. -
Peripheral.observeValueUpdateAndSetNotificationadded,Peripheral.setNotifyValueandPeripheral.setNotificationAndMonitorUpdatesremoved. From now on is seting notificaiton on subscription and unseting it on disposing. In addition it is possible to set more observables for same characteristic - it will work in a way that dispose will only happen when there are no observables for characteristic.
-let stateObservable = manager.rx_state
+let stateObservable = manager.observeState()-manager.rx_state
+manager.observeState()
+ .startWith(manager.state)
.filter { $0 == .poweredOn }
.timeout(3.0, scheduler)
.take(1)
- .flatMap { manager.scanForPeripherals(withServices: [serviceId]) }
+ .flatMap { _ in manager.scanForPeripherals(withServices: [serviceId]) } manager.scanForPeripherals(withServices: [serviceId]).take(1)
- .flatMap { $0.peripheral.connect() }
+ .flatMap { $0.peripheral.establishConnection() }
.subscribe(onNext: { peripheral in
print("Connected to: \(peripheral)")
})-peripheral.connect()
- .flatMap { $0.discoverServices([serviceId]) }
+peripheral.establishConnection()
+ .flatMap { $0.discoverServices([serviceId]) }.asObservable()
.flatMap { Observable.from($0) }-peripheral.connect()
- .flatMap { $0.discoverServices([serviceId]) }
+peripheral.establishConnection()
+ .flatMap { $0.discoverServices([serviceId]) }.asObservable()
.flatMap { Observable.from($0) }
- .flatMap { $0.discoverCharacteristics([characteristicId])}
+ .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
.flatMap { Observable.from($0) }-peripheral.connect()
- .flatMap { $0.discoverServices([serviceId]) }
+peripheral.establishConnection()
+ .flatMap { $0.discoverServices([serviceId]) }.asObservable()
.flatMap { Observable.from($0) }
- .flatMap { $0.discoverCharacteristics([characteristicId])}
+ .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
.flatMap { Observable.from($0) }-characteristic.setNotificationAndMonitorUpdates()
+let disposable = characteristic.observeValueUpdateAndSetNotification()
.subscribe(onNext: {
let newValue = $0.value
})-characteristic.setNotifyValue(false)
- .subscribe(onNext: { characteristic in
- //Notification are now disabled.
- })
+disposable.dispose()-peripheral.connect()
- .flatMap { Observable.from($0.discoverServices([serviceId])) }
- .flatMap { Observable.from($0.discoverCharacteristics([characteristicId])}
- .flatMap { $0.readValue }
- .subscribe(onNext: {
- let data = $0.value
- })
+peripheral.establishConnection()
+ .flatMap { $0.discoverServices([serviceId]) }.asObservable()
+ .flatMap { Observable.from($0) }
+ .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
+ .flatMap { Observable.from($0) }
+ .flatMap { $0.readValue() }
+ .subscribe(onNext: {
+ let data = $0.value
+ })