我是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
方法沒有被調用。有人能告訴我我做錯了什麼嗎?
什麼是你'mBTCentralManager的聲明財產?擺脫'@ synthesize'並使用'self.mBTCentralManager'。你在運行什麼設備? – Paulw11 2014-12-05 20:37:18
@ Paulw11這是在4S上進行測試。查看財產編輯。 – BlackHatSamurai 2014-12-05 20:39:08