|
| 1 | +# Guide for migrating to `RxBluetothKit` 5 |
| 2 | + |
| 3 | +### What has changed? |
| 4 | + |
| 5 | +`RxBlutoothKit` starting from version 5.0.0 changes a lot in API. Here is a list of main changes that occured: |
| 6 | + |
| 7 | +- `BluetoothManager` renamed to `CentralManager` - with this change we have unified naming with `CoreBluetooth`. This change also gives us possibility to add easily `PeripheralManager` support. |
| 8 | +- Changed returns from `Observable<>` to `Single` where applies. |
| 9 | +- Removed `BluetoothManager.listenOnRestoredState` and instead of this added new `CentralManager.init(queue: DispatchQueue, options: [String: AnyObject]?, onWillRestoreState: OnWillRestoreState?)` initializer |
| 10 | +- 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 scan `Observable` will immediately finish with `BluettothError.scanInProgress` error. |
| 11 | +- `BluetoothManager.rx_state` renamed to `CentralManager.observeState`. In addition to it `CentralManager.observeState` is no more starting with current state value. |
| 12 | +- `BluetoothManager.connect` renamed to `CentralManager.establishConnection`. Removed `BluetoothManager.cancelPeripheralConnection` and `Peripheral.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 call `CentralManager.establishConnection` on device that is already connected (the only exception of this behaviour is for devices that we get from state restoration). |
| 13 | +- `CentralManager.retrieveConnectedPeripherals` and `CentralManager.retrievePeripherals` are now returning `[Peripheral]` instead of `Observable<[Peripheral]>`. |
| 14 | +- `BluetoothManager.monitorConnection` renamed to `CentralManager.observeConnect`. |
| 15 | +- `BluetoothManager.monitorDisconnection` renamed to `CentralManager.observeDisconnect`. |
| 16 | +- all `Peripheral.monitorWrite` methods renamed to `Peripheral.observeWrite`. |
| 17 | +- all `Peripheral.monitorValueUpdate` methods renamed to `Peripheral.observeValueUpdate`. |
| 18 | +- `Peripheral.monitorNameUpdate` renamed to `Peripheral.observeNameUpdate`. |
| 19 | +- `Peripheral.monitorServicesModification` renamed to `Peripheral.observeServicesModification`. |
| 20 | +- `Peripheral.observeValueUpdateAndSetNotification` added, `Peripheral.setNotifyValue` and `Peripheral.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. |
| 21 | + |
| 22 | + |
| 23 | +### Diffs on `README.md` file |
| 24 | + |
| 25 | +```swift |
| 26 | +-let stateObservable = manager.rx_state |
| 27 | ++let stateObservable = manager.observeState() |
| 28 | +``` |
| 29 | + |
| 30 | +```swift |
| 31 | +-manager.rx_state |
| 32 | ++manager.observeState() |
| 33 | ++ .startWith(manager.state) |
| 34 | + .filter { $0 == .poweredOn } |
| 35 | + .timeout(3.0, scheduler) |
| 36 | + .take(1) |
| 37 | +- .flatMap { manager.scanForPeripherals(withServices: [serviceId]) } |
| 38 | ++ .flatMap { _ in manager.scanForPeripherals(withServices: [serviceId]) } |
| 39 | +``` |
| 40 | + |
| 41 | +```swift |
| 42 | + manager.scanForPeripherals(withServices: [serviceId]).take(1) |
| 43 | +- .flatMap { $0.peripheral.connect() } |
| 44 | ++ .flatMap { $0.peripheral.establishConnection() } |
| 45 | + .subscribe(onNext: { peripheral in |
| 46 | + print("Connected to: \(peripheral)") |
| 47 | + }) |
| 48 | +``` |
| 49 | + |
| 50 | +```swift |
| 51 | +-peripheral.connect() |
| 52 | +- .flatMap { $0.discoverServices([serviceId]) } |
| 53 | ++peripheral.establishConnection() |
| 54 | ++ .flatMap { $0.discoverServices([serviceId]) }.asObservable() |
| 55 | + .flatMap { Observable.from($0) } |
| 56 | +``` |
| 57 | + |
| 58 | +```swift |
| 59 | +-peripheral.connect() |
| 60 | +- .flatMap { $0.discoverServices([serviceId]) } |
| 61 | ++peripheral.establishConnection() |
| 62 | ++ .flatMap { $0.discoverServices([serviceId]) }.asObservable() |
| 63 | + .flatMap { Observable.from($0) } |
| 64 | +- .flatMap { $0.discoverCharacteristics([characteristicId])} |
| 65 | ++ .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable() |
| 66 | + .flatMap { Observable.from($0) } |
| 67 | +``` |
| 68 | + |
| 69 | +```swift |
| 70 | +-peripheral.connect() |
| 71 | +- .flatMap { $0.discoverServices([serviceId]) } |
| 72 | ++peripheral.establishConnection() |
| 73 | ++ .flatMap { $0.discoverServices([serviceId]) }.asObservable() |
| 74 | + .flatMap { Observable.from($0) } |
| 75 | +- .flatMap { $0.discoverCharacteristics([characteristicId])} |
| 76 | ++ .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable() |
| 77 | + .flatMap { Observable.from($0) } |
| 78 | +``` |
| 79 | + |
| 80 | +```swift |
| 81 | +-characteristic.setNotificationAndMonitorUpdates() |
| 82 | ++let disposable = characteristic.observeValueUpdateAndSetNotification() |
| 83 | + .subscribe(onNext: { |
| 84 | + let newValue = $0.value |
| 85 | + }) |
| 86 | +``` |
| 87 | + |
| 88 | +```swift |
| 89 | +-characteristic.setNotifyValue(false) |
| 90 | +- .subscribe(onNext: { characteristic in |
| 91 | +- //Notification are now disabled. |
| 92 | +- }) |
| 93 | ++disposable.dispose() |
| 94 | +``` |
| 95 | + |
| 96 | + ```swift |
| 97 | +-peripheral.connect() |
| 98 | +- .flatMap { Observable.from($0.discoverServices([serviceId])) } |
| 99 | +- .flatMap { Observable.from($0.discoverCharacteristics([characteristicId])} |
| 100 | +- .flatMap { $0.readValue } |
| 101 | +- .subscribe(onNext: { |
| 102 | +- let data = $0.value |
| 103 | +- }) |
| 104 | ++peripheral.establishConnection() |
| 105 | ++ .flatMap { $0.discoverServices([serviceId]) }.asObservable() |
| 106 | ++ .flatMap { Observable.from($0) } |
| 107 | ++ .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable() |
| 108 | ++ .flatMap { Observable.from($0) } |
| 109 | ++ .flatMap { $0.readValue() } |
| 110 | ++ .subscribe(onNext: { |
| 111 | ++ let data = $0.value |
| 112 | ++ }) |
| 113 | + ``` |
0 commit comments