Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Can a BLE device (not an iBeacon) wake up my app?

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)?

like image 256
Vincent D Avatar asked Dec 08 '25 18:12

Vincent D


2 Answers

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.

like image 83
Paulw11 Avatar answered Dec 11 '25 09:12

Paulw11


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.

like image 28
Fennelouski Avatar answered Dec 11 '25 07:12

Fennelouski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!