iOS:沟通从不同的视图控制器使用corebluetooth库iOS:Communicate from different view controllers using corebluetooth library

- 此内容更新于:2015-11-28


i am doing corebluetooth application. And developed the library using corebluetooth framework.i have 4 view controllers .In the first view controller i have scan button when i click scan button second screen appears with scanning devices and those devices are appeared in the tableview. when i click the table view peripheral connects and provides its info like services and charactestics.When i came back to the first view controller i had a button called battery status when i click that button it should call the battery information from the second view controller connected peripheral. Following is my code in second view controller

     { coreBle = [[CoreBLE alloc] init];   
     coreBle.delegate = self;

and for connectng the devices

        - (IBAction)Go:(id)sender
     if (periperal==nil) {
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil    
     message:@"Select watch to connect"
     cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];

   [coreBle connectToPeripheralAt:[periperal integerValue]];
    NSLog(@"peripheral connected is %@",periperal);
    HUD.labelText = @"Connecting...";

    [HUD showWhileExecuting:@selector(connectingtask) onTarget:self 
    withObject:nil animated:YES];



conncectng the peripheral device from did select row at index path value as below

    - (void)tableView:(UITableView *)tableView   
   didSelectRowAtIndexPath:(NSIndexPath   *)indexPath


   deviceselected= [NSString stringWithFormat:@"Table cell pressed.   
   (%d)", indexPath.row];
   periperal=[NSString stringWithFormat:@"%d",indexPath.row];

The above code is for connecting the peripheral in the second view controller .The same peripheral should connected in the first view controller .So that i can retrive values from it.Please help me


Why not move the CoreBLE code to a single instance alone in a new class. And in the four viewcontrollers just call the relative methods to get the information about the CoreBLE. Because in your ui operation, you must do followering the four view controllers sequence.


(原文:Corble class is in one class that act as a library and from that only retriving method s i have needed and when doing that app is getting crashed as giving values as[CBCentralManager connectPeripheral:options:], /SourceCache/CoreBluetooth/CoreBluetooth-242.1/CBCentralManager.m:328 2015-01-26 13:58:44.170 SmartWatch[1358:266327] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: peripheral != nil')