我有一個視圖控制器,當您按下按鈕時啓動藍牙掃描。centralManagerDidUpdateState拋出無法識別的選擇器發送到實例錯誤
這裏是.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];
時,該應用不會崩潰。
任何人都可以告訴我爲什麼這個崩潰正在發生,我該如何解決它?
謝謝你的精彩解釋。我是iOS編程新手,在Android開發方面我有很強的背景,所以對我來說這是一個新點子。謝謝你的迴應。如果您知道任何能夠幫助我適應iOS的閱讀材料或信息,我不會抱怨。再次感謝。 – BlackHatSamurai 2014-12-06 01:07:58