diff --git a/ExampleApp/ExampleApp.xcodeproj/project.pbxproj b/ExampleApp/ExampleApp.xcodeproj/project.pbxproj index f3dc2926..fc290938 100644 --- a/ExampleApp/ExampleApp.xcodeproj/project.pbxproj +++ b/ExampleApp/ExampleApp.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 8D72C58A2539C65000456D1A /* RxBluetoothKit in Frameworks */ = {isa = PBXBuildFile; productRef = 8D72C5892539C65000456D1A /* RxBluetoothKit */; }; 8D72C58F2539C87300456D1A /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D72C58E2539C87300456D1A /* WelcomeView.swift */; }; + 8D896D472542D7C500FD5FE5 /* AlertPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D896D462542D7C500FD5FE5 /* AlertPresenter.swift */; }; 8DA476A625399CFA00B79A0A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DA476A525399CFA00B79A0A /* AppDelegate.swift */; }; 8DA476A825399CFA00B79A0A /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DA476A725399CFA00B79A0A /* SceneDelegate.swift */; }; 8DA476AA25399CFA00B79A0A /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DA476A925399CFA00B79A0A /* WelcomeViewController.swift */; }; @@ -22,6 +23,7 @@ /* Begin PBXFileReference section */ 8D72C58E2539C87300456D1A /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = ""; }; + 8D896D462542D7C500FD5FE5 /* AlertPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertPresenter.swift; sourceTree = ""; }; 8DA476A225399CFA00B79A0A /* ExampleApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8DA476A525399CFA00B79A0A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 8DA476A725399CFA00B79A0A /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -53,6 +55,7 @@ children = ( 8DA476A525399CFA00B79A0A /* AppDelegate.swift */, 8DA476A725399CFA00B79A0A /* SceneDelegate.swift */, + 8D896D462542D7C500FD5FE5 /* AlertPresenter.swift */, ); path = Application; sourceTree = ""; @@ -211,6 +214,7 @@ 8DB15AA6253D801C00DECF92 /* CentralViewController.swift in Sources */, 8DB15AAC253D938700DECF92 /* PeripheralViewController.swift in Sources */, 8DA476A625399CFA00B79A0A /* AppDelegate.swift in Sources */, + 8D896D472542D7C500FD5FE5 /* AlertPresenter.swift in Sources */, 8DA476A825399CFA00B79A0A /* SceneDelegate.swift in Sources */, 8DB15AAF253D93A800DECF92 /* PeripheralView.swift in Sources */, 8D72C58F2539C87300456D1A /* WelcomeView.swift in Sources */, diff --git a/ExampleApp/ExampleApp/Application/AlertPresenter.swift b/ExampleApp/ExampleApp/Application/AlertPresenter.swift new file mode 100644 index 00000000..e5767fe7 --- /dev/null +++ b/ExampleApp/ExampleApp/Application/AlertPresenter.swift @@ -0,0 +1,19 @@ +import UIKit + +class AlertPresenter { + + static func presentError(with message: String, on viewController: UIViewController?) { + let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert) + alert.addAction( + UIAlertAction( + title: "Ok", + style: .cancel, + handler: { action in + viewController?.dismiss(animated: true) + } + ) + ) + viewController?.present(alert, animated: true) + } + +} diff --git a/ExampleApp/ExampleApp/Screens/Central/CentralViewController.swift b/ExampleApp/ExampleApp/Screens/Central/CentralViewController.swift index 37d5a3df..12f587df 100644 --- a/ExampleApp/ExampleApp/Screens/Central/CentralViewController.swift +++ b/ExampleApp/ExampleApp/Screens/Central/CentralViewController.swift @@ -48,6 +48,7 @@ class CentralViewController: UIViewController { Observable.combineLatest(managerIsOn, Observable.just(manager)) { $1 } .flatMap { $0.scanForPeripherals(withServices: [serviceUuid]) } + .timeout(.seconds(7), scheduler: MainScheduler.instance) .take(1) .flatMap { $0.peripheral.establishConnection() } .do(onNext: { [weak self] _ in self?.centralView.readValueLabel.isEnabled = true }) @@ -56,10 +57,15 @@ class CentralViewController: UIViewController { .flatMap { $0.discoverCharacteristics([characteristicUuid]) } .flatMap { Observable.from($0) } .flatMap { $0.observeValueUpdateAndSetNotification() } - .subscribe(onNext: { [weak self] in - guard let data = $0.value, let string = String(data: data, encoding: .utf8) else { return } - self?.updateValue(string) - }) + .subscribe( + onNext: { [weak self] in + guard let data = $0.value, let string = String(data: data, encoding: .utf8) else { return } + self?.updateValue(string) + }, + onError: { [weak self] in + AlertPresenter.presentError(with: $0.localizedDescription, on: self?.navigationController) + } + ) .disposed(by: disposeBag) } diff --git a/ExampleApp/ExampleApp/Screens/Peripheral/PeripheralViewController.swift b/ExampleApp/ExampleApp/Screens/Peripheral/PeripheralViewController.swift index b5421b50..59db926b 100644 --- a/ExampleApp/ExampleApp/Screens/Peripheral/PeripheralViewController.swift +++ b/ExampleApp/ExampleApp/Screens/Peripheral/PeripheralViewController.swift @@ -77,10 +77,15 @@ class PeripheralViewController: UIViewController { Observable.combineLatest(managerIsOn, Observable.just(manager)) { $1 } .flatMap { $0.add(service) } .flatMap { [manager] in manager.startAdvertising($0.advertisingData) } - .subscribe(onNext: { [weak self] in - print("advertising started! \($0)") - self?.setUpdate(enabled: true) - }) + .subscribe( + onNext: { [weak self] in + print("advertising started! \($0)") + self?.setUpdate(enabled: true) + }, + onError: { [weak self] in + AlertPresenter.presentError(with: $0.localizedDescription, on: self?.navigationController) + } + ) .disposed(by: disposeBag) }