我正在开发我的第一个蓝牙应用程序。现在,我有一个 AppUIview,它实现了一个按钮,该按钮调用 AppCentralManager 中的一个函数,其中蓝牙功能将被实现:
- (IBAction) Scan{
NSLog(@"scan function");
[[AppBluetoothCenter alloc]initialize];
}
在 AppBluetoothCenter.h 我已经声明了这些功能:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreBluetooth/CBService.h>
#import "AppDelegate.h"
@interface AppBluetoothCenter : NSObject <UIApplicationDelegate,CBCentralManagerDelegate, CBPeripheralDelegate>
@property CBCentralManager* CentralManager;
- (void) initialize;
@end
在 AppBluetoothCenter.m 中,我实现了以下功能:
#import "AppBluetoothCenter.h"
@implementation AppBluetoothCenter
-(void)initialize{
NSLog(@"initialize");
_CentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state == CBCentralManagerStatePoweredOff){
NSLog(@"BLE OFF");
}
else if (central.state == CBCentralManagerStatePoweredOn){
NSLog(@"BLE ON");
}
else if (central.state == CBCentralManagerStateUnknown){
NSLog(@"NOT RECOGNIZED");
}
else if(central.state == CBCentralManagerStateUnsupported){
NSLog(@"BLE NOT SUPPORTED");
}
}
@end
在我收到的日志控制台中运行应用程序:
AppBluetooth[1273:60b] 扫描函数
AppBluetooth[1273:60b] 初始化
为什么 centralManagerDidUpdateState 不被调用?