Search code examples
iosiphoneswiftbluetoothcore-bluetooth

iOS App connection Bluetooth Lower Energy Service from Background


I have been building a little app that connects with a small sensor using BLE. It is working ok. However now I want to put the application in Background and when the service from Bluetooth is detected open the App ( or change the State from Background to Foreground ). Is that possible?

Right now I have this code working:

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {

    let localName = advertisementData[CBAdvertisementDataLocalNameKey] as! String
    if !localName.isEmpty {

        println("Found the \(localName)")

        // Trying to open the app but it does not work
        dispatch_async(dispatch_get_main_queue(), { () in
            UIApplication.sharedApplication().openURL(NSURL(string: "app_url_scheme://")!)
        })

        self.centralManager!.stopScan()
        peripheral.delegate = self

        self.centralManager!.connectPeripheral(peripheral, options: nil)

    }

}

After closing the app and putting the peripheral on it is discovered, but now I do not know how to open the app. I was trying to follow this tutorial to open using the openURL feature. However it is not working.

Any idea how to get the App to open itself?

Thanks a lot for your time!


Solution

  • Apple has guidelines to follow and you cannot cause an app to open "Itself" it always needs user input.

    What I do when monitoring beacons is have a UILocalnotification pop up and when the user interacts with it, your app will open.