Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate not called on different thread

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];

}
like image 527
Teddy13 Avatar asked Dec 18 '13 00:12

Teddy13


1 Answers

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
like image 124
Emmanuel Avatar answered Oct 16 '22 23:10

Emmanuel