I have a unique use case where I am using CoreBluetooth in my C++ application. There are a lot of old tutorials on CoreBluetooth implementation for objective c and my code looks familiar to this. My problem is that after initializing CBCentralManager, centralManagerDidUpdateState
is not being called.
wrapper.h file for c++
// this file defines c++ functions to be used in the .mm file
void *get_object();
.h file for objective c
#import "wrapper.h"
@interface MyObject : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>
@property(strong, nonatomic) CBCentralManager *centralManager;
- (id)init;
- (void)initialize_manager;
@end
.mm file
// declared in wrapper.h, this is a c++ method to return a void pointer to MyObject
void *get_object() {
void *obj_ptr = [[MyObject alloc] init];
return obj_ptr;
}
// this is the objective c code
@implementation MyObject
- (id)init {
self = [super init];
if (self) {
// this is where the central manager should be created
[self initialize_manager];
}
return self;
}
- (void)initialize_manager {
_centralManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:nil];
}
// this should get called immediately after initialize_manager
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
std::cout << "this never gets called" << std::endl;
NSLog(@"this never gets called");
.cpp file to test it
void *p = get_object();
Summary of code: In my .cpp file I am calling get_object() which initializes a new MyObject and returns a void pointer to it. When initializing a new MyObject, it calls initialize_manager() which should (but currently doesnt call) call centralManagerDidUpdate.
Previous issue I found of centralManagerDidUpdateState not being called: Their problem was they didn't assign the central manager to an instance variable. However, I did so doesn't seem like that applies to me.
Not sure if this is a problem with objective-c++ not handling delegates properly, or if storing MyObject as a void* pointer messes with some internal magic.
Also maybe it is because my program exits too soon? Every implementation of CoreBluetooth I've seen is used in a ViewController, can I replicate the non terminating aspect of a view controller in C++? Maybe it's something to do with async issues? Would appreciate any pointers!
update 1: I think CoreBluetooth may be sharing the same thread as the main c++ application