0

I'm trying to get my central (iOS device) to communicate with two peripherals (one iOS device, one not). Individually they work fine but I'm finding that once I get both peripherals involved, only the peripheral that was connected to the most recently is able to receive data from the central device. Is there a way to send data from the central to each peripheral without disconnecting and reconnecting the peripheral?

This is my code for writing to peripheral:

- (void) peripheral: (CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBChatacteristic *)characteristic error:(NSError *)error{
    NSString *newValue = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
    NSLog(@"Received: %@ from %@", newValue, peripheral.name);
    CBPeripheral *sender = peripheral;
    if([newValue isEqualToString:@"ready"]){
        NSData *messageValue = [@"challenge dataUsingNSUTF8StringEncoding];
        [sender writeValue:messageValue forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];
        NSLog(@"Challenge sent to %@", sender.name);
    }

Breakpoints indicate the code is being executed and the log shows "challenge sent" to the correct peripheral, it's just that peripheral never receives it.

Code for peripheral receiving:

-(void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *) requests{
    for(int i=0; i<requests.count; i++){
        CBATTRequest *request = requests[i];
        if([request.characteristic.UUID isEqual:_writeCharacteristic.UUID]){
            NSString *stringValue = [[NSString alloc] initWithData:request.value encoding:NSASCIIStringEncoding];
            NSLog(@"Write Request: %@", stringValue);
        }
    }
Hester
  • 133
  • 1
  • 13
  • How do you keep reference of the peripheral? How do you send your data to both peripheral (since I guess you are sending the same data to both)? – Larme Nov 09 '16 at 16:30
  • Please show some code – Paulw11 Nov 09 '16 at 19:00
  • There's quite a lot of code and it's spread around so it's difficult to share. Which particular part of code is relevant? I'm sending different data to the peripherals. Essentially one peripheral is used to trigger the central to send data to another. The central is always able to receive data from each peripheral, just not send to both. By send I mean [peripheral writeValue:forCharacteristic:type:] and by receive I mean peripheral didUpdateValueForCharacteristic callback. – Hester Nov 10 '16 at 08:30
  • Show the code when you write to the peripheral. – Larme Nov 10 '16 at 12:18
  • OK I added it to the original question – Hester Nov 10 '16 at 13:19

1 Answers1

0

To answer your question: Yes it is possible that you can have multiple connections to different peripherals and read/write to them. iOS devices can handle up to 8 simultaneous connections.

For the implementation, have a look at the Core Bluetooth Programming Guide from Apple. All the things you need are explained there.

Just as a suggestion: If multiple devices (let's call them B and C) should receive data from 1 device (called A), I would use the peripheral role on device A that needs to send the data to the others. Because then devices B and C can scan, connect and subscribe to a characteristic and receive updates without having to read again.

p2pkit
  • 1,159
  • 8
  • 11
  • Hi p2pkit. The reason I can't have A as the peripheral is that if B is connected, then C will not be able to see it and vice versa. Do you have a particular reference in the Core Bluetooth guide? I've been using it a lot and haven't found anything specifying how to handle multiple connections like this. – Hester Nov 21 '16 at 11:28