![](/img/trans.png)
[英]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.