How to Wake Up iOS App with Bluetooth Signal (Ble)

How to wake up iOS app with bluetooth signal (BLE)

If the app was running in the background and then closed (I mean here terminated - and you do not see it anymore in the list of background apps when you double click the home button) due to memory pressure from other apps, then the iOS will take care of waking it up again when a delegate is called (assuming you have background modes listed in .plist).

If the app was running in the background and then closed by the user (again I mean here terminated. So the user double clicked to get the list of apps running in the background and then clicked on your app in the background list until it wiggled and then pressed the 'x' next to the app to kill it) then this is a clear indication that the user does not want the app running in the background and the app will not be re-launched. The user has to relaunch the app for its delegates to start functioning again in the background (new in iOS7).

Update:
I am not sure if this is in the documentation but it is in CoreBluetooth WWDC 2013 Video. I recommend you watch it. They spent a good portion of the video on how CoreBluetooth behaves in the background.

Wake up iOS application by iBeacon when still connected to Bluetooth

A few thoughts:

  1. Yes, BLE Peripheral devices typically stop advertising when connected. In this state, you cannot get an iBeacon app wakeup if the BLE peripheral is no longer advertising the beacon.

    • While this behavior is typical, it is not an absolute rule. It is possible to build peripherals that keep advertising when connected, although there may be limitations with the chipset you are using.

    • A common approach to prevent ever-lasting connections is to build peripheral code that will automatically disconnect if there has not been any communication with the mobile phone in X seconds or minutes. This will allow beaconing to begin again.

  2. Bluetooth Pairing and BLE Connections don't work the same way. You can establish a BLE connection in the background with no user interaction. BLE connections (not pairing) is how this is typically done to make it automatic. A connection to a GATT service may be established silently in the background and data exchanged with the following background mode in the plist (which requires no special App Store justification):

    <key>UIBackgroundModes</key>
    <array>
    <string>bluetooth-central</string>
    </array>
  3. If you really do want to pair with user interaction, you will need to take the following steps:

    • When the beacon is detected in the background, send a local notification to the user telling them to launch the app to pair. (Adjust the language to match your use case.)
    • When the user taps on the notification, the app will come to he foreground. Detect this and start the pairing process.

iOS stops waking up the app upon incoming BLE connection from peripheral

I wanna start by saying that I have been working with CoreBluetooth for a long time now and from what I have noticed CoreBluetooth State Preservation and Restoration does not work reliably at all. You can get it working sort of "ok", but you will never get it to reconnect reliably unless Apple fixes it some day.

There are so many bugs that causes this to not work properly, but I will give you one that I believe is causing your problems:

State restoration will only relaunch your app due to bluetooth related activity if the event originates from a peripheral accessory that you are communicating with, such as connect/disconnect events and characteristics notifications. For other events, most importantly general bluetooth state change events, your app will not be relaunched and notified of this. The reason why this is so bad is because all bluetooth state change events will cancel all pending or current connections, meaning that pending connections will be dropped and your application will not be notified of it. This effectively means that your application will still believe that the connections are still pending when in fact they are not. Since your application is terminated at this time, the only way for it to wake up again is by having the user manually launch it again (or alternatively “hack” other background modes for this purpose, which does not work very reliably either).

This thing happens if the user toggles Flight Mode, toggles Bluetooth, power cycles the iOS device, or any other undefined reasons that many cause state changes…

But this is only one bug. Many other exists as well, such as the XPC connection being interrupted at different times for no apparent reason. I have also noticed that the pending connection can go into “limbo” mode where the peripheral state gets set to Connecting, but in fact it will never connect unless you cycle the connection state.

Anyhow, I am sad to say it, but if you are developing an app that must rely on the peripheral being reconnected in the background then I would not recommend doing it. You will be frustrated. I could probably write an essay about all the bugs in Core Bluetooth that Apple does not want to fix. Even more strange is that you can pretty easily ruin the bluetooth connectivity globally on the device from one single app so that no app can use bluetooth until the device is rebooted. This is pretty bad since it goes against Apple's own Sandboxing principle.



Related Topics



Leave a reply



Submit