CBPeripheral.swift 963 B

123456789101112131415161718192021222324252627282930313233343536
  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. }