// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import Foundation #if os(iOS) import CoreBluetooth #endif /// Bluetooth LE MIDI controllers (the Android app's BleMidi). Discovery and /// connecting go through the system's Bluetooth MIDI panel (CABTMIDICentralViewController, /// Setup › MIDI Sync › Find controllers); once connected a controller is an /// ordinary CoreMIDI source, listed and played like the others. /// /// When the panel closes, the connected BLE MIDI peripherals are remembered /// (identifier and name, in AppSettings); at the next launch Sizzle connects them /// again through CoreBluetooth, so they come back without opening the panel. /// Bluetooth is only touched once something is remembered or the panel is used, /// so the permission prompt doesn't appear for people without BLE controllers. @MainActor final class BluetoothMidi: ObservableObject { static let shared = BluetoothMidi() /// Peripheral identifier -> name. @Published private(set) var remembered: [String: String] private init() { remembered = AppSettings.shared.bleMidiDevices } func forget(_ id: String) { remembered[id] = nil AppSettings.shared.bleMidiDevices = remembered #if os(iOS) if let uuid = UUID(uuidString: id), let p = central?.retrievePeripherals(withIdentifiers: [uuid]).first { central?.cancelPeripheralConnection(p) } #endif } #if os(iOS) static let midiService = CBUUID(string: "03B80E5A-EDE8-4B33-A751-6CE34EC4C700") private var central: CBCentralManager? private var pending: [() -> Void] = [] private lazy var delegate = CentralDelegate { [weak self] in self?.poweredOn() } /// Run [action] once Bluetooth is on (creating the central manager on first use). private func whenPoweredOn(_ action: @escaping () -> Void) { if let c = central, c.state == .poweredOn { action(); return } pending.append(action) if central == nil { central = CBCentralManager(delegate: delegate, queue: .main) } } /// At launch: connect every remembered controller. func reconnectRemembered() { guard !remembered.isEmpty else { return } whenPoweredOn { [self] in let ids = remembered.keys.compactMap(UUID.init(uuidString:)) for p in central?.retrievePeripherals(withIdentifiers: ids) ?? [] where p.state == .disconnected { central?.connect(p, options: nil) } } } /// The Bluetooth MIDI panel closed: remember what is connected now. func panelClosed() { whenPoweredOn { [self] in for p in central?.retrieveConnectedPeripherals(withServices: [Self.midiService]) ?? [] { remembered[p.identifier.uuidString] = p.name ?? "Bluetooth MIDI" } AppSettings.shared.bleMidiDevices = remembered } } private func poweredOn() { let actions = pending pending.removeAll() actions.forEach { $0() } } #else func reconnectRemembered() {} func panelClosed() {} #endif } #if os(iOS) /// The central manager's delegate, kept out of the Objective-C header (which /// doesn't import CoreBluetooth). private final class CentralDelegate: NSObject, CBCentralManagerDelegate { private let onPoweredOn: @MainActor () -> Void init(_ onPoweredOn: @escaping @MainActor () -> Void) { self.onPoweredOn = onPoweredOn } func centralManagerDidUpdateState(_ central: CBCentralManager) { guard central.state == .poweredOn else { return } Task { @MainActor in self.onPoweredOn() } } } #endif