2016-06-10 29 views
1

试图获得核蓝牙状态恢复迅速在持续工作,但我只能似乎得到它开火一次,然后它不到目前为止不再回应...核心蓝牙状态恢复仅触发一次

我在我的课的init:

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 ...任何人都有任何想法?

回答

0

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

筛选禁用,每个中心从周边

然后接收广告包时会产生一个发现事件,状态恢复的委托方法将被多次调用,但只有在必须调用时才会调用,即如果在发现事件服务之间被禁用。