I am setting up an app that uses up to 3 external iPhones as controllers via Bluetooth. Everything seems to be set up properly, and the first 2 devices will connect properly, but once I try to connect a third iPhone, it refuses to connect to it.
Is there some limit or some problem with my code? Why can't the third device connect?
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Found peripheral: %@\n\twith data: %@", peripheral, advertisementData);
//Check advertisement data for UUID
CBUUID* serviceUUID = [CBUUID UUIDWithString:serviceUUIDString];
NSArray* serviceUUIDs = [advertisementData valueForKey:CBAdvertisementDataServiceUUIDsKey];
if([serviceUUIDs containsObject:serviceUUID])
{
//Valid controller, connect
NSLog(@"Valid peripheral, connecting...");
[self.btManager connectPeripheral:peripheral options:nil];
//Save peripheral as player #x
if(!self.player2)
{
self.player2 = peripheral;
}
else if(!self.player3)
{
self.player3 = peripheral;
}
else if(!self.player4)
{
self.player4 = peripheral;
//Maximum players connected
//Stop scanning
[self.btManager stopScan];
}
else
{
//Maximum players connected
//Ignore and stop scanning
[self.btManager stopScan];
return;
}
if(!_peripherals)
_peripherals = [NSMutableArray arrayWithCapacity:0];
[_peripherals addObject:peripheral];
}
}
I am storing the peripheral and calling the connection, but it never actually completes the connection. I have tried connecting each of the iPhone in different orders and all 3 devices will connect if they are player 2 or player 3, but player 4 refuses to connect.
Edit:
The second I disconnect any of the connected devices, the player 4 device will connect.