RileyLinkHeartbeatBluetoothDevice.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // RileyLinkHeartbeatBluetoothTransmitter.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2025-01-08.
  6. // Copyright © 2025 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import CoreBluetooth
  10. class RileyLinkHeartbeatBluetoothDevice: BluetoothDevice {
  11. private let CBUUID_Service_RileyLink: String = "0235733B-99C5-4197-B856-69219C2A3845"
  12. private let CBUUID_ReceiveCharacteristic_TimerTick: String = "6E6C7910-B89E-43A5-78AF-50C5E2B86F7E"
  13. private let CBUUID_ReceiveCharacteristic_Data: String = "C842E849-5028-42E2-867C-016ADADA9155"
  14. init(bluetoothDeviceDelegate: BluetoothDeviceDelegate) {
  15. guard let selectedDevice = Storage.shared.selectedBLEDevice.value else {
  16. fatalError("No selected BLE device found in storage.")
  17. }
  18. let address = selectedDevice.id.uuidString
  19. let deviceName = selectedDevice.name
  20. super.init(
  21. address: address,
  22. name: deviceName,
  23. CBUUID_Advertisement: nil,
  24. servicesCBUUIDs: [CBUUID(string: CBUUID_Service_RileyLink)],
  25. CBUUID_ReceiveCharacteristic: CBUUID_ReceiveCharacteristic_TimerTick,
  26. bluetoothDeviceDelegate: bluetoothDeviceDelegate
  27. )
  28. }
  29. override func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
  30. super.centralManager(central, didConnect: peripheral)
  31. self.bluetoothDeviceDelegate?.heartBeat()
  32. }
  33. override func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
  34. super.peripheral(peripheral, didUpdateValueFor: characteristic, error: error)
  35. guard characteristic.uuid == CBUUID(string: CBUUID_ReceiveCharacteristic_TimerTick) else {
  36. return
  37. }
  38. self.bluetoothDeviceDelegate?.heartBeat()
  39. }
  40. override func expectedHeartbeatInterval() -> TimeInterval? {
  41. return 60
  42. }
  43. }