diff --git a/.gitignore b/.gitignore index 0589c84..38287b2 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ DerivedData Pods Carthage +.swiftpm diff --git a/Framework/Source Files/Advertisement/AdvertisementService.swift b/Framework/Source Files/Advertisement/AdvertisementService.swift index eeb4d85..81b3d84 100644 --- a/Framework/Source Files/Advertisement/AdvertisementService.swift +++ b/Framework/Source Files/Advertisement/AdvertisementService.swift @@ -30,10 +30,10 @@ internal final class AdvertisementService: NSObject { internal var writeCallback: ((Characteristic, Data?) -> ())? /// Callback called upon upcoming errors. - private var errorHandler: ((AdvertisementError) -> ())? + private var errorHandler: ((AdvertisementError?) -> ())? /// Starts advertising peripheral with given configuration of services and characteristics. - internal func startAdvertising(_ peripheral: Peripheral, errorHandler: ((AdvertisementError) -> ())?) { + internal func startAdvertising(_ peripheral: Peripheral, errorHandler: ((AdvertisementError?) -> ())?) { self.peripheral = peripheral self.errorHandler = errorHandler peripheralManager.startAdvertising(peripheral.advertisementData?.combined()) @@ -81,6 +81,7 @@ extension AdvertisementService: CBPeripheralManagerDelegate { return } self.peripheral?.configuration.services.map({ $0.assignAdvertisementService() }).forEach(peripheralManager.add(_:)) + errorHandler?(nil) } /// - SeeAlso: `CBPeripheralManagerDelegate` diff --git a/Framework/Source Files/Advertisement/BluetoothAdvertisement.swift b/Framework/Source Files/Advertisement/BluetoothAdvertisement.swift index 5875572..c34444d 100644 --- a/Framework/Source Files/Advertisement/BluetoothAdvertisement.swift +++ b/Framework/Source Files/Advertisement/BluetoothAdvertisement.swift @@ -25,7 +25,7 @@ public final class BluetoothAdvertisement { /// - errorHandler: an error handler. Will be called only after unsuccessfull advertisement setup. /// - SeeAlso: `AdvertisementError` /// - SeeAlso: `Peripheral` - public func advertise(peripheral: Peripheral, errorHandler: ((AdvertisementError) -> ())?) { + public func advertise(peripheral: Peripheral, errorHandler: ((AdvertisementError?) -> ())?) { advertisementService.startAdvertising(peripheral, errorHandler: errorHandler) } diff --git a/Framework/Source Files/Connection/ConnectionService.swift b/Framework/Source Files/Connection/ConnectionService.swift index ad8852a..595e91e 100644 --- a/Framework/Source Files/Connection/ConnectionService.swift +++ b/Framework/Source Files/Connection/ConnectionService.swift @@ -84,18 +84,9 @@ extension ConnectionService { /// Starts connection with passed device. Connection result is passed in handler closure. internal func connect(_ peripheral: Peripheral, handler: @escaping (Peripheral, ConnectionError?) -> ()) { - if connectionHandler == nil { - connectionHandler = handler - } - do { - try centralManager.validateState() - peripherals.append(peripheral) - reloadScanning() - } catch let error { - if let error = error as? BluetoothError { - handler(peripheral, .bluetoothError(error)) - } - } + connectionHandler = handler + peripherals.append(peripheral) + reloadScanning() } /// Disconnects given device. diff --git a/Package.swift b/Package.swift index e327320..758cf76 100644 --- a/Package.swift +++ b/Package.swift @@ -3,7 +3,7 @@ let package = Package( name: "BlueSwift", - platforms: [ .iOS(.v10) ], + platforms: [ .iOS(.v10), .macOS(.v11) ], products: [ .library( name: "BlueSwift",