0
votes

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?

2

2 Answers

1
votes

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.

0
votes

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.