Home:ALL Converter>iOS CoreBluetooth Simultaneous Peripheral and Central Managers

iOS CoreBluetooth Simultaneous Peripheral and Central Managers

Ask Time:2014-01-31T02:42:34         Author:mflac

Json Formatter

I am developing an app to synchronize information between ios devices via bluetooth. I have tried keeping the CentralManager and PeripheralManager running simultaneously, and when one manager connects, stopping the other. This works, but only intermittantly. At some point the CentralManager seems to have trouble connecting to new peripherals, discovering services etc.I went back a started with the Apple BTLE-Transfer project, abstracting manager classes and then running thm simultaneously. Everything works well until they are both active simultaneously. This seems to be similar to what others have reported.

Peripheral and central at the same time on iOS

Can iOS do central and peripheral work on same app at same time?

Presently I am flip-flopping back and forth between central and peripheral mode every 5 seconds if the active manager does not have any connections. Upon connection CM stops scanning and PM stops advertising. This seems to be much more robust, but I can't see it working in the background.

Has anybody had more success, with both managers be active simultaneously, or a method of making the "flip-flop" method work in the background.

Here is some of the code I am using in the CentralManager. I have analogous code in the 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];
        });
    }
}

Author:mflac,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21464921/ios-corebluetooth-simultaneous-peripheral-and-central-managers
yy