0
votes

I have asked this question a few times (here and here) with no usable responses so I will ask this in a different way now...

What I need

I need to be able to wake my app up from terminated, send and receive some data from a Bluetooth peripheral I have programmed myself. This is important data for security reasons so I absolutely need the app to wake up. However, the device isn't always on most times it's powered off, so therefore I need this use case to be handled when the peripheral is turned on/off. To do this I need to use iBeacon to wake up the app, send a command to the box, get a response and then go into continuous exchange mode. If the peripheral device is turned off I don't care, just when it's turned back on thats when I need the process of continuous data exchange to restart.

The Problem

I have tried all the suggested background "app wakable" info.plist items... Voip, background audio, iBeacon and bluetooth didConnectPeripheral/didDisconnect delegate methods and nothing really works very reliably. For a few days I had the iBeacon didEnterRegion and didExitRegion methods firing when I would plug/unplug my device so at least theres something sort of reliable.

The real problem begins if I use iBeacon to wake up the app and connect to bluetooth (because I cant exchange info with the box until I connect/pair at the iOS system level). Because the app hasn't been paired with yet, none of the commands in the background ever make it to the box. When this process is started in the background, no message comes up asking if you would like to pair with this device (it all works fine when app is running and you can pair).

Also, even if I did get a chance to pair then I would now be connected/paired permanently (which I wouldn't want the user to have to press "pair" every time I plug in the device and the app is closed). Unless the user goes into bluetooth settings and "forgets the device" then I can't disconnect them. Therefore I have just lost my ability to use iBeacon to wake up the app once it gets closed, because now iBeacon will never fire so long as the app is connected via Bluetooth.

My Question(s)

How are other apps doing this so easily and what's the proper flow for permanently waking your app from background/terminated? I am struggling to figure out the flow here.

Am I really expected to only use iBeacon in use cases where no connectivity or pairing is also required? Am I supposed to disconnect the user somehow from the peripheral?

Summation

So in summary, using iBeacon for waking up the app, doesn't work if you need to connect/pair to a peripheral because then those methods will never fire again until no longer paired/connected. If I were to find some magic way to disconnect a person when the app dies and revert back to iBeacon, it wouldn't work because I can't seem to pair from the background so the commands don't make it to the peripheral. Lastly, if I use a purely connected Bluetooth approach, the didConnect and didDisconnect methods never fire when I plug/unplug the device.

1

1 Answers

1
votes

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.