我是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
方法未被调用。有人能告诉我我做错了什么吗?
答案 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
我测试了这个并且它可以正常工作