未调用centralManager方法

时间:2014-12-05 20:32:59

标签: ios xcode bluetooth-lowenergy

我是Android开发人员,转移到iOS,所以请关注iOS开发的基础知识。

我有以下代码:

以下是.m文件:

#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

以下是.h文件:

#import <Foundation/Foundation.h>

@import CoreBluetooth;

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

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;


@end

当我致电initializeCBCentralManager时,一切似乎都有效,但由于某种原因,centralManagerDidUpdateState方法未被调用。有人能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

当您的iVar与您的财产混淆时,您应该清理您的属性定义。如果您声明属性,则不需要声明iVar。除非您需要支持变量的特定名称,否则您也不需要@synthesize。如果您使用self.表示法,那么您可以确定您指的是该属性而不是iVar。

你的.h文件应该是 -

@import CoreBluetooth;

@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>

@property (strong, retain) CBCentralManager *mBTCentralManager;

-(void) initializeCBCentralManager;

@end

然后您的.m文件将

#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

我测试了这个并且它可以正常工作