iOS:Communicate from different view controllers using corebluetooth library

浪尽此生 提交于 2019-12-12 03:25:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/28146105/ioscommunicate-from-different-view-controllers-using-corebluetooth-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!