did you ever find an answer to this question?
Post
Replies
Boosts
Views
Activity
Okay in a sudden fit of clarity, I solved this problem.main.cppvoid do_corebluetooth stuff() {
*ptr = get_bluetooth_object();
}
int main() {
std::thread worker(do_corebluetooth_stuff);
}corebluetooth.mmvoid get_bluetooth_object() {
// initialize the object
void *ptr = [BlueToothObject alloc] init];
return ptr;
}
@implementation BlueToothObject
// override constructor
-(id)init {
self = [super init];
if (self) {
std::cout << "init ObjC" << std::endl;
std::cout << "ObjC running on: " << std::this_thread::get_id() << std::endl;
if ([NSThread isMainThread]) {
std::cout << "we're on the main thread" << std::endl;
} else {
std::cout << "we're not on the main thread" << std::endl;
}
_centralQueue = dispatch_queue_create("centralmanager", DISPATCH_QUEUE_SERIAL);
@autoreleasepool {
dispatch_async(_centralQueue, ^{
std::cout << "inside here";
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_centralQueue options:nil];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
while (([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])) {
}
[[NSRunLoop currentRunLoop] run];
});
};
return self;
}
}explanation: in c++ I dispatched a call to create a new CoreBlueToothObject object in a separate thread. Then I created a new serial dispatch queue that will be used to handle corebluetooth callbacks. Next I initialized the Central Manager using the new queue. Because CoreBluetooth needs a runloop to function and that's how callbacks are sent, I explicitly called a runloop on the current thread (which is not the main thread anymore).