How to conform to CBCentralManagerDelegate Protocol?

前端 未结 5 587
后悔当初
后悔当初 2021-01-18 13:31

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

This is part of my code:

class ViewController: UIVie         


        
相关标签:
5条回答
  • 2021-01-18 14:05

    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.

    0 讨论(0)
  • 2021-01-18 14:06

    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")
           }
        }
     }
    
    0 讨论(0)
  • 2021-01-18 14:12

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

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        <#code#>
    }
    
    0 讨论(0)
  • 2021-01-18 14:17

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-18 14:20

    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.

    0 讨论(0)
提交回复
热议问题