我创建一个继承自NSObject
并添加delegate
方法的类。在我的课堂上,我想使用 CBCentralManager 及其委托方法。但是委托方法没有被调用。这是我的代码 -
这是 VZBluetooth.h
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@protocol BluetoothDelegate <NSObject>
@required
-(void)getBluetoothStatus:(NSString*)status;
@end
@interface VZBluetooth : NSObject<CBCentralManagerDelegate, CBPeripheralDelegate>
@property (nonatomic, strong) id<BluetoothDelegate> delegate;
-(void)callBluetooth;
@end
对于 VZBluetooth.m
@implementation VZBluetooth
{
NSString *status;
CBCentralManager *ce;
}
@synthesize delegate = _delegate;
-(void)callBluetooth
{
ce = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
#pragma mark - Bluetooth Delegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
if(central.state == CBCentralManagerStatePoweredOn){
if ([central respondsToSelector:@selector(scanForPeripheralsWithServices:options:)]) {
status = CASE_STATUS_PASS;
}
else{
status = CASE_STATUS_FAIL;
}
}
else{
status = CASE_STATUS_FAIL;
}
if ([self.delegate respondsToSelector:@selector(getBluetoothStatus:)]) {
[self.delegate getBluetoothStatus:status];
}
}
我的电话——
VZBluetooth *blu = [[VZBluetooth alloc]init];
[blu callBluetooth];
blu.delegate = self;