connectPeripheral 方法在具有 CBCentralManagerDelegate 的类中定义。I need to call connectPeripheral from the table view controller when didSelectRowAtIndexPath is selected. 连接外围设备后,它应该继续执行视图控制器中剩余的代码行。我能够连接到外围设备。但在连接完成之前,它会执行代码的剩余部分。由于外围设备尚未连接,因此它没有完成必要的工作。
我使用 dispatch_sync 来确保建立连接,然后执行剩余的代码,但它不起作用。我怎样才能解决这个问题?我对IOS编程比较陌生。任何输入都受到高度赞赏。
- (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;
}
}