|
|
|
@ -66,6 +66,9 @@ public class BleService: NSObject { |
|
|
|
// 自动重连队列(统一管理所有待连接的设备) |
|
|
|
private var pendingReconnectUUIDs = Set<UUID>() |
|
|
|
|
|
|
|
// 标记是否为主动断开连接 |
|
|
|
private var isManualDisconnect = false |
|
|
|
|
|
|
|
private let logger = OSLog(subsystem: "com.yunqiinnovation.ble_service", category: "BleService") |
|
|
|
|
|
|
|
// MARK: - Opus音频处理器 |
|
|
|
@ -365,6 +368,7 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
} |
|
|
|
|
|
|
|
if centralManager != nil { |
|
|
|
isManualDisconnect = true // 标记为主动断开 |
|
|
|
updateConnectionState(BleConst.STATE_DISCONNECTING) |
|
|
|
centralManager?.cancelPeripheralConnection(peripheral) |
|
|
|
os_log("🔌 开始断开设备连接", log: logger, type: .info) |
|
|
|
@ -1420,13 +1424,18 @@ extension BleService: CBCentralManagerDelegate { |
|
|
|
|
|
|
|
updateConnectionState(BleConst.STATE_DISCONNECTED) |
|
|
|
|
|
|
|
// 无论是否意外断开,都立即发起新的连接 |
|
|
|
if central.state == .poweredOn { |
|
|
|
os_log("🔄 立即重新连接设备: %{public}@", log: logger, type: .info, peripheral.identifier.uuidString) |
|
|
|
connectPeripheral(peripheral) |
|
|
|
// 只有在非主动断开的情况下才重新连接 |
|
|
|
if !isManualDisconnect { |
|
|
|
if central.state == .poweredOn { |
|
|
|
os_log("🔄 意外断开,立即重新连接设备: %{public}@", log: logger, type: .info, peripheral.identifier.uuidString) |
|
|
|
connectPeripheral(peripheral) |
|
|
|
} else { |
|
|
|
os_log("🔄 蓝牙未就绪,添加到重连队列: %{public}@", log: logger, type: .info, peripheral.identifier.uuidString) |
|
|
|
pendingReconnectUUIDs.insert(peripheral.identifier) |
|
|
|
} |
|
|
|
} else { |
|
|
|
os_log("🔄 蓝牙未就绪,添加到重连队列: %{public}@", log: logger, type: .info, peripheral.identifier.uuidString) |
|
|
|
pendingReconnectUUIDs.insert(peripheral.identifier) |
|
|
|
os_log("✅ 主动断开连接,不进行自动重连", log: logger, type: .info) |
|
|
|
isManualDisconnect = false // 重置标志 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|