Post

Replies

Boosts

Views

Activity

How do I run CoreBluetooth in background thread?
So I managed to get CoreBluetooth functionality working in an objective c++ commandline application, however corebluetooth blocks the main thread:main.cppint main() { call_obj_c_corebluetooth_initialization(); // corebluetooth occupies main thread std::thread worker(do_cpp_stuff); // c++ code runs in background thread, but I want to run c++ in main loop }How can I set up corebluetooth properly when I initialize the CBCentralManager on a background thread?std::thread worker(call_objective_c_code_to_initialize_cbcentralmanager)things I know:Must call [[NSRunLoop currentRunLoop] run] after CBCentralManager initialization because CoreBluetooth needs a runloop for delegate callbacks.CoreBluetooth has a background mode (but I don't think background mode is activated with the background thread)things I tried:creating a serial dispatch queue and initializing CBCentralManager with it._centralQueue = dispatch_queue_create("centralqueue", DISPATCH_QUEUE_SERIAL); self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_centralQueue options:nil]; [[NSRunLoop currentRunLoop] run];
2
0
1.9k
May ’20
CoreBluetooth using objective c++ centralManagerDidUpdateState not being called
More details in my SO post: https://stackoverflow.com/questions/61715002/corebluetooth-delegate-method-never-called-objective-cSo I am trying to make calls to corebluetooth using c++. I am at the point where I have all the implementation details set up in objective c and can call from c++. The problem is that after initializing CBCentralManager, the function centralManagerDidUpdateState is not called.Expectly, when I call centralManagerDidUpdateState manually the manager's state is unknown. Also one thing to note is most implementation of corebluetooth I've seen are in view controllers, whereas mine is just a simple call to initialize the bluetooth and then terminate the program, so maybe something is wrong there?Anyone here have experience with this issue?Thank you!edit: after digging around, I think corebluetooth is sharing the same thread as the main c++ threadedi2: okay after some painful debugging and digging I think corebluetooth callbacks depend on running through a main runloop. I implemented a really dirty one to run corebluetooth separate from my testing class thread and now my code works. Will post the solution to stackoverflow when I clean it up.
0
0
818
May ’20