BlackHatSamurai
BlackHatSamurai

Reputation: 23503

centralManager method not being called

I am an Android developer, moving over to iOS, so please bear with me regarding the basics of iOS development.

I have the following code:

Here is the .m file:

#import "BlueToothLEManager.h"
#import "Constants.h"

@implementation BlueToothLEManager

@synthesize mBTCentralManager;


-(void)initializeCBCentralManager{
    NSLog(@"initializing CBCentral Manager"); <--- This is being logged
    mBTCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI); 
}

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
    NSLog(@"Start scan"); <---- This is  NOT being logged. 
    if(central.state != CBCentralManagerStatePoweredOn){
        return;
    }
    if(central.state == CBCentralManagerStatePoweredOn){
        NSLog(@"Scanning for BTLE device"); 
        [mBTCentralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_NAME]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}
@end

Here is the .h file:

#import <Foundation/Foundation.h>

@import CoreBluetooth;

@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>{
    CBCentralManager *mBTCentralManager; 
}

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;


@end

When I call initializeCBCentralManager everything appears to work, but for some reason the centralManagerDidUpdateState method isn't being called. Can someone tell me what I'm doing wrong?

Upvotes: 1

Views: 2259

Answers (1)

Paulw11
Paulw11

Reputation: 114974

You should clean up your property definition as your iVar is getting mixed up with your property. If you declare a property you don't need to declare an iVar. You also don't need @synthesize unless you want a specific name for the backing variable. If you use the self. notation then you can be sure that you are referring to the property and not an iVar.

Your .h file should be -

@import CoreBluetooth;

@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;

@end

Then your .m file will be

#import "BlueToothLEManager.h"
#import "Constants.h"

@implementation BlueToothLEManager


-(void)initializeCBCentralManager{
    NSLog(@"initializing CBCentral Manager"); 
    self.mBTCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI); 
}

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
    NSLog(@"Start scan"); 

    if(central.state == CBCentralManagerStatePoweredOn){
        NSLog(@"Scanning for BTLE device"); 
        [central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_NAME]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}
@end

I have tested this and it works

Upvotes: 1

Related Questions