2

I'm afraid I already know the answer of my own question, but I decided to ask anyway before losing my hopes.

I have the following use case: an app writes some bytes in a "shared" resource (let's say on a file) and another app reads the data and sends them to an external device via BLE while in background. Unfortunately, I know that concepts like shared resource and background are bounded in iOS. What I tried so far is:

Using App Group to share data between two apps

This is working fine even in background, but both the apps need to be produced by the same developer team (i.e. Team ID must be the same). This is a problem because one app is not produced by my developer team.

Copying data in the pasteboard

This is not working when app is in background. Data will always be nil and will update only when the app becomes active again.

...and of course there are...

Background limitations

Even though I go for the App Group solution, the only way I can manage to read data and send to the BLE device in background is by starting a background task. This obviously is a caveat: the task has an expiration time (from tests I performed it keeps going for about 3 minutes before being suspended by the OS). I don't need to run a long-time task, but I cannot assure it can be performed completely by 3 minutes or so.

Here's the question: Is there some other solution for this use case or should I finally give up?

Dree
  • 702
  • 9
  • 29
  • One idea would be deeplikning. Check this Apple document: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html, section "Using URL Schemes to Communicate with Apps" – mag_zbc Jul 12 '17 at 08:47
  • @mag_zbc Using URL Schemes is not an option, because that way apps would keep switching from one to another and this is not user-friendly. – Dree Jul 12 '17 at 09:28

1 Answers1

1

If you are using CoreBluetooth to send files to your Bluetooth device, you can try adding bluetooth-central as UIBackgroundMode key in Info.plist of your app.

Apple has mentioned this in its programming guide:

Communicating with a Bluetooth Accessory Apps that work with Bluetooth peripherals can ask to be woken up if the peripheral delivers an update when the app is suspended. This support is important for Bluetooth-LE accessories that deliver data at regular intervals, such as a Bluetooth heart rate belt. You enable support for using bluetooth accessories from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the bluetooth-central value in your app’s Info.plist file.) When you enable this mode, the Core Bluetooth framework keeps open any active sessions for the corresponding peripheral. In addition, new data arriving from the peripheral causes the system to wake up the app so that it can process the data. The system also wakes up the app to process accessory connection and disconnection notifications.

In iOS 6, an app can also operate in peripheral mode with Bluetooth accessories. To act as a Bluetooth accessory, you must enable support for that mode from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the bluetooth-peripheral value in your app’s Info.plist file.) Enabling this mode lets the Core Bluetooth framework wake the app up briefly in the background so that it can handle accessory-related requests. Apps woken up for these events should process them and return as quickly as possible so that the app can be suspended again.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • Of course I'm using CoreBluetooth, but this doesn't change the fact that the task will be suspended anyway by an undefined amount of time. Let's say I need about 5 minutes to complete my task: how can I be sure that my task will complete? – Dree Jul 12 '17 at 09:20
  • @Dree: The app gets time of three minutes, if you have used beginBackgroundTaskWithName:, but if your app comes in the category of long running apps like using Bluetooth LE Accessory, then it is allowed to run in the background for longer durations. – Puneet Sharma Jul 12 '17 at 09:31
  • I know. Here the problem is that I need to read the data when app runs in background. This operation is not part of exchanging messages via CoreBluetooth and will be stopped sooner or later. – Dree Jul 12 '17 at 09:40
  • @Dree: Lets say you start reading the data only when your bluetooth accessory asks for data. I guess reading of data should be allowed in tha case. – Puneet Sharma Jul 12 '17 at 10:11
  • How can the app know that it has to read data if it's in the background? – Dree Jul 12 '17 at 10:26
  • I have not worked much with CoreBluetooth and am not aware of the nitty gritties but looks like there is tutorial for the same here:https://community.estimote.com/hc/en-us/articles/203253193-Launching-notifications-in-iOS-when-the-app-is-killed – Puneet Sharma Jul 12 '17 at 10:35