CBPeripheral.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // CBPeripheral.swift
  3. // xDripG5
  4. //
  5. // Copyright © 2017 LoopKit Authors. All rights reserved.
  6. //
  7. import CoreBluetooth
  8. // MARK: - Discovery helpers.
  9. extension CBPeripheral {
  10. func servicesToDiscover(from serviceUUIDs: [CBUUID]) -> [CBUUID] {
  11. let knownServiceUUIDs = services?.compactMap({ $0.uuid }) ?? []
  12. return serviceUUIDs.filter({ !knownServiceUUIDs.contains($0) })
  13. }
  14. func characteristicsToDiscover(from characteristicUUIDs: [CBUUID], for service: CBService) -> [CBUUID] {
  15. let knownCharacteristicUUIDs = service.characteristics?.compactMap({ $0.uuid }) ?? []
  16. return characteristicUUIDs.filter({ !knownCharacteristicUUIDs.contains($0) })
  17. }
  18. }
  19. extension Collection where Element: CBAttribute {
  20. func itemWithUUID(_ uuid: CBUUID) -> Element? {
  21. for attribute in self {
  22. if attribute.uuid == uuid {
  23. return attribute
  24. }
  25. }
  26. return nil
  27. }
  28. func itemWithUUIDString(_ uuidString: String) -> Element? {
  29. for attribute in self {
  30. if attribute.uuid.uuidString == uuidString {
  31. return attribute
  32. }
  33. }
  34. return nil
  35. }
  36. }