Using CoreBluetooth in multiple ViewController

I am trying to create an app with multiple ViewController using bluetooth. One is for searching for new peripherals and adding them to a list and one is to receive and send data based on actions taken by the user to the peripheral.

How can I use bluetooth in both ViewController or is there a better option to deal with this problem?

Accepted Reply

I can’t get too far into the details, but the general idea is to define a protocol that contains methods and/or properties to retrieve the data you need. The model class would have its own private properties (to store the list of peripherals in your case), then provide a public method or methods the VC can call to retrieve them. VCs can come and go but the model retains the state. If the list can change over time then you’ll also need a notification or delegate/listener mechanism so the model can notify the VC to update the UI. I’d recommend reading up on the MVC paradigm in general - there’s lots of info out there when you search. For example www.raywenderlich.com/1000705-model-view-controller-mvc-in-ios-a-modern-approach

Replies

Google “iOS Singleton”. This is a common issue. You need to put the shared code/state into a separate model class and refer to it from both VCs. It’s kind of the basic purpose of the whole MVC paradigm.

Thanks for your answer and sorry if this was a novice question, but I am pretty new to app development.

After searching for "iOS Singleton", I found this thread on SO which describes my problem pretty well: https://stackoverflow.com/questions/42088851/creating-a-singleton-for-bluetooth-in-swift-3


I set the delegate via a function in my Singleton class, which I call in AppDelegate.swift after launching the app.

func startBluetooth() {
     manager = CBCentralManager(delegate: self, queue: nil)
}


Now I want to scan for peripherals and list them in a table view to connect to one of them selected by the user. I already have the standard function to show the discovered peripherals.

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
       print("\(String(describing: peripheral.name)): \(peripheral)")
}

How can I get access to all the discovered peripherals in a VC?

I can’t get too far into the details, but the general idea is to define a protocol that contains methods and/or properties to retrieve the data you need. The model class would have its own private properties (to store the list of peripherals in your case), then provide a public method or methods the VC can call to retrieve them. VCs can come and go but the model retains the state. If the list can change over time then you’ll also need a notification or delegate/listener mechanism so the model can notify the VC to update the UI. I’d recommend reading up on the MVC paradigm in general - there’s lots of info out there when you search. For example www.raywenderlich.com/1000705-model-view-controller-mvc-in-ios-a-modern-approach