简体   繁体   中英

CoreBluetooth read value of specific characteristic while reading other characteristcs

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM