Question

I am trying to use the CoreBluetooth framework and I have created a helper class (btHelper) to make code more maintainable and such.

The problem is that in this helper class, the delegate methods are no longer being called like they were originally when everything was smushed into big class. Yes I have set the delegate method in .h of the helper class and yes I have set the object CBCentralManager delegate to self. I have pretty much done everything the same as when it was all in one class. I believe it has something to do with the main thread but I have very little experience in this.

Specifically, the delegate method that I want called is

-(void)centralManagerDidUpdateState:(CBCentralManager *)central 

Can anyone please tell me what I must do to get delegate calling working as it should in this helper class? Thanks!

The following is the PrimaryViewController class and the btHelper class

btHelper.m

-(void) activateBluetooth
{
self.manager= [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
  //DELEGATE METHOD THAT NEVER GETS CALLED. SHOULD BE CALLED AS SOON AS self.manager is initiated
   self.isAvailable=FALSE;
   switch (central.state) {
    case CBCentralManagerStatePoweredOff:
        NSLog(@"CoreBluetooth BLE hardware is powered off");
        break;
    case CBCentralManagerStatePoweredOn:
        NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
         self.isAvailable=TRUE;
        break;
    case CBCentralManagerStateResetting:
        NSLog(@"CoreBluetooth BLE hardware is resetting");
        break;
    case CBCentralManagerStateUnauthorized:
        NSLog(@"CoreBluetooth BLE state is unauthorized");
        break;
    case CBCentralManagerStateUnknown:
        NSLog(@"CoreBluetooth BLE state is unknown");
        break;
    case CBCentralManagerStateUnsupported:
        NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
        break;
    default:
        break;
}

}

PrimaryViewController.m

-(IBAction)connect
{  
  btHelper *bluetoothManager= [[btHelper alloc]init];
  [bluetoothManager activateBluetooth];

}
Was it helpful?

Solution

You helper instance is a local variable in the -connect method. So ARC release it (and the CBCentralManager instance) before you can receive delegate methods. Make your helper an instance variable (or property) of PrimaryViewController, and this will fixed your problem.

@interface PrimaryViewController : UIViewController
{
    // BTHelper instead of btHelper, to follow naming convention
    BTHelper *_bluetoothManager;
}
@end

@implementation PrimaryViewController
    -(IBAction)connect
    {
      // FIXME: this don't check if _bluetoothManager is already instantiated
      _bluetoothManager = [[BTHelper alloc] init];
      [_bluetoothManager activateBluetooth];
    }
@end

OTHER TIPS

Ensure that your btHelper.m adopt the CBCentralManagerDelegate protocol.

@interface btHelper () <CBCentralManagerDelegate> {}

In your case above, most likely you did not get any errors with the method:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

since as of a few versions ago, the compiler stopped requiring that you declare any private methods (in the interface section of your .m file).

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top