How to comply with the CBCentralManagerDelegate protocol?

I am trying to initialize a central manager instance to make an application with a Bluetooth connection.

This is part of my code:

class ViewController: UIViewController, CBCentralManagerDelegate { var myCentralManager = CBCentralManager(delegate: self, queue: nil) //error on this line func centralManagerDidUpdateState(central: CBCentralManager!) { } 

I get an error message:

"Type" ViewController -> () -> ViewController! 'does not comply with the CBCentralManagerDelegate protocol

The only method required by the protocol is centralManagerDidUpdateState() , which I added, so why am I getting an error message?

+5
source share
5 answers

The error message is a little cheating and indicates that you are not able to solve the problem. The problem is that you are accessing self in the initializer for a stored property that you cannot do so.

One way is to simply declare the property without initializing it, and then move the assignment to a variable somewhere, like an initializer for your view controller, or one of your controller lifecycle methods, such as viewDidLoad.

+4
source

When creating the central manager, the central dispatcher calls the centralManagerDidUpdateState method of the delegate object. Therefore, you need to implement this delegate method in order to maintain low Bluetooth energy and use it for use on the central device. As below:

 func centralManagerDidUpdateState(central: CBCentralManager!){ println("CentralManager is initialized") switch central.state{ case CBCentralManagerState.Unauthorized: println("The app is not authorized to use Bluetooth low energy.") case CBCentralManagerState.PoweredOff: println("Bluetooth is currently powered off.") case CBCentralManagerState.PoweredOn: println("Bluetooth is currently powered on and available to use.") default:break } } 
+3
source

I really found a difficult workaround if you already found some other open source project that uses CBCentralManagerDelegate. (And it’s easy to find some open source projects that use CBCentralManagerDelegate)

Assume that in an open source project, "ViewController.swift" uses CBCentralManagerDelegate. In your project you will need "TESTViewController.swift" to use the CBCentralManagerDelegate.

Now all you have to do is

Step 1, copy (i.e. drag) ViewController.swift (which is from an open source project) into your project

Step 2, delete all the rows in ViewController.swift that were just dragged into your project.

Step 3. If necessary, rename it from ViewController.swift to TESTViewController.swift and rename the class name as well.

Step 4 Now you can write your own code "class TESTViewController: UIViewController, CBCentralManagerDelegate {...}"

Yes, it can be stupid, but it is very useful for my project.

+1
source

The code does not match CBCentralManagerDelegate because the "func centralManagerDidUpdateState (central: CBCentralManager!)" Method must be added to a class of type CBCentralManager. Not a view controller type. You must create a new class of type CBCentralManager. Here is my solution to the problem you are referencing above:

 // // ViewController.swift // BLECentral // // Created by Sophronis Mantoles on 11/17/15. // Copyright © 2015 Sophronis Mantoles. All rights reserved. // import UIKit import CoreBluetooth class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } @IBOutlet var statusLabel: UILabel! var bleManager: BLEManager! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func showStatus(sender: AnyObject) { func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertismentData: [NSObject : AnyObject]!, RSSI: NSNumber!) { statusLabel.text = ("\(peripheral.name) : \(RSSI) dBm") } } } class BLEManager { var centralManager : CBCentralManager! var bleHandler : BLEHandler // delegate init() { self.bleHandler = BLEHandler() self.centralManager = CBCentralManager(delegate: self.bleHandler, queue: nil) } } class BLEHandler : NSObject, CBCentralManagerDelegate { override init () { super.init() } func centralManagerDidUpdateState(central: CBCentralManager){ switch (central.state) { case.Unsupported: print("BLE is not supported") case.Unauthorized: print("BLE is unauthorized") case.Unknown: print("BLE is Unknown") case.Resetting: print("BLE is Resetting") case.PoweredOff: print("BLE service is powered off") case.PoweredOn: print("BLE service is powered on") default: print("default state") } } } 
+1
source

You need to at least provide this function in your class:

 func centralManagerDidUpdateState(_ central: CBCentralManager) { <#code#> } 
0
source

Source: https://habr.com/ru/post/1211865/


All Articles