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
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")
}
}
}