[英]How to control iOS app from BLE device for background processing without putting the application in front
[英]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方法中初始化CBCenteralManager
其self
设置为其委托。 我假设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.