Core Bluetooth Background Modes on a package with SPM

Hi,

I'm working on an SPM library that uses CoreBluetooth. Since I need to work with Bluetooth in background, I've set CBCentralManagerOptionRestoreIdentifierKey option to my instance of CBCentralManager. When I run the unit tests I encounter the following exception:

Thread 1: "State restoration of CBCentralManager is only allowed for applications that have specified the "bluetooth-central" background mode"

Of course, my demo application that I use to test the library has the bluetooth-central background mode checked.

I think the issue is related to the fact that when the test target is executed, there is no associated application. Can background modes be applied to the package? How can this be resolved?

Replies

You should unit test your logic, not your CBCentralManager itself. Unit testing Core Bluetooth isn't going to yield the results you expect. It is just as you described, unit testing isn't running in an app per-say.

Simple fact, you can't initialize many of the Core Bluetooth types, namely CBPeripheral. I'd check out the following video and refactor your code accordingly for testing. https://developer.apple.com/videos/play/wwdc2018/417/