Reputation: 153
I have several custom BLE LED lights (peripherals). I've implemented a bluetooth helper class (CBCentralManager and associated delegate methods), and can connect and control each light individually without issue. I have handled all of the basic retrieve / connect / notify logic, and it works great.
My next step was to connect to multiple lights and control them essentially simultaneously (or as near as possible). To manage the different active peripherals, I created a dictionary to store them in, using the UUID as the key like so (BLEDevice contains a peripheral property where I store the reference):
class BLEDevice {
var uuid: String!
var peripheral: CBPeripheral!
var rssi: NSNumber!
var name: String!
var advertData: [String: Any]!
var type = 0
init() {
}
}
var bleDeviceDict: [String: BLEDevice] = [:]
This dictionary gets populated during the retrieval process, and then I do a connection request for each one. Upon success of connection, service discovery, characteristic discovery...etc, I update the dictionary object. I'm able to connect to each device without any issue, but when I attempt to send any commands (with or without a response flag) I get unreliable operation. Sometimes it works great, and other times it throws the following error:
[CoreBluetooth] WARNING: <CBCharacteristic: 0x129701430, UUID = FFE1, properties = 0x1C, value = <45080154>, notifying = YES> is not a valid characteristic for peripheral <CBPeripheral: 0x127e0ad70, identifier = 44CDB018-2A5C-D776-7641-7F193470945A, name = 3CA5090A21AFED, state = connected
>
To send the group commands, I set up a basic for loop to iterate through and send the command to each peripheral, and I'm almost certain this brute force approach is the wrong way to accomplish this. I've done some searching, but have been unable to find any literature on the proper way to control devices at the same time. I could really use some advice or direction to help solve this problem. What is the best practice for achieving this behavior?
Upvotes: 2
Views: 414
Reputation: 114875
You need to store a specific instance of the relevant characteristic for each peripheral you are connected to.
Add properties to your BLEDevice
to store references to the relevant characteristics you find during discovery.
There is no other way to send the data to each of the peripherals; You need to iterate over your peripheral collection and write the data to each one.
Upvotes: 3