iOS CoreBluetooth同步外设和中央管理器

我正在开发一个应用程序,通过蓝牙同步ios设备之间的信息。 我试过保持CentralManager和PeripheralManager同时运行,当一个管理器连接时,停止另一个管理器。 这有效,但只能间歇性地进行。 在某些时候,CentralManager似乎无法连接到新的外围设备,发现服务等。我回过头来开始使用Apple BTLE-Transfer项目,抽象经理类,然后同时运行。 一切都运作良好,直到它们同时活跃。 这似乎与其他人报道的相似。

在iOS上同时使用外围设备和中央设备

iOS可以同时在同一个应用程序上执行中央和外围设备吗?

目前,如果活动管理器没有任何连接,我每隔5秒就会在中央模式和外围模式之间来回翻转。 连接时,CM停止扫描,PM停止广告。 这似乎更加强大,但我看不到它在后台工作。

有没有人取得更大成功,两位经理同时活跃,或者让“触发器”方法在后台运行。

以下是我在CentralManager中使用的一些代码。 我在PeripheralManager中有类似的代码

-(void)connectionTimer:(NSTimer*)timer{ NSInteger count=[self.connectedPeripherals count]; if (count>0) { NSLog(@"CM Timer connected[%ld]->no switch",(long)count); }else{ NSLog(@"CM Timer no peripheral connected->switch to peripheral"); [self switchToPeripheral]; } } -(void)addPeripheralToConnected:(CBPeripheral*)peripheral{ [self stopDetecting]; self.centralConnected=YES; if (![self.connectedPeripherals containsObject:peripheral]) { [self.connectedPeripherals addObject:peripheral]; peripheral.delegate=self; } } -(void)removePeripheralFromConnected:(CBPeripheral*)peripheral{ [self cleanup:peripheral]; [self.connectedPeripherals removeObject:peripheral]; if ([self.connectedPeripherals count]<1) { self.centralConnected=NO; [self switchToPeripheral]; } } -(void)switchToPeripheral{ [self stopDetecting]; if (!self.peripheralManager) { self.peripheralManager=[TNPeripheralManager sharedInstance]; }else{ dispatch_async(dispatch_get_main_queue(), ^{ [NSTimer scheduledTimerWithTimeInterval:SWITCH_TIME //1 sec target:self.peripheralManager selector:@selector(startBroadcasting) userInfo:nil repeats:NO]; }); } }