当应用程序进入暂停或终止状态时,我有一个需要与外围设备交互的应用程序。我已将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
?对于发生的事情感到困惑。
答案 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现在正在接收委托方法!