RiValT_BluetoothRemote

final class RiValT_BluetoothRemote : NSObject, ObservableObject
extension RiValT_BluetoothRemote: CBCentralManagerDelegate, CBPeripheralDelegate

A foreground iPad can vend the same request/reply protocol as the paired phone. GATT indications carry replies and change hints; each request is applied only by the host.

  • The transport shared by the timer delegate and the app’s remote settings.

    Declaration

    Swift

    static let shared: RiValT_BluetoothRemote
  • Shared device identifiers and packet framing for the Bluetooth link.

    Declaration

    Swift

    private typealias Packet = RiValT_BluetoothRemotePacket
  • The current assembly for fragments received from the remote device.

    Declaration

    Swift

    private var incoming: RiValT_BluetoothRemote.Packet.Assembly
  • The identity and advertised name of an iPad discovered by the Watch.

    See more

    Declaration

    Swift

    struct Device : Identifiable
  • Whether the Watch uses Bluetooth to control an iPad.

    Declaration

    Swift

    @Published
    private(set) var usesIPad: Bool { get set }
  • Whether the selected iPad has approved the encrypted request channel.

    Declaration

    Swift

    @Published
    private(set) var isConnected: Bool { get set }
  • The iPads found during the current device scan.

    Declaration

    Swift

    @Published
    private(set) var devices: [RiValT_BluetoothRemote.Device] { get set }
  • The localized scan, connection, or pairing status shown on the Watch.

    Declaration

    Swift

    @Published
    private(set) var status: String { get set }
  • The persisted identifier of the selected iPad.

    Declaration

    Swift

    @Published
    private(set) var selectedDeviceID: UUID? { get set }
  • The last advertised label saved for the selected iPad.

    Declaration

    Swift

    @Published
    private(set) var selectedDeviceName: String? { get set }
  • Notifies the timer delegate that the host asked for a fresh snapshot.

    Declaration

    Swift

    var onChange: (() -> Void)?
  • Notifies the timer delegate that link or device selection state changed.

    Declaration

    Swift

    var onConnectionChange: (() -> Void)?
  • The Core Bluetooth central manager used to discover and connect to iPads.

    Declaration

    Swift

    private var manager: CBCentralManager?
  • Recently discovered peripherals keyed by their Bluetooth identifiers.

    Declaration

    Swift

    private var discovered: [UUID : CBPeripheral]
  • The peripheral connecting to or serving the Watch.

    Declaration

    Swift

    private var peer: CBPeripheral?
  • The characteristic used to send requests and receive indications.

    Declaration

    Swift

    private var channel: CBCharacteristic?
  • Whether the device picker is actively requesting a scan.

    Declaration

    Swift

    @Published
    private(set) var isBrowsing: Bool { get set }
  • Whether pairing failed and automatic reconnection must wait for user action.

    Declaration

    Swift

    private var denied: Bool
  • The request fragments awaiting acknowledged writes.

    Declaration

    Swift

    private var outgoing: [Data]
  • The packet identifier used to match a reply to the current request.

    Declaration

    Swift

    private var requestID: UInt32
  • The last Watch name acknowledged by the iPad on this connection.

    Declaration

    Swift

    private var reportedWatchName: String?
  • The completion callback for the current request’s reply.

    Declaration

    Swift

    private var onReply: (([String : Any]) -> Void)?
  • The completion callback for a timed-out or failed request.

    Declaration

    Swift

    private var onFailure: (() -> Void)?
  • The pending deadline for the current request.

    Declaration

    Swift

    private var timeout: DispatchWorkItem?
  • The scheduled reconnect after a dropped peripheral connection.

    Declaration

    Swift

    private var retry: DispatchWorkItem?
  • Initializes the central manager or resumes discovery when browsing or using an iPad.

    Declaration

    Swift

    func activate()
  • Starts a device scan and clears stale results while retaining the currently connected iPad.

    Declaration

    Swift

    func browse()
  • Retries an explicit connection attempt and clears a prior authorization denial.

    Declaration

    Swift

    func retryConnection()
  • Stops device browsing and keeps scanning only when needed to reconnect to the selected iPad.

    Declaration

    Swift

    func endBrowsing()
  • Switches to the paired iPhone and closes the Bluetooth link.

    Declaration

    Swift

    func selectPhone()
  • Persists the chosen iPad, resets the old link, and begins connecting.

    Declaration

    Swift

    func selectDevice(_ device: Device)
  • Connects to the saved peripheral when available and scans when browsing or searching for it.

    Declaration

    Swift

    private func discover()
  • Clears the current peripheral, cancels its connection, and fails any request in flight.

    Declaration

    Swift

    private func clearPeer()
  • Releases a disconnected peer and schedules discovery again if automatic retry is allowed.

    Declaration

    Swift

    private func reconnect(after peripheral: CBPeripheral)
  • Sends a single framed request and calls the matching reply or failure handler.

    Declaration

    Swift

    func request(_ message: [String : Any], reply: @escaping ([String : Any]) -> Void, failure: @escaping () -> Void)
  • Cancels the deadline and clears the assembly, fragments, and handlers for the finished request.

    Declaration

    Swift

    private func finishRequest()
  • Clears the request before notifying its failure handler.

    Declaration

    Swift

    private func failRequest()
  • Resumes discovery when Bluetooth powers on or clears the link and displays the unavailable state.

    Declaration

    Swift

    func centralManagerDidUpdateState(_ central: CBCentralManager)
  • Adds a discovered iPad to the picker and connects when it matches the saved selection.

    Declaration

    Swift

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,
                        advertisementData: [String: Any], rssi RSSI: NSNumber)
  • Discovers the RVT service on the selected iPad after connecting.

    Declaration

    Swift

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
  • Schedules reconnection if the selected peripheral fails to connect.

    Declaration

    Swift

    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?)
  • Schedules reconnection after losing the selected peripheral.

    Declaration

    Swift

    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)
  • Finds the host service and requests discovery of its control characteristic.

    Declaration

    Swift

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
  • Reconnects when the selected iPad invalidates the timer service.

    Declaration

    Swift

    func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService])
  • Subscribes to indications on the selected iPad’s control characteristic.

    Declaration

    Swift

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
  • Handles a failed subscription and requires a manual retry after authentication or pairing errors.

    Declaration

    Swift

    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?)
  • Sends the next request fragment after a write is acknowledged or fails the exchange.

    Declaration

    Swift

    func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?)
  • Handles authorization and change indications or completes the matching request from reply fragments.

    Declaration

    Swift

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)