F_79
F_79

Reputation: 133

How to conform to CBCentralManagerDelegate Protocol?

I am trying to initialize a central manager instance to make an app with Bluetooth connectivity.

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:

"Type 'ViewController -> () -> ViewController!' does not conform to protocol 'CBCentralManagerDelegate'

The only method required by the protocol is centralManagerDidUpdateState() which I have added, so why do I get an error?

Upvotes: 7

Views: 9147

Answers (5)

Michel Storms
Michel Storms

Reputation: 376

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

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

Upvotes: 1

Sophman
Sophman

Reputation: 85

The code does not conform to the CBCentralManagerDelegate because the method "func centralManagerDidUpdateState(central: CBCentralManager!)" needs to be added to the class that is of type CBCentralManager. Not the view controller type. You have to 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")
       }
    }
 }

Upvotes: 1

Chenghao
Chenghao

Reputation: 355

When you create a central manager, the central manager calls centralManagerDidUpdateState method of its delegate object. So you have to implement this delegate method to ensure that Bluetooth low energy is supported and available to use the central device. Like 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
    }
}

Upvotes: 5

Ancalagon BerenLuthien
Ancalagon BerenLuthien

Reputation: 1194

I actually found a tricky workaround, as long as you already found some other's open source project which uses CBCentralManagerDelegate. (And it is not hard to find some open source projects that use CBCentralManagerDelegate)

Lets say in the open source project, "ViewController.swift" uses CBCentralManagerDelegate. In your project, you need "TESTViewController.swift" to use CBCentralManagerDelegate.

Now all you need to do is to

Step 1, Copy (that is, drag) the ViewController.swift (which is from the open source project) to your project

Step 2, delete all lines in the ViewController.swift, which was just dragged to your project.

Step 3, If needed, 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 {...} "

Yeah, this might be silly, but it does work for my project quickly.

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130222

The error message is a little deceiving and is pointing you away from the actual issue. The problem is that you are accessing self in the initializer for a stored property, which you can't do like this.

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

Upvotes: 5

Related Questions