簡體   English   中英

BLE后台處理在iOS應用程序中不起作用

[英]BLE background processing not working in iOS application

我有一個需要在進入暫停或終止狀態時與外圍設備進行交互的應用程序。 我已經將bluetooth-central添加到Info.plist和Capabilities( image )中,但是在連接到外圍設備后,它在后台運行時不會觸發任何委托方法,但是一旦應用程序進入前台委托方法被調用! 我還設置了CBCenteralManager狀態還原

manager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: CoreBluetoothRestorationKey])

let ids = [CBUUID(string: ServiceUUID)]
manager.scanForPeripheralsWithServices(ids, options: nil)

manager.connectPeripheral(peripheral, options: nil)

我已經在互聯網上閑逛了幾個小時,卻什么也沒發現。 以前有人遇到過類似情況嗎? 有小費嗎? 謝謝!

編輯:上下文中的代碼:

class AppDelegate: UIResponder, UIApplicationDelegate {

 var window: UIWindow?

 let bluetoothManager = CoreBluetoothObserver()

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

 return true
}
}

class CoreBluetoothObserver: NSObject {

  let manager: CBCentralManager

  override init() {
   manager = CBCentralManager(delegate: nil, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: CoreBluetoothRestorationKey])
   super.init()
   manager.delegate = self
 }
}

CoreBlueToothObserver()在其init方法中初始化CBCenteralManagerself設置為其委托。 我假設CoreBlueToothObserver()將在應用程序的生命周期CoreBlueToothObserver()在,並且不需要在didFinishLaunchingWithOptions重新didFinishLaunchingWithOptions 還是我需要重新創建CBCenteralManager並將其注入didFinishLaunchingWithOptions 只是對發生的事情感到困惑。

在@ Paulw11的幫助下,此方法成功了

class AppDelegate: UIResponder, UIApplicationDelegate {

 var window: UIWindow?

 var bluetoothManager: CoreBluetoothObserver!

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  bluetoothManager = CoreBluetoothObserver()
  return true
 }
}

應用程序現在正在接收委托方法!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM