0

In my application I want to connect to two CBPeripheral but after the connection is established and notifications are enabled I can send data only to the device that was last connect. I suspect that this is due to I have to create separated CBPeripheralDelegates in order to handle the communication.

because each time I connect peripheral I set peripheral.delegate.self.

Any help guys how to pass the peripheral delegate into another class so I can read/write from each device ?

Part of my connection code with CBPeripheral:

if (!operationLocked)
{
  if ([self isArrayEmpty])
  {
     return FAILED;
  } else {
     //I am calling this recursively
     CBPeripheral *deviceToConnect = [_devicesToConnect objectAtIndex:0];

     deviceToConnect.delegate = self;

     operationLocked = true;
     [_centralManager connectPeripheral:deviceToConnect options:nil];

     return SUCESS;
  }}...and so on
Dimitar Ivanov
  • 133
  • 1
  • 8
  • Your architecture is unclear. Could you be more specific? – Larme Sep 10 '15 at 13:42
  • May be this can help you a bit - http://stackoverflow.com/questions/28582532/major-and-minor-on-altbeacons-under-ios/28582852#28582852 – Kampai Sep 10 '15 at 13:42
  • @Larme Hi Larme my architecture is as follow: Each time application load a scan from central manager is started. When device is found I set that device delegate to self of my class. I think my problem is comming from that the two devices I connect in my case point to the same delegate but I experience some problem to set another CBPeripheralDelegate in another class so I can handle each peripheral with its own delegate. – Dimitar Ivanov Sep 10 '15 at 13:47
  • @Kampai Thanks for the link but its not usefull in my case,peripheral.delegate = self; they also set delegate peripheral to self and if you connect second device with the same delegate you will be able to send data only to the last device you associated with that delegate.What I think is the right approach to have separated CBPeripheralDelegates but I am not sure how to achieve that in objective-c – Dimitar Ivanov Sep 10 '15 at 13:49
  • 1
    The question is how do you keep references for the 2 `CBPeripheral`s? In a `NSArray`? – Larme Sep 10 '15 at 16:19
  • @Larme Thanks for trying to help man, I am storing the two CBPeripherals into NSMutableArray and when I need a peripheral I dig into that array get the item and apply the property and the methods I need to execute.I will share my connection code above – Dimitar Ivanov Sep 10 '15 at 19:05
  • @Larme also do you know if several peripherals can report using the same delegate because I think that my problem is caused reusing the same delegate reference in each different device. – Dimitar Ivanov Sep 10 '15 at 19:22

1 Answers1

0

All of your peripherals can use the same delegate. In the delegate code, you can differentiate between the peripherals with their unique identifiers (peripheral.identifier in code).

You must store each peripheral object in order to be able to write to them, unless the writing were in response to one of the reads, in which case you'd already have the peripheral object in the delegate method.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • thanks for you answer,but that doest explain why I am experiencing my issue the strange thing in the problem I have is that after connecting and setting up all the devices initially I receive data from both of them but I can write only to the last device I connected. – Dimitar Ivanov Sep 10 '15 at 20:02
  • Hi I managed to solve my issue by doing a class for the manager and one class to handle each instance of the CBPeripheral with separated delegates. – Dimitar Ivanov Sep 11 '15 at 08:38