I am working on an iPhone app and have implemented CBCentralManager
. Also updated plist with background modes, initialized centralmanager with an identifier.
Also have added this code in the didFinishLaunchingWithOptions
if var centralManagerIdentifiers: NSArray = launchOptions? [UIApplicationLaunchOptionsBluetoothCentralsKey] as? NSArray {
// Awake as Bluetooth Central
// No further logic here, will be handled by centralManager willRestoreState
for identifier in centralManagerIdentifiers {
if identifier as NSString == "centralManager"{
var notification = UILocalNotification()
notification.alertBody = String(centralManagerIdentifiers.count)
notification.alertAction = "open"
notification.fireDate = NSDate()
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
}
I have created a central manager in different class and that is singleton.
class var sharedInstance: BLEManager {
struct Singleton {
static let instance = BLEManager()
}
return Singleton.instance
}
override init() {
super.init()
let centralQueue = dispatch_queue_create(“centralManager_queue”, DISPATCH_QUEUE_SERIAL)
centralManager = CBCentralManager(delegate: self, queue: centralQueue, options: [CBCentralManagerOptionRestoreIdentifierKey : "centralManager"])
}
If i don't use my app for a day or two and then peripheral starts advertising, app wakes and fires this notification but doesn't call any CBCentral delegate method. I have also implemented willRestoreState method but thats also not getting card.
Use case: I need to connect the peripheral and send data once its starts advertising, even though app is not being used. Where should I handle connection process when app gets didFinishLaunchingWithOptions call? do i have to initialize the centralManager in the did finishlaunch method?