BLE后台处理在iOS应用程序中不起作用

时间:2016-06-12 19:52:47

标签: ios iphone swift bluetooth

当应用程序进入暂停或终止状态时,我有一个需要与外围设备交互的应用程序。我已将bluetooth-central添加到Info.plist和功能(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方法中初始化CBCenteralManager,并将self设置为其委托。我认为CoreBlueToothObserver()会在应用程序的生命周期内出现,而不需要在didFinishLaunchingWithOptions重新初始化?或者我可能需要重新创建CBCenteralManager并将其注入didFinishLaunchingWithOptions?对于发生的事情感到困惑。

1 个答案:

答案 0 :(得分:0)

在@ Paulw11的帮助下,这工作

class AppDelegate: UIResponder, UIApplicationDelegate {

 var window: UIWindow?

 var bluetoothManager: CoreBluetoothObserver!

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

  bluetoothManager = CoreBluetoothObserver()
  return true
 }
}

app现在正在接收委托方法!