I am working on a Bluetooth Low Energy (BLE) project which needs iPhone to act as a peripheral device. I am using BLEManager to deal with CoreBluetooth related jobs, which includes good implementations for both Central and Peripheral mode.
Question:
I don't have any idea about design patterns and best practices of a peripheral's architecture. There is no pattern for the definition of Services and Characteristics. It seems I can choose one of the following patterns:
- define multiple services, with one read/write/notify characteristic
- define one service, with some read/write/notify characteristics
- define one service, one characteristic for read/write/notify, and extract commands from the characteristic updated value
Which one shall I choose?
Implementation
Here are some parts of my peripheral implementation using BLEManager:
let peripheralServiceUUID = CBUUID(string: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
let nameCharacteristicUUID = CBUUID(string: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
var CharactersticArray = [MyCharacterstic]()
characteristic.uuid = nameCharacteristicUUID
characteristic.value = nil
characteristic.permission = [.writeable, .readEncryptionRequired]
characteristic.property = [.write, .read, .notify]
CharactersticArray.append(characteristic)
Peripheral.instance().service_UUID = peripheralServiceUUID
Peripheral.instance().service_characteristics = CharactersticArray
Peripheral.instance().localName = "iPhone"
Peripheral.instance().startAdvertising()