2014-12-05 69 views
1

我是Android開發人員,轉向iOS,因此請耐心等待iOS開發的基礎知識。centralManager方法不被稱爲

我有以下代碼:

這裏是.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方法沒有被調用。有人能告訴我我做錯了什麼嗎?

+0

什麼是你'mBTCentralManager的聲明財產?擺脫'@ synthesize'並使用'self.mBTCentralManager'。你在運行什麼設備? – Paulw11 2014-12-05 20:37:18

+0

@ Paulw11這是在4S上進行測試。查看財產編輯。 – BlackHatSamurai 2014-12-05 20:39:08

回答

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 

我測試了這一點,它的工作原理

+0

首先,謝謝你的幫助!我做了你所建議的改變,但我仍然無法獲得調用'centralManagerDidUpdateState'方法。 – BlackHatSamurai 2014-12-05 21:21:29

+1

你如何實例化'BluetoothLEManager'類並調用'initializeCBCentralManager'?如果你沒有在屬性中保留對這個類實例的引用,那麼它將被釋放。正如我所說我已經測試了上面的代碼,它發現設備 – Paulw11 2014-12-05 21:23:47

+1

我認爲這可能是問題,再次,我是這個整個iOS的新東西,所以感謝您的耐心。具體來說,我打電話給'[[BlueToothLEManager alloc] initializeCBCentralManager];'。我從你最後的評論中猜測,這就是爲什麼它不起作用。 – BlackHatSamurai 2014-12-05 21:25:28