为什么没有CBPeripheralManagerDelegate方法调用,尽管我已经将类初始化为 var bluetoothManager = CBPeripheralManager(delegate:self,queue:nil,options:nil)
已经在我的Bundle中添加了BlueTooth Framework。
import UIKit
import CoreBluetooth
class AppSettingsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension AppSettingsViewController : CBPeripheralManagerDelegate{
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
if peripheral.state == CBPeripheralManagerState.PoweredOn{
println("broadcasting")
}else if peripheral.state == CBPeripheralManagerState.PoweredOff{
println("no bluetooth found")
}else if peripheral.state == CBPeripheralManagerState.Unauthorized {
println("This option is not allowed by your application")
}
}
}
答案 0 :(得分:3)
使bluetoothManager
成为实例变量。目前,viewDidLoad()
完成后会被取消分配。
答案 1 :(得分:2)
您必须将bluetoothManager
保存到ViewController的全局变量才能接收回叫。
class AppSettingsViewController: UIViewController, CBPeripheralManagerDelegate {
var bluetoothManager : CBPeripheralManager?
override func viewDidLoad() {
super.viewDidLoad()
self.bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
}