[英]BLE background processing not working in iOS application
I have an application that needs to interact with peripherals when the app enters a suspended or terminated state. 我有一个需要在进入暂停或终止状态时与外围设备进行交互的应用程序。 I've added the
bluetooth-central
to the Info.plist
and Capabilities ( image ) but after i connect to a peripheral it doesn't fire any of the delegate methods while running in the background, but as soon as the app enters the foreground the delegate methods are called! 我已经将
bluetooth-central
添加到Info.plist
和Capabilities( image )中,但是在连接到外围设备后,它在后台运行时不会触发任何委托方法,但是一旦应用程序进入前台委托方法被调用! I've also set up CBCenteralManager
state restoration 我还设置了
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)
I've been digging around the internet for hours and haven't found anything. 我已经在互联网上闲逛了几个小时,却什么也没发现。 Has anyone had a similar situation before?
以前有人遇到过类似情况吗? Any tips?
有小费吗? Thanks!
谢谢!
edit: code in context: 编辑:上下文中的代码:
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
}
}
the CoreBlueToothObserver()
initializes CBCenteralManager
in its init method and sets self
to its delegate. CoreBlueToothObserver()
在其init方法中初始化CBCenteralManager
其self
设置为其委托。 I would assume CoreBlueToothObserver()
would be around for the lifetime of the app and not need to be re-initalized in didFinishLaunchingWithOptions
? 我假设
CoreBlueToothObserver()
将在应用程序的生命周期CoreBlueToothObserver()
在,并且不需要在didFinishLaunchingWithOptions
重新didFinishLaunchingWithOptions
? or maybe i need to recreate a CBCenteralManager
and inject it in didFinishLaunchingWithOptions
? 还是我需要重新创建
CBCenteralManager
并将其注入didFinishLaunchingWithOptions
? Just stumped as to whats happening. 只是对发生的事情感到困惑。
With the help from @Paulw11 this worked 在@ 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 is now receiving the delegate methods! 应用程序现在正在接收委托方法!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.