这是我创建的自定义类:
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") } }
我只是不知道如何从我的主ViewController初始化它.当我尝试以下操作时,它会抱怨ViewController不符合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()} }