user6165632
user6165632

Reputation: 11

Can a CBCentralManager extend custom Protocol? Can we override CBCentralManagerDelegate?

// Custom class

public protocol BluetoothManagerProtocol {
    var delegate: CBCentralManagerDelegate? {get set}
    //var state: CBCentralManagerState { get }

    func scanForPeripheralsWithServices(serviceUUIDs: [CBUUID]?, options: [String : AnyObject]?)
    func stopScan()
    func connectPeripheral(peripheral: CBPeripheral, options: [String : AnyObject]?)
    func connectPeripheral(peripheral: BluetoothPeripheral, options: [String : AnyObject]?)
}

extension CBCentralManager : BluetoothManagerProtocol {
    public func connectPeripheral(peripheral: CBPeripheral, options: [String : AnyObject]?) {
        //
    }

    public func scanForPeripheralsWithServices(serviceUUIDs: [CBUUID]?, options: [String : AnyObject]?) {
        //
    }

    public func connectPeripheral(peripheral: BluetoothPeripheral, options: [String : AnyObject]?) {
        guard let peripheral = peripheral as? CBPeripheral else {
            return
        }
        connectPeripheral(peripheral, options: options)
    }
}

extension CBCentralManagerDelegate{
    func centralManager(central: BluetoothManagerProtocol, didDiscoverPeripheral peripheral: BluetoothPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {}

func centralManager(central: BluetoothManagerProtocol,didConnectPeripheral peripheral:BluetoothPeripheral) {}

func centralManagerDidUpdateState(central: BluetoothManagerProtocol) {}

func centralManager(central: BluetoothManagerProtocol, didDisconnectPeripheral peripheral: BluetoothPeripheral, error: NSError?) {}
}

I just jumped in to advanced topics of Protocols and Delegates in Swift.I am surprised, can a CBCentralManager extend custom Protocol? How can CBCentralManagerDelegate methods can be parametrized with the custom Protocol? Whats the concept behind this? And what exactly is the need?

This was written in swift 2.3. Will this strategy work in Swift 4.0?

Upvotes: 1

Views: 566

Answers (1)

Amith Dubbasi
Amith Dubbasi

Reputation: 9

Yes, CBCentralManager is from Core Bluetooth framework and can be made to include custom protocol definitions. This approach is followed to leverage TDD - Test Driven Development.

As unit testing Bluetooth functionality makes it difficult while syncing devices, developers take advantage of dependency injection to mock methods by creating their own custom methods instead of using the methods provided by Apple for iOS Frameworks.

You can include your own custom methods for UIView, UIColor etc..

For example

class MockCBCentralManager : BluetoothManagerProtocol {
    var delegate: CBCentralManagerDelegate?
    var scanCalled: Bool = false
    var connectPeripheralCalled = false
    fileprivate var internalState: CBCentralManagerState = .unknown
    var state: CBCentralManagerState {
        get {
            return internalState
        }
    }
}
 func scanForPeripheralsWithServices(_ serviceUUIDs: [CBUUID]?, options[String : AnyObject]?) 
{
  scanCalled = true
  let advertisementData = 
  [CBAdvertisementDataServiceUUIDsKey : 
  [STUDENT_APP_UUID],CBAdvertisementDataLocalNameKey:"MockPeripheral"]           
                
  let mock = MockPeripheral()         
  (delegate as? CentralManager)?.centralManager(self, 
  didDiscoverPeripheral: mock, advertisementData: 
  advertisementData,RSSI: 90) 
    
 }

More information can be found at https://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future

Cheers!

Upvotes: 1

Related Questions