title: BLE: How to get correct service UUID and characteristic UUID?
body: <p>I am a new in Bluetooth connection and I want to add second device in my iOS project. I already have one device, and the new device is very similar with first one, but little bit deferent. I have one process for the two devices, and I did not change a lot of code, just created all value for the new device. My all devices have different name and identifier, first device is working fine.</p>
  <p>For creating UUID values, I used UUID generator (<a href="https://www.guidgenerator.com/online-guid-generator.aspx" rel="nofollow noreferrer">https://www.guidgenerator.com/online-guid-generator.aspx</a>).</p>
  <pre><code>class BleConstants: NSObject {
  let deviceTwoServiceUUID = “59DE3994-6A63-4654-8FF0-F85C5163B2F5”
  let deviceTwoFirstCharacteristicUUID = “59DE3994-6A63-4654-8FF0-F85C5163B2F6”
  let deviceTwoSecondCharacteristicUUID = “59DE3994-6A63-4654-8FF0-F85C5163B2F7”
  let deviceOneServiceUUID = “A6AF4483-E210-457B-B9D6-B8A621513D1D”
  let deviceOneFirstCharacteristicUUID = “A6AF4483-E210-457B-B9D6-B8A621513D2D”
  let deviceOneSecondCharacteristicUUID = “A6AF4483-E210-457B-B9D6-B8A621513D2D”
  }
  
  class BleManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
  @objc private(set) static var sharedInstance = BleManager()
  var cbManager : CBCentralManager? = nil
  var currentPeripheral : CBPeripheral? = nil
  var secondService : CBService? = nil
  var firstService : CBService? = nil
  var secondFirstCharacteristic : CBCharacteristic!
  var secondSecondCharacteristic : CBCharacteristic!
  var firstFirstCharacteristic : CBCharacteristic!
  var firstSecondCharacteristic : CBCharacteristic!
  
  func initCentralManager() {
      if cbManager == nil {
          cbManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : “MyApp”, CBCentralManagerOptionShowPowerAlertKey: true])            
      }
  
  func deinitCentralManager() {
      cbManager = nil
  }
  
  func isBluetoothAvailable() -&gt; Bool {
      return cbManager?.state == CBManagerState.poweredOn
  }
  
  func scan() {
      if (cbManager != nil &amp;&amp; (cbManager?.isScanning)!) {
          return
      }
      
      discoveredPeripherals.removeAll()
      
      let serviceUUIDs = [CBUUID(string: BleConstants.deviceTwoServiceUUID), CBUUID(string: BleConstants.deviceOneServiceUUID)]
      
      cbManager?.scanForPeripherals(withServices: serviceUUIDs,
                                    options: [CBCentralManagerScanOptionAllowDuplicatesKey : 1])
  }
  
  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
      if(!discoveredPeripherals.contains(peripheral)) {
          discoveredPeripherals.append(peripheral)
      }
  }
  
  func stopScan() {
      if cbManager != nil &amp;&amp; (cbManager?.isScanning)! {
          cbManager?.stopScan()
      }
  }
  
  func connect(peripheral: CBPeripheral) {
      if cbManager?.state == CBManagerState.poweredOn {
          if currentPeripheral == nil || currentPeripheral?.state != CBPeripheralState.connected {
              cbManager?.connect(peripheral, options: nil)
          } else {
              cbManager?.cancelPeripheralConnection(peripheral)
          }
      }
  }
  
  func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
  Device.savePeripheralString(peripheral: peripheral.identifier.uuidString)
      AutoConnect.stop()
  DeviceUpdate.updateProgress = .None
  Device.isDongleConnected = true
  currentPeripheral = peripheral
  currentPeripheral?.delegate = self
  currentPeripheral?.discoverServices(nil)
  disableSleep()
  }
  
  func disableSleep() {
      UIApplication.shared.isIdleTimerDisabled = true
  }
  
  func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
   if error != nil {
          return
      }
  
      if let services = peripheral.services {
  
          for service in services {
              if service.uuid.uuidString == BleConstants.deviceTwoServiceUUID {
          Device.dongleType = port.second
                      secondService = service
                  peripheral.discoverCharacteristics(nil, for: service)
          }
              if service.uuid.uuidString == BleConstants.deviceOneServiceUUID {
          Device.dongleType = port.first
                  firstService = service
                  peripheral.discoverCharacteristics(nil, for: service)
              } else {
                  Log.bt(&quot;didDiscoverServices for peripheral not found \(peripheral.identifier.uuidString)&quot;)
              }
          }
      }
  }
  
  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
  
      for characteristic in service.characteristics! {
          if characteristic.uuid.uuidString == BleConstants.deviceOneFirstCharacteristicUUID {
       firstCharacteristic = characteristic
          }  
      else if characteristic.uuid.uuidString == BleConstants.deviceOneSecondCharacteristicUUID {
             firstSecondCharacteristic = characteristic
          else if characteristic.uuid.uuidString == BleConstants.deviceTwoFirstCharacteristicUUID {
              secondFirstCharacteristic = characteristic
          } else if characteristic.uuid.uuidString == BleConstants.deviceTwoSecondCharacteristicUUID {
      secondSecondCharacteristic = characteristic
          } else {
              Log.bt(&quot;didDiscoverCharacteristics not found \(characteristic.uuid.uuidString)&quot;)
          }
      }
      
      if Device.dongleType == .deviceTwo {
           openPortDeviceTwo()
      } else {
          openPortDeviceOne()
      }
  }
  </code></pre>
  <p>}</p>
  <p>Data from logs:</p>
  <p>For first device:</p>
  <ol>
  <li>isBluetoothAvailable()</li>
  <li>scan()</li>
  <li>CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) (peripheral &lt;CBPeripheral: 0x267hg5670, identifier = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56, name = DeviceOne, state = disconnected)</li>
  <li>CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)</li>
  <li>scan()</li>
  <li>CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) (peripheral &lt;CBPeripheral: 0x267hg5670, identifier = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901, name = SecondDevice, state = disconnected&gt;)</li>
  <li>stopScan()</li>
  <li>connect(peripheral: CBPeripheral) (currentPeripheral nil, currentPeripheral nil)</li>
  <li>CentralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral</li>
  <li>centralManager(_:didConnect:) - didConnectPeripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 Second device</li>
  <li>savedPeripheralString FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 Second device</li>
  <li>Bluetooth stop()  currentPeripheral Optional(&lt;CBPeripheral: 0x2jk875fe0, identifier = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901, name = SecondDevice, state = connected&gt;)</li>
  <li>disableSleep()</li>
  <li>peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral.services Optional([&lt;CBService: 0x2jkki2dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5&gt;, &lt;CBService: 0xhj67c240, isPrimary = YES, UUID = Device Information&gt;])
  [&lt;CBService: 0x5678f2dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5&gt;, &lt;CBService: 0x28186c240, isPrimary = YES, UUID = Device Information&gt;], peripheral.services is Optional([&lt;CBService: 0xhjy62dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5&gt;, &lt;CBService: 0x2hju7240, isPrimary = YES, UUID = Device Information&gt;]))
  service.uuid.uuidString  is 59DE3994-6A63-4654-8FF0-F85C5163B2F5 and services is [&lt;CBService: 0xhj7892dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5&gt;, &lt;CBService: 0xhjk678340, isPrimary = YES, UUID = Device Information&gt;]</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices SecondDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral not found FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901</li>
  </ol>
  <p>For second device:</p>
  <ol>
  <li>isBluetoothAvailable()</li>
  <li>scan()</li>
  <li>CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) (peripheral &lt;CBPeripheral: 0xh6789a40, identifier = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56, name = DeviceOne, state = disconnected)</li>
  <li>scan()</li>
  <li>CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) (peripheral &lt;CBPeripheral: 0x678jhs0, identifier = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400, name = ThirdDevice, state = disconnected)</li>
  <li>CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)</li>
  <li>stopScan()</li>
  <li>connect(peripheral: CBPeripheral) currentPeripheral nil, currentPeripheral nil</li>
  <li>CentralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral</li>
  <li>centralManager(_:didConnect:) - didConnectPeripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400</li>
  <li>savedPeripheralString H56KIL35-7835-7JKL-2B11-HJKLIYTAA400</li>
  <li>Bluetooth  stop() - currentPeripheral Optional(&lt;CBPeripheral: 0x78jkl680, identifier = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400, name = ThirdDevice, state = connected&gt;)</li>
  <li>disableSleep()</li>
  <li>peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400</li>
  <li>**peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral.services Optional([&lt;CBService: 0xlki8901c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice&gt;, &lt;CBService: 0x281111180, isPrimary = YES, UUID = Device Information&gt;])
  services is [&lt;CBService: 0x2827444c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice&gt;, &lt;CBService: 0x282744580, isPrimary = YES, UUID = Device Information&gt;], peripheral.services is Optional([&lt;CBService: 0xlki8901c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice&gt;, &lt;CBService: 0x281111180, isPrimary = YES, UUID = Device Information&gt;]))
  service.uuid.uuidString  is 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDeviceand services is [&lt;CBService: 0x27893kdc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice&gt;, &lt;CBService: 0x679kh8580, isPrimary = YES, UUID = Device Information&gt;]</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices ThirdDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice</li>
  <li>peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral not found H56KIL35-7835-7JKL-2B11-HJKLIYTAA400**</li>
  </ol>
  <p>Based on logs information for second device in 16th step app connected to my first service UUID and first characteristic UUID, that is wrong!</p>
  <p>Do you have idea,  Did I create UUIDs correct?</p>
  <p>P.S:
  Android app works fine with both devices.</p>
  <p>Thanks a lot!</p>