The method connectPeripheral is defined in a class with CBCentralManagerDelegate. I need to call connectPeripheral from the table view controller when didSelectRowAtIndexPath is selected. Once the peripheral is connected it should continue to execute remaining lines of code in the view controller. I am able to connect to the Peripheral. But before the connection is complete it executes the remaining section of the code. Since the peripheral is not yet connected it does not do the necessary job.
I used dispatch_sync to ensure that the connection is established and then the remaining code is executed but it doesn't work. How can I get around this problem? I am relatively new to IOS programming. Any inputs is highly appreciated.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CBPeripheral *peripheral = nil;
NSArray *devices = [[NSArray alloc] init];
switch (indexPath.section) {
case 0: {
devices = [[BTLECentralManager sharedInstance] foundPeripherals];
peripheral = (CBPeripheral *)[devices objectAtIndex:indexPath.row];
dispatch_sync(syncConnection, ^{
[[BTLECentralManager sharedInstance] connectPeripheral:peripheral];
activePeripheral = [self serviceForPeripheral:peripheral];
NSLog(@"Active Peripheral %@",activePeripheral);
[activePeripheral getTimeDate];
});
break;
}
default:
break;
}
}