核心蓝牙状态恢复只触发一次

core bluetooth state restoration only fires once

试图让核心蓝牙状态恢复在 swift 中始终如一地工作,但我似乎只能让它启动一次,然后它就不再响应了...

到目前为止,我的 class 初始化中有:

override init() {
    super.init()
    let centralQueue = dispatch_queue_create("com.domain.app", DISPATCH_QUEUE_SERIAL)
    centralManager = CBCentralManager(delegate: self, queue: centralQueue, options: [CBCentralManagerOptionRestoreIdentifierKey: "myCentralManager", CBCentralManagerOptionShowPowerAlertKey: true]) 

}

和我的 WillRestoreState 代表:

func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) {       

    let peripheral = dict[CBCentralManagerRestoredStatePeripheralsKey] 

    for peripheral in peripherals as! [CBPeripheral] {
        showGenericNotification("BLE \(peripheral)")
        peripheral.delegate = bleService
    }   
} 

然后当我从 BLE 设备发送数据时,showNotification() 会在我的通知中心放置一个通知。它只触发一次,然后停止响应。 bleService 是带有管理外设的 CBPeripheralDelegate 的实例

委托似乎没有分配给 bleService...有人有任何想法吗??

CBCentralManager 有一个方法 scanForPeripherals(withServices:options:),您可以在其中指定扫描选项。默认情况下,此管理器会为收到的多个外设数据包生成一个发现事件,如 docs 中所述。您必须在选项字典 CBCentralManagerScanOptionAllowDuplicatesKey 下传递 true 值才能获得预期效果:

filtering is disabled and a discovery event is generated each time the central receives an advertising packet from the peripheral

然后,将多次调用用于状态恢复的委托方法,但仅在必须调用时才会调用,即如果在发现事件之间服务被禁用。