0

My app uses a Bluetooth accessories so I turn the corresponding background mode on. Is it possible to receiving CoreMotion gyroscope data continuously in the background?

The scenario is to scan and hook up a BLE peripheral device when the app is in the background. And to detect phone rotation while the app is in the background, and to send something to the BLE device when some motion is detected.

The gyroscope data update is using an NSOperationQueue I created. It starts as soon as the first view is loaded.

When I test it with app in the foreground, BLE peripheral connected, then put in the background. The gyroscope data update soon ceases. When I walk out of BLE range, the gyroscope data starts again, then soon stopped. I walked in within BLE peripheral range, the gyroscope data is showing again. (I am walking with iPhone hooked to laptop with XCode running and seeing it directly from the logs).

Is there a way to reliably obtain gyroscope data at any time, even when app is in the background?

Desdenova
  • 5,326
  • 8
  • 37
  • 45
huggie
  • 17,587
  • 27
  • 82
  • 139

2 Answers2

1

Have a look at this question.

It looks like you can't get data from CoreMotion without some other background activity like Background Audio or GPS tracking. From my experience, both of those modes will allow you to execute code in the background.

EDIT:
Using BLE, make sure to send data over Bluetooth from time to time to keep iOS from suspending your app for inactivity.

Community
  • 1
  • 1
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
  • I do have Bluetooth background mode — bluetooth-central on. But it doesn't seem to do it continuously. It doesn't make sense for my app to use audio or GPS tracking at all. Any other solutions? – huggie May 11 '15 at 08:25
  • Is your BT device sending data to your app frequently ? Since this is BLE, a lack of communication could give iOS the opportunity to suspend your app. That could explain why you receive CoreMotion updates when you get in range and off range. – Quentin Hayot May 11 '15 at 08:27
  • It works when I repeated querying for service. Thanks. Now there is no way to guarantee that app is going to run indefinitely is there? At some point if many programs are opened, this one could be killed? – huggie May 20 '15 at 04:02
  • 1
    Since you declared background services, your app will be kept alive in priority. Make sure to keep a low profile (low RAM usage) as much as you can. That way, iOS may not have to kill your app. But it can happen, yes. – Quentin Hayot May 20 '15 at 07:38
0

To confirm and add to the accepted answer (a search for background motion updates on iOS led me here). Yes, some other background activity needs to be running for CoreMotion updates to work. I've created a simple iOS app that includes the boilerplate I needed for exploring background CoreLocation and CoreMotion updates. AppDelegate and ViewController are documented to some degree. Tested on an XS Max running iOS 14.2. Hopefully self-explanatory and maybe useful to someone else starting to look into the same areas.

Robin Macharg
  • 1,468
  • 14
  • 22