Skip to content

Commit

Permalink
Merge pull request #370 from radumvlad/add-ancsauthorized-functionality
Browse files Browse the repository at this point in the history
Added support for observing ancsAuthorized property on peripheral.
  • Loading branch information
minixT authored Aug 28, 2020
2 parents 08e6d0c + d22c48e commit 7c48869
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
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

0 comments on commit 7c48869

Please sign in to comment.