0
votes

In my application, I need to read/write values from more than one peripherals that have the same service and characteristics UUIDs. Each peripheral has many characteristics, out of which value of one particular characteristic is used to uniquely identify the peripheral. What I am trying to figure out is

  • How do I connect to all the peripherals that have the same service uuids.
  • Is it possible to read the value of the characteristic used to uniquely identify the peripheral while reading the value of other characteristics, so that I can distinguish for which peripheral the characteristics values are updated.
1
When each peripheral is discovered you will get an instance of CBPeripheral passed to your delegate method. This object has an identifier property that you can use to uniquely identify that peripheral. When you get a callback to your CBPeripheralDelegate method with data, that same CBPeripheral instance will be passed to the delegate method, so you can determine which peripheral the data came from.Paulw11
Thanks Paul. The identifier property will be changing as Andrea mentioned in his answer. Is it possible to read value of another characteristic after reading/subscribing to one characteristics ?saikamesh
Once you have connected to the peripheral you can read any characteristic you like, but I have an app that stores it’s target peripheral identifier in userdefaults and the identifier hasn’t changed over a period of yearsPaulw11

1 Answers

1
votes

Once you have launched the scan for a specific service and you set the CBCentralManager delegate, you are going to receive this callback:

optional func centralManager(_ central: CBCentralManager, 
                 didDiscover peripheral: CBPeripheral, 
           advertisementData: [String : Any], 
                        rssi RSSI: NSNumber)

Here you are getting the peripheral that exposes your specific service and you can connect to it, by simply calling on the the CBCentralManager passing the peripheral you are going to connect:

func connect(_ peripheral: CBPeripheral, 
     options: [String : Any]? = nil)

Pay attention that you can connect maximum on ten peripheral (if I remember well).
How you read characteristic and manage how to store them using different peripherals it's up to you. To uniquely identify a peripheral you can use the identifier property, this is a computed property based on the mac address. The identifier is not guaranteed to be fixed in a lifetime.