2015-06-19 42 views
3

我正在開發iPhone應用程序並已實施CBCentralManager。還用後臺模式更新了plist,使用標識符初始化centralmanager。狀態保存和恢復BLE-調用didFinishLaunchingWithOptions,但不調用CBCentral的任何代理方法

而且已經在didFinishLaunchingWithOptions

if var centralManagerIdentifiers: NSArray = launchOptions? [UIApplicationLaunchOptionsBluetoothCentralsKey] as? NSArray { 
    // Awake as Bluetooth Central 
    // No further logic here, will be handled by centralManager willRestoreState 

    for identifier in centralManagerIdentifiers { 
     if identifier as NSString == "centralManager"{ 
      var notification = UILocalNotification() 
      notification.alertBody = String(centralManagerIdentifiers.count) 
      notification.alertAction = "open" 
      notification.fireDate = NSDate() 
      notification.soundName = UILocalNotificationDefaultSoundName 
      UIApplication.sharedApplication().scheduleLocalNotification(notification) 

      } 
     } 
} 

我已經創造了不同的類中的中央管理器中添加該代碼,這是單身。

class var sharedInstance: BLEManager { 
    struct Singleton { 
     static let instance = BLEManager() 
    } 

    return Singleton.instance 
} 


override init() { 
    super.init() 
    let centralQueue = dispatch_queue_create(「centralManager_queue」, DISPATCH_QUEUE_SERIAL) 
    centralManager = CBCentralManager(delegate: self, queue: centralQueue, options: [CBCentralManagerOptionRestoreIdentifierKey : "centralManager"]) 
} 

如果我不使用我的應用程序的一兩天,然後開始周邊廣告,應用程序喚醒和火災此通知,但不調用任何CBCentral委託方法。我也實現了willRestoreState方法,但那也沒有獲取卡。

使用案例:我需要連接外圍設備並在其開始廣告後發送數據,即使應用程序未被使用。 當應用程序獲取didFinishLaunchingWithOptions調用時,我應該在哪裏處理連接過程?我是否必須在did finishlaunch方法中初始化centralManager?

回答

3

中央管理器必須立即初始化以處理委託調用。一旦CBPeripheralManager被創建並且委託被傳遞給init方法,委託方法就會被調用。如果管理器的標識符與掛起的會話相匹配,狀態將被恢復。

實例化管理器的好處在didFinishLaunchingWithOptions方法中。

您可以通過導致崩潰或在測試時按xCode的停止按鈕並等待由BLE觸發的新操作(例如來自通知特徵的通知)來輕鬆進行測試。它比等待幾天後系統殺死應用程序處於非活動狀態的速度要快得多。

+0

所以它第一次工作,當我打開它調用willRestoreState,didUpdateState,didConnectPeripherial的外設。 當我關閉外設時,它也調用了willRestoreState,如果我再打開它,除非我解鎖iPhone,否則不會執行任何操作。 – Paragon

+0

這是一個不同的問題。看起來您沒有正確使用恢復流程。 –

+0

當我關閉外圍設備時應該調用willRestoreState嗎? – Paragon

相關問題