Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for observing ancsAuthorized property on peripheral. #370

Merged
merged 6 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Source/CBCentralManagerDelegateWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CBCentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
let didConnectPeripheral = PublishSubject<CBPeripheral>()
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 }
Expand Down Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions Source/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool> {
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.
Expand Down
6 changes: 6 additions & 0 deletions Tests/Autogenerated/Mock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ class CBCentralManagerDelegateWrapperMock: NSObject , CBCentralManagerDelegate {
var didConnectPeripheral = PublishSubject<CBPeripheralMock>()
var didFailToConnectPeripheral = PublishSubject<(CBPeripheralMock, Error?)>()
var didDisconnectPeripheral = PublishSubject<(CBPeripheralMock, Error?)>()
var didUpdateANCSAuthorizationForPeripheral = PublishSubject<(CBPeripheralMock)>()

override init() {
}
Expand All @@ -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<BluetoothState>()
Expand Down
21 changes: 21 additions & 0 deletions Tests/Autogenerated/_CentralManager.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool> {
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.
Expand Down