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! I've also set up CBCenteralManager
state restoration
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. I would assume CoreBlueToothObserver()
would be around for the lifetime of the app and not need to be re-initalized in didFinishLaunchingWithOptions
? or maybe i need to recreate a CBCenteralManager
and inject it in didFinishLaunchingWithOptions
? Just stumped as to whats happening.
With the help from @Paulw11 this worked
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.