-
Notifications
You must be signed in to change notification settings - Fork 369
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:
-
BluetoothManager
renamed toCentralManager
- with this change we have unified naming withCoreBluetooth
. This change also gives us possibility to add easilyPeripheralManager
support. - Changed returns from
Observable<>
toSingle
where applies. - Removed
BluetoothManager.listenOnRestoredState
and instead of this added newCentralManager.init(queue: DispatchQueue, options: [String: AnyObject]?, onWillRestoreState: OnWillRestoreState?)
initializer - removed support for calling
CentralManager.scanForPeripherals
when 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 scanObservable
will immediately finish withBluettothError.scanInProgress
error. -
BluetoothManager.rx_state
renamed toCentralManager.observeState
. In addition to itCentralManager.observeState
is no more starting with current state value. -
BluetoothManager.connect
renamed toCentralManager.establishConnection
. RemovedBluetoothManager.cancelPeripheralConnection
andPeripheral.cancelConnection
. We have changed way of connecting to peripheral in more reactive way.CentralManager.establishConnection
is now also canceling connection on it's dispose. Due to that chanes it is now not possible to callCentralManager.establishConnection
on device that is already connected (the only exception of this behaviour is for devices that we get from state restoration). -
CentralManager.retrieveConnectedPeripherals
andCentralManager.retrievePeripherals
are now returning[Peripheral]
instead ofObservable<[Peripheral]>
. -
BluetoothManager.monitorConnection
renamed toCentralManager.observeConnect
. -
BluetoothManager.monitorDisconnection
renamed toCentralManager.observeDisconnect
. - all
Peripheral.monitorWrite
methods renamed toPeripheral.observeWrite
. - all
Peripheral.monitorValueUpdate
methods renamed toPeripheral.observeValueUpdate
. -
Peripheral.monitorNameUpdate
renamed toPeripheral.observeNameUpdate
. -
Peripheral.monitorServicesModification
renamed toPeripheral.observeServicesModification
. -
Peripheral.observeValueUpdateAndSetNotification
added,Peripheral.setNotifyValue
andPeripheral.setNotificationAndMonitorUpdates
removed. 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
+ })