iosdeveloper
iosdeveloper

Reputation: 99

iOS:Communicate from different view controllers using corebluetooth library

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

     -(void)viewdidload
     { 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"
                                                delegate:self   
     cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
      }
    else{

   [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

Upvotes: 0

Views: 493

Answers (1)

Feng Lin
Feng Lin

Reputation: 593

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.

Upvotes: 1

Related Questions