我有一个视图控制器,当您按下按钮时会启动蓝牙扫描。
以下是.h
文件:
#import <UIKit/UIKit.h>
@import CoreBluetooth;
@interface ViewControllerIntroPage2 : UIViewController{
IBOutlet UIButton *scanForFetchTagsButton;
}
@property (nonatomic, retain) UIButton *scanForFetchTagsButton;
@property (nonatomic, retain) CBCentralManager *mCentralManager;
-(IBAction)scanButtonPressed:(id)sender;
@end
以下是.m
文件:
#import "ViewControllerIntroPage2.h"
#import "BlueToothLEManager.h"
@interface ViewControllerIntroPage2 ()
@end
@implementation ViewControllerIntroPage2
@synthesize scanForFetchTagsButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)scanButtonPressed:(id)sender {
NSLog(@"Scan Button Clicked");
self.mCentralManager = [[BlueToothLEManager alloc]initializeCBCentralManager];
NSLog(@"Scan Done");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
这是我的BluetoothLEManager文件:
.h
档案:
#import <Foundation/Foundation.h>
@import CoreBluetooth;
@import QuartzCore;
@interface BlueToothLEManager : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>
@property (strong, retain) CBCentralManager *mBTCentralManager;
-(CBCentralManager*) initializeCBCentralManager;
@end
.m
档案:
#import "BlueToothLEManager.h"
#import "Constants.h"
@implementation BlueToothLEManager
-(CBCentralManager*)initializeCBCentralManager{
NSLog(@"initializing CBCentral Manager");
return 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
问题是应用程序不断崩溃/暂停。有时它会抛出一个unrecognized selector sent to instance error
,但并不总是会发生。我已经检查过以确保该按钮没有问题,因为当我删除self.mCentralManager = [[BlueToothLEManager alloc]initializeCBCentralManager];
该应用时,该应用并未崩溃。
任何人都可以告诉我为什么会发生此次崩溃以及如何解决这个问题?
答案 0 :(得分:2)
您的代码有多个问题。现在你没有保留对BluetoothLEManager实例的引用,这意味着它将在你从scanButtonPressed:
返回后不久被释放,但该实例仍然是CBCentralManager的委托,具体取决于CBCentralManager如何存储其委托这可能导致崩溃或不会调用任何委托方法。通常不保留委托,因此如果实例被释放,CBCManager将访问无效内存或将其委托设置为nil。
您应该保留对BluetoothLEManager实例的引用,而不是在ViewController中保留对CBCentralManager的另一个引用。这将解决您的第一个问题,如果您保持其周围的实例将不会被解除分配并且将正确调用CBCentralManagerDelegate方法。
你正在使用错误的分配模式。在Objective-C中,如果你调用alloc,你必须调用完整的交易,例如[[Class alloc] init]
或[[Class alloc] initWithSomething:someThing]
。以init开头的方法必须返回自己的Class。
这些是我现在可以发现的问题。
我已修复并现代化您的代码:
BTMgr.h
#import <Foundation/Foundation.h>
@import CoreBluetooth;
@interface BlueToothLEManager : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>
@property (strong, readonly) CBCentralManager *mBTCentralManager;
@end
BTMGr.m
#import "BlueToothLEManager.h"
//#import "Constants.h"
@implementation BlueToothLEManager
- (instancetype)init {
self = [super init];
NSLog(@"initializing BluetoothLEManager");
_mBTCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
return self;
}
#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
VC.h
#import <UIKit/UIKit.h>
@interface ViewControllerIntroPage2 : UIViewController
@property (nonatomic, strong) UIButton *scanForFetchTagsButton;
-(IBAction)scanButtonPressed:(id)sender;
@end
VC.m
#import "ViewControllerIntroPage2.h"
#import "BlueToothLEManager.h"
@interface ViewControllerIntroPage2 ()
@property (strong) BlueToothLEManager *bluetoothManager;
@end
@implementation ViewControllerIntroPage2
-(IBAction)scanButtonPressed:(id)sender {
NSLog(@"Scan Button Clicked");
if (!self.bluetoothManager) {
// create if it doesn't exist
self.bluetoothManager = [[BlueToothLEManager alloc] init];
}
NSLog(@"Scan Done");
}
@end