QualifiedCharacteristic.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import class CoreBluetooth.CBUUID
  2. import class CoreBluetooth.CBCharacteristic
  3. struct QualifiedCharacteristic: Equatable {
  4. let id: CharacteristicID
  5. let serviceID: ServiceID
  6. let peripheralID: PeripheralID
  7. }
  8. extension QualifiedCharacteristic {
  9. init(_ characteristic: CBCharacteristic) {
  10. self.init(
  11. id: characteristic.uuid,
  12. serviceID: characteristic.service?.uuid ?? ServiceID(),
  13. peripheralID: characteristic.service?.peripheral?.identifier ?? PeripheralID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
  14. )
  15. }
  16. }
  17. struct QualifiedCharacteristicIDFactory {
  18. func make(from message: CharacteristicAddress) -> QualifiedCharacteristic? {
  19. guard
  20. message.hasCharacteristicUuid,
  21. message.hasServiceUuid,
  22. let peripheralID = UUID(uuidString: message.deviceID)
  23. else { return nil }
  24. let characteristicID = CBUUID(data: message.characteristicUuid.data)
  25. let serviceID = CBUUID(data: message.serviceUuid.data)
  26. return QualifiedCharacteristic(
  27. id: characteristicID,
  28. serviceID: serviceID,
  29. peripheralID: peripheralID
  30. )
  31. }
  32. }