Search code examples
iosobjective-cbluetooth-lowenergycore-bluetooth

When app in Background scan Bluetooth connecting peripherals


iOS 11: When App in Background CBCentralManager Delegate Method didDiscoverPeripheral, didConnectPeripheral, didFailToConnectPeripheral, didDisconnectPeripheral not called.


Solution

  • Core Bluetooth Background Execution Modes.

    If your app needs to run in background to perform certain Bluetooth-related tasks, it must declare that it supports a Core Bluetooth background execution mode in its Information property list (Info.plist) file.

    When your app declares this, the system wakes it up from a suspended state to allow it to handle Bluetooth-related events. This support is important for apps that interact with Bluetooth low energy devices that deliver data at regular intervals, such as a heart rate monitor.

    There are two Core Bluetooth background execution modes that an app may declare—one for apps implementing the central role, and another for apps implementing the peripheral role. If your app implements both roles, it may declare that it supports both background execution modes. The Core Bluetooth background execution modes are declared by adding the UIBackgroundModes key to your Info.plist file and setting the key’s value to an array containing one of the following strings:

    1) bluetooth-central—The app communicates with Bluetooth low energy peripherals using the Core Bluetooth framework.

    2) bluetooth-peripheral—The app shares data using the Core Bluetooth framework.

    following apple developer document link.

    https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

    Update:

    The bluetooth-peripheral Background Execution Mode To perform certain peripheral role tasks while in the background, you must include the UIBackgroundModes key with the bluetooth-peripheral value in your app’s Info.plist file. When this key-value pair is included in the app’s Info.plist file, the system wakes up your app to process read, write, and subscription events.

    In addition to allowing your app to be woken up to handle read, write, and subscription requests from connected centrals, the Core Bluetooth framework allows your app to advertise while in the background state.

    That said, you should be aware that advertising while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is advertising while in the background.

    May be it's useful to you!!