3

Is it possible to associate a regular Bluetooth Low Energy device (not an iBeacon!) with my iPhone app so that when the device sends data my app gets woken up by the iPhone even if the iPhone is locked and the app is terminated (not even in the background)?

Vincent D
  • 137
  • 2
  • 12
  • Possible duplicate of [How to wake up iOS app with bluetooth signal (BLE)](https://stackoverflow.com/questions/19932090/how-to-wake-up-ios-app-with-bluetooth-signal-ble) – LCJ Jun 28 '17 at 18:31

3 Answers3

6

As long as your app specifies Bluetooth Central background mode then it will be woken if

  • Your app has a current connection to the device and it sends data (i.e. the device is in range and it notifies or indicates on a characteristic)
  • Your app has a pending connection to the device and it comes into range(i.e. the device was out of range, but you have called connect to automatically reconnect when it comes into range)
  • Your app was scanning for specific service types and a device advertising one of these service types comes into range

The case where you app is terminated is slightly different. For these scenarios to work in this case your app must implement state preservation and restoration

Core Bluetooth supports state preservation and restoration for apps that implement the central role, peripheral role, or both.

When your app implements the central role and adds support for state preservation and restoration, the system saves the state of your central manager object when the system is about to terminate your app to free up memory (if your app has multiple central managers, you can choose which ones you want the system to keep track of). In particular, for a given CBCentralManager object, the system keeps track of:

  • The services the central manager was scanning for (and any scan options specified when the scan started)
  • The peripherals the central manager was trying to connect to or had already connected to
  • The characteristics the central manager was subscribed to

The Apple guide talks about the situation where your App is terminated due to memory pressure. It doesn't specify what happens if the app is terminated by the user "swiping up" in the app switcher - In many cases iOS takes this as an indication that the user doesn't want the app to run at all and won't restore it in this case.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • So if my app has a current connection to a BLE device (not iBeacon) and my app is terminated by the user "swiping it up" in the app switcher. My app will not wake up when the BLE device starts sending data, even if I implemented state restoration? – Vincent D Aug 10 '15 at 01:40
  • I am not sure. I haven't tested this and the documentation only mentions termination due to memory pressure. – Paulw11 Aug 10 '15 at 02:04
2

There are two states for a bluetooth device to interact with your app:

  1. It has never interacted with your app before

  2. It has already connected to the use's device once and to the app once

    In either case, an iBeacon device will be able to interact with your app.

If the device hasn't connected with your app before, I'm not entirely certain if there is a way to make it work. I have tried and failed to get it to wake up the app.

However, if the bluetooth device has connected before, then you can use CBCentralManager and its delegate methods to communicate between the device and your app.

Community
  • 1
  • 1
Fennelouski
  • 2,411
  • 1
  • 18
  • 26
  • Thanks Fennelouski! 1- So if the Bluetooth LE device (not iBeacon) has already connected with the app before, the iPhone will wake up my app automatically, even if the app was terminated? 2 - Is this done instantly (in less than one second) or is there a delay between the data transmission from the BLE device and the app waking up to receive the data? 3 - And could the app send that data to a web server during its wake up time? – Vincent D Aug 09 '15 at 17:09
0

Core Bluetooth should wake up your app from the OS if registered. Once awake, it's running in the background like normal. You have up to 3 minutes to perform whatever tasks you need to.

Fennelouski
  • 2,411
  • 1
  • 18
  • 26
  • Thanks again Fennelouski! Is this done instantly (in less than one second) or is there a delay between the data transmission from the BLE device and the app waking up to receive the data? – Vincent D Aug 09 '15 at 18:01
  • 1
    It's pretty quick. I'd assume less than 1 second in almost all cases. The bigger delay would be in the connection of the Bluetooth device to the phone. – Fennelouski Aug 09 '15 at 19:04