Polidea / RxBluetoothKit

iOS & OSX Bluetooth library for RxSwift
Apache License 2.0
1.41k stars 366 forks source link

Add initializer to CentralManager with CBCentralManager #364

Closed devMEremenko closed 3 years ago

devMEremenko commented 4 years ago

Is your feature request related to a problem? Please describe. I develop SDK (iOS/Cocoatouch framework) for custom earbuds used by iOS apps. RxBluetoothKit is used under the hood.

The main problem is that there is no possibility of retrieving peripherals. What? Why?

The app may use CoreBluetooth for personal needs. This means it creates CBCentralManager, discovers devices, and sets peripherals/passes identifiers to SDK to perform some operations.

RxBluetoothKit creates its own CBCentralManager behind the scene.

The problem is that data & events of CoreBluetooth are not shared between multiple instances of CBCentralManager.

If SDK calls retrievePeripherals(by ids:), observeConnect() or whatever functions using RxBluetoothKit (with own CBCentralManager) there is no effect.

Describe the solution you'd like I would like to add an additional initializer to CentralManager so the client can pass a CBCentralManager instance and eliminate a hidden dependency.

public init(centralManager: CBCentralManager) {
        let delegateWrapper = CBCentralManagerDelegateWrapper()
        let centralManager = CBCentralManager(delegate: delegateWrapper, queue: queue, options: options)
        self.init(
            centralManager: centralManager,
            delegateWrapper: delegateWrapper,
            peripheralProvider: PeripheralProvider(),
            connector: Connector(centralManager: centralManager, delegateWrapper: delegateWrapper)
        )
    }

Describe alternatives you've considered I understand that RxBlueetoothKit does not expose CoreBluetooth to clients, that's great 👍

I just want to mention a negative impact of hiding CBCentralManager inside of RxBluetoothKit:

Since this functionality may not be needed for all clients of RxBluetoothKit, we can create an additional CocoaPods subspec/module/whatever that contains this initializer.

For instance, Moya provides additional functionality such as Rx & Reactive versions.