Search code examples
swiftmacos-sierracbcentralmanager

CBCentralManagerDelegate didRetrievePeripherals not found


I am trying to implement Apple's sample code written in Objective-C in Swift on MacOS with Deployment target 10.12. The sample can be found here for reference: Heart Rate Monitor

On implementation, I came across an issue where the CentralManager: didRetrievePeripherals callback is not implementable. I have checked both the CBCentralManagerDelegate and CBPheripheralDelegate protocols that my ViewController implements and none of them contain such a method. The only methods I can find are:

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any])
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) 

I am not quite sure what is going wrong here. I checked Apple's Objective-C version and went through both delegates implemented there but don't see anything in the protocol definitions anything that says that I can implement the following function:

- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
    ...
} 

But they have implemented this under the set of CBCentralManagerDelegates and project builds without issue. How is this possible?

My implementation for reference is as follows:

import Cocoa
import CoreBluetooth
import QuartzCore

class ViewController: NSViewController, CBPeripheralDelegate, CBCentralManagerDelegate {

    var manager: CBCentralManager?
    var peripheral: CBPeripheral?
    var autoconnect = true
    let arrayController: NSArrayController = NSArrayController()
    var heartRateMonitors = NSMutableArray()
    let scanSheet: NSWindow = NSWindow()

    //MARK: IBOutlets
    @IBOutlet weak var connectButton: NSButton!
    @IBOutlet weak var indicatorButton: NSButton!
    @IBOutlet weak var progressIndicator: NSProgressIndicator!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


        manager = CBCentralManager(delegate: self, queue: nil)
        if autoconnect {
            startScan()
        }
    }
...
}

Thank you


Solution

  • From Apple changelog we can get that centralManager: didRetrievePeripherals method was removed in iOS 9. So if you are using xcode with latest SKD this method is not included in it.