Here is my custom class that I have created:
import UIKit
import CoreBluetooth
protocol vaBeanDelegate
{
}
class vaBean: CBCentralManager, CBCentralManagerDelegate {
override init!(delegate: CBCentralManagerDelegate!, queue: dispatch_queue_t!) {
println("bean initialised")
super.init()
self.delegate = delegate
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println("discovered peripheral(s)")
}
func centralManagerDidUpdateState(central: CBCentralManager!) {
switch (central.state) {
case .PoweredOff:
println("hardware is powered off")
case .PoweredOn:
println("hardware is powered on and ready")
//centralManager.scanForPeripheralsWithServices(nil, options: nil)
case .Resetting:
println("hardware is resetting")
case .Unauthorized:
println("state is unauthorized")
case .Unknown:
println("state is unknown");
case .Unsupported:
println("hardware is unsupported on this platform");
}
}
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
println("didConnectPeripheral")
}
}
I just don't know how to initialize it from my main ViewController. When I try the following it complains that ViewController does not conform to the CBCentralManagerDelegate:
import UIKit
class ViewController: UIViewController {
var myBean: vaBean!
override func viewDidLoad() {
super.viewDidLoad()
println("blueToothTest v1.00")
println("opening bluetooth connection ...")
myBean = vaBean(delegate: self, queue: nil)
}
override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}
}
vaBean(delegate: self, queue: nil), theselfrefers to the ViewController, which cannot serve as a delegate because it doesn't conform to the delegate protocol. I think your best bet is to change your custom initializer to take only thequeueparameter, and have it setself.delegate = self-- in the context of the initializer,selfwill refer to your vaBean instance, which is what you want.