7

I have been reading a lot of posts here on the forum and I saw quite a few relating to my case. However I still don't have the clarity that I was looking for.

I want to connect to two CBPeripherals and to write data to both of them. From what I have read, I have the idea that before connecting to a second device I have to disconnect the current peripheral. Okay, so suppose I were to write a command onto one of the peripherals and then I want to write another command to the other one, will I have to disconnect from the current peripheral? If I did disconnect to connect to the other, will the previous command still hold effect? What are the best practises for this on iOS?

Jobs
  • 583
  • 5
  • 26
  • I was working with two BLE devices at the same time, so you don't have to disconnect from any of them – OlDor Jan 30 '16 at 21:23

1 Answers1

2

my bluetooth friend, first of all it isn't necessary to disconnect current Peripheral to connect another if u want to send both messages. But many apps limit number of connected devices(CBPeripheral) to 5 - 10, because more than 5-10 connected devices, can spontaneously be lost, I know about it a little (I worked only with 4 devices). For example:

[[RKCentralManager sharedManager] scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@NO}  onUpdated:^(RKPeripheral *peripheral)
{
  //first of all u should start a scan

  [[RKCentralManager sharedManager] connectPeripheral: peripheral options:nil onFinished:^(RKPeripheral * connectedperipheral, NSError *error) 
     {
      //after u can connect to Peripheral immediately

         [connectedperipheral discoverServices:nil onFinish:^(NSError *error) 
         {
           // services - a collection of data and associated behaviors for accomplishing a function or feature of a device

              [connectedperipheral discoverCharacteristics:nil forService: [connectedperipheral.services lastObject] onFinish:^(CBService *service, NSError *error) 
              {
                //after u should take a characteristic - Represents a service's characteristic
                CBCharacteristic * characteristic = service.characteristics[0];

                //and at last u can write value in characteristic in which you are going to write down something
                 NSData * data = [NSData  dataWithHexString: newstring];
                 CBCharacteristicWriteType type = CBCharacteristicWriteWithoutResponse;
                 [connectedperipheral writeValue:data forCharacteristic:characteristic type:type onFinish:nil];

              }];

         }];

     }];

}];

The approximate scheme of sending the message for bluetooth device, it isn't obligatory to do an investment of methods, they can be distributed on actions.

You shouldn't worry about connection and sendings data to several devices because it is work for CBCentralManager, if U use it correctly.

CBCentralManager objects are used to manage discovered or connected remote peripheral devices (represented by CBPeripheral objects), including scanning for, discovering, and connecting to advertising peripherals.

You can connect at once some devices and send them messages, and all be ok. If you have questions, will try to answer.

This is good example, u can see how its work : https://github.com/ruiking/ble

About max count of devices https://stackoverflow.com/a/17282862/4912496

Community
  • 1
  • 1
Joe Hallenbeck
  • 1,442
  • 11
  • 20
  • Thanks for the answer brother! I'm hoping not to use the library though as it would add another layer to the app. There must be a way to manage with the native functions right? – Jobs Feb 01 '16 at 11:36
  • @Jobs https://github.com/ruiking/ble this not a library, its simply project it is possible to take on for a tag some decisions, I wish you good luck with BLE it really difficult and very few people are engaged in it. – Joe Hallenbeck Feb 01 '16 at 11:40
  • Oh okay, let me explore more thoroughly! BLE is a piece of cake once you get the hang of it. Until then though, cheers! I'll hit you up if I run into any issues I can't solve. – Jobs Feb 01 '16 at 12:08
  • @JoeHallenbeck hey Joe, have you got any examples of how to handle two BLE peripherals in Swift ? – richc Oct 06 '17 at 16:40