繁体   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