IOS CoreBluetooth Simultaneous Peripheral and Central Managers

I am developing an application to synchronize information between ios devices via bluetooth. I tried the simultaneous operation of CentralManager and PeripheralManager, and when one manager connects, stopping the other. It works, but only intermittently. At some point, CentralManager had problems connecting to new peripherals, discovering services, etc. I returned with the Apple BTLE-Transfer project, abstracting the manager classes and then running thm at the same time. Everything works well until they are simultaneously active. This is similar to what others have reported.

Peripheral and central at the same time in iOS

Can iOS perform central and peripheral work in the same application at the same time?

Currently, I switch between central and peripheral modes every 5 seconds if the active manager has no connections. When connected, CM stops scanning, and PM stops advertising. This seems a lot more reliable, but I don't see it running in the background.

Someone had more success when both managers were active at the same time, or the method of creating the β€œtrigger” method works in the background.

Here are some of the code that I use in CentralManager. I have similar code in 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]; }); } } 
+1
source share

Source: https://habr.com/ru/post/1203273/


All Articles