2013-06-21 92 views
2

我第一次使用coreBluetooth。 這裏是我的實現是否有任何錯誤的地方。每次掃描藍牙設備時都會給CBCentralManagerStateUnknown?

@synthesize CM,activePeripheral; 

- (id)init 
{ 
    if ((self = [super init])) 
    { 
     CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()]; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (int) scanForPeripherals 
{ 

    if (self.CM.state != CBCentralManagerStatePoweredOn) 
    { 
     NSLog(@"CoreBluetooth is %s",[self centralManagerStateToString:self.CM.state]); 
     return -1; 
    } 

    [self.CM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}]; 
    return 0; 
} 

- (const char *) centralManagerStateToString: (int)state 
{ 
    switch(state) 
    { 
     case CBCentralManagerStateUnknown: 
      return "State unknown (CBCentralManagerStateUnknown)"; 
     case CBCentralManagerStateResetting: 
      return "State resetting (CBCentralManagerStateUnknown)"; 
     case CBCentralManagerStateUnsupported: 
      return "State BLE unsupported (CBCentralManagerStateResetting)"; 
     case CBCentralManagerStateUnauthorized: 
      return "State unauthorized (CBCentralManagerStateUnauthorized)"; 
     case CBCentralManagerStatePoweredOff: 
      return "State BLE powered off (CBCentralManagerStatePoweredOff)"; 
     case CBCentralManagerStatePoweredOn: 
      return "State powered up and ready (CBCentralManagerStatePoweredOn)"; 
     default: 
      return "State unknown"; 
    } 

    return "Unknown state"; 
} 


- (void) connectPeripheral:(CBPeripheral *)peripheral { 
    printf("Connecting to peripheral with UUID : %s\r\n",[self UUIDToString:peripheral.UUID]); 

    self.activePeripheral = peripheral; 
    self.activePeripheral.delegate = self; 
    [self.CM connectPeripheral:self.activePeripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]]; 
} 


-(const char *) UUIDToString:(CFUUIDRef)UUID 
{ 
    if (!UUID) 
     return "NULL"; 

    CFStringRef s = CFUUIDCreateString(NULL, UUID); 
    return CFStringGetCStringPtr(s, 0); 
} 


#pragma mark -Central manager delegate method 

- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
{ 

    NSLog(@"hits it"); 
    if (central.state != CBCentralManagerStatePoweredOn) { 
     return; 
    } 

    isOn=YES; 
} 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
{ 
    NSLog(@"Received periferal :%@",peripheral); 
    NSLog(@"Ad data :%@",advertisementData); 

    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
} 


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
{ 
    NSLog(@"Connected peripheral %@",peripheral); 
} 


- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 
{ 
    NSLog(@"Error occured :%@",[error localizedDescription]); 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)scanDevices:(id)sender { 
    if (isOn) { 
     [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
     [self scanForPeripherals]; 
    } 
} 

每次它給登陸

CoreBluetooth is State unknown (CBCentralManagerStateUnknown) 

更新質疑

由@邁克爾Dautermann給出的建議後,我才知道,我的centralManagerDidUpdateState委託方法是不是hitting.I不知道爲什麼會發生這種情況。如果有人發現原因,請通知我。

謝謝你。

回答

3

CoreBluetooth只是在啓動過程中。

您需要此委託方法首先擊中:

- (void) centralManagerDidUpdateState: (CBCentralManager *) central; 

而且一旦它,然後可以爲外設開始掃描。

+0

是否有可能,它不支持iPhone 4 – Warewolf

+0

我正在用iphone4運行最新的ios 6.1.3。 – Warewolf

+1

iPhone 4沒有BLE。從4S開始,這就是問題所在。 – Aboelseoud

0

你在哪裏打電話scanDevices?我認爲你說得太早。你應該在centralManagerDidUpdateState之內調用它。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
{ 

    NSLog(@"hits it"); 
    if (central.state != CBCentralManagerStatePoweredOn) { 
     [self scanDevices]: 
    } 

    isOn=YES; 
} 
+0

由於下面的答案是說,我應該調用centralManagerDidUpdateState之後我調用它,但如果centralManagerDidUpdateState沒有調用,那麼我該怎麼辦? – Warewolf

+0

如果藍牙處於打開狀態,centralManagerDidUpdateState會自動調用。 – Aboelseoud

+0

當藍牙關閉時,它也會打電話 – Warewolf

1

幾天後,我檢查了CentralManager的初始化,我才知道在firstViewController是init()方法從未called.I簽出在init方法打印日誌。

- (id)init 
{ 
    NSLog(@"hello init"); 
    if ((self = [super init])) 
    { 
     CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()]; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()]; 
    isOn=NO; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

所以,我初始化viewDidLoad中的中央管理器,現在它被初始化,centralManagerDidUpdateState終於called.Now打印CBCentralManagerStateUnsupported這不是問題,因爲iPhone 4不支持it.Thanks邁克爾幫了我很多。