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);
}
}