diff --git a/Source/CBCentralManagerDelegateWrapper.swift b/Source/CBCentralManagerDelegateWrapper.swift index beb24e15..c8ebda01 100644 --- a/Source/CBCentralManagerDelegateWrapper.swift +++ b/Source/CBCentralManagerDelegateWrapper.swift @@ -10,6 +10,7 @@ class CBCentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate { let didConnectPeripheral = PublishSubject() let didFailToConnectPeripheral = PublishSubject<(CBPeripheral, Error?)>() let didDisconnectPeripheral = PublishSubject<(CBPeripheral, Error?)>() + let didUpdateANCSAuthorizationForPeripheral = PublishSubject<(CBPeripheral)>() func centralManagerDidUpdateState(_ central: CBCentralManager) { guard let bleState = BluetoothState(rawValue: central.state.rawValue) else { return } @@ -59,4 +60,16 @@ class CBCentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate { """) didDisconnectPeripheral.onNext((peripheral, error)) } + + #if !os(macOS) + @available(iOS 13.0, watchOS 6.0, tvOS 13.0, *) + func centralManager(_ central: CBCentralManager, + didUpdateANCSAuthorizationFor peripheral: CBPeripheral) { + RxBluetoothKitLog.d(""" + \(central.logDescription) didUpdateANCSAuthorizationFor + (peripheral: \(peripheral.logDescription) + """) + didUpdateANCSAuthorizationForPeripheral.onNext(peripheral) + } + #endif } diff --git a/Source/CentralManager.swift b/Source/CentralManager.swift index 123d485f..dda31608 100644 --- a/Source/CentralManager.swift +++ b/Source/CentralManager.swift @@ -297,6 +297,27 @@ public class CentralManager: ManagerType { } } + // MARK: ANCS + + /// Emits boolean values according to ancsAuthorized property on a CBPeripheral. + /// + /// - parameter peripheral: `Peripheral` which is observed for ancsAuthorized chances. + /// - returns: Observable which emits next events when `ancsAuthorized` property changes on a peripheral. + #if !os(macOS) + @available(iOS 13.0, watchOS 6.0, tvOS 13.0, *) + public func observeANCSAuthorized(for peripheral: Peripheral) -> Observable { + let observable = delegateWrapper.didUpdateANCSAuthorizationForPeripheral + .asObservable() + .filter { $0 == peripheral.peripheral } + // ancsAuthorized is a Bool by default, but the testing framework + // will use Bool! instead. In order to support that we are converting + // to optional and unwrapping the value. + .map { ($0.ancsAuthorized as Bool?)! } + + return ensure(.poweredOn, observable: observable) + } + #endif + // MARK: Internal functions /// Ensure that specified `peripheral` is connected during subscription. diff --git a/Tests/Autogenerated/Mock.generated.swift b/Tests/Autogenerated/Mock.generated.swift index 7d361d62..2bd7d33b 100644 --- a/Tests/Autogenerated/Mock.generated.swift +++ b/Tests/Autogenerated/Mock.generated.swift @@ -574,6 +574,7 @@ class CBCentralManagerDelegateWrapperMock: NSObject , CBCentralManagerDelegate { var didConnectPeripheral = PublishSubject() var didFailToConnectPeripheral = PublishSubject<(CBPeripheralMock, Error?)>() var didDisconnectPeripheral = PublishSubject<(CBPeripheralMock, Error?)>() + var didUpdateANCSAuthorizationForPeripheral = PublishSubject<(CBPeripheralMock)>() override init() { } @@ -595,6 +596,11 @@ class CBCentralManagerDelegateWrapperMock: NSObject , CBCentralManagerDelegate { func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { } + + #if !os(macOS) + func centralManager(_ central: CBCentralManager, didUpdateANCSAuthorizationFor peripheral: CBPeripheral) { + } + #endif } class CBPeripheralManagerDelegateWrapperMock: NSObject , CBPeripheralManagerDelegate { var didUpdateState = PublishSubject() diff --git a/Tests/Autogenerated/_CentralManager.generated.swift b/Tests/Autogenerated/_CentralManager.generated.swift index c900acf0..d1f1e30f 100644 --- a/Tests/Autogenerated/_CentralManager.generated.swift +++ b/Tests/Autogenerated/_CentralManager.generated.swift @@ -298,6 +298,27 @@ class _CentralManager: _ManagerType { } } + // MARK: ANCS + + /// Emits boolean values according to ancsAuthorized property on a CBPeripheralMock. + /// + /// - parameter peripheral: `_Peripheral` which is observed for ancsAuthorized chances. + /// - returns: Observable which emits next events when `ancsAuthorized` property changes on a peripheral. + #if !os(macOS) + @available(iOS 13.0, watchOS 6.0, tvOS 13.0, *) + func observeANCSAuthorized(for peripheral: _Peripheral) -> Observable { + let observable = delegateWrapper.didUpdateANCSAuthorizationForPeripheral + .asObservable() + .filter { $0 == peripheral.peripheral } + // ancsAuthorized is a Bool by default, but the testing framework + // will use Bool! instead. In order to support that we are converting + // to optional and unwrapping the value. + .map { ($0.ancsAuthorized as Bool?)! } + + return ensure(.poweredOn, observable: observable) + } + #endif + // MARK: Internal functions /// Ensure that specified `peripheral` is connected during subscription.