Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will the flow be like for connecting two BLE Peripherals?

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?

like image 405
Jobs Avatar asked Jan 22 '16 11:01

Jobs


1 Answers

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

like image 130
Joe Hallenbeck Avatar answered Oct 05 '22 11:10

Joe Hallenbeck