Search code examples
ioscore-motion

Is it possible to obtain gyroscope data updates when app is in the background


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?


Solution

  • 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.