title: BLE Peripheral design on iOS
body: <p>I am working on a Bluetooth Low Energy (BLE) project which needs iPhone to act as a peripheral device. I am using <a href="https://github.com/Hassaniiii/BLEManager" rel="nofollow noreferrer">BLEManager</a> to deal with <em>CoreBluetooth</em> related jobs, which includes good implementations for both <em>Central</em> and <em>Peripheral</em> mode.</p>
  
  <p><strong>Question</strong>:</p>
  
  <p>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:</p>
  
  <ul>
  <li>define multiple services, with one <em>read/write/notify</em> characteristic</li>
  <li>define one service, with some <em>read/write/notify</em> characteristics</li>
  <li>define one service, one characteristic for <em>read/write/notify</em>, and extract commands from the characteristic updated value</li>
  </ul>
  
  <p>Which one shall I choose?</p>
  
  <p><strong>Implementation</strong></p>
  
  <p>Here are some parts of my peripheral implementation using <a href="https://github.com/Hassaniiii/BLEManager" rel="nofollow noreferrer">BLEManager</a>:</p>
  
  <pre><code>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()
  </code></pre>