|
|
|
@ -41,11 +41,12 @@ public class BleService: NSObject { |
|
|
|
private var centralManager: CBCentralManager? |
|
|
|
private var peripheralDevice: CBPeripheral? |
|
|
|
private var writeCharacteristic: CBCharacteristic? |
|
|
|
private var callAudioCharacteristic: CBCharacteristic? |
|
|
|
private var callWriteCharacteristic: CBCharacteristic? |
|
|
|
|
|
|
|
private var notifyCharacteristic: CBCharacteristic? |
|
|
|
private var audioCharacteristic: CBCharacteristic? |
|
|
|
|
|
|
|
private var callAudioCharacteristic: CBCharacteristic? |
|
|
|
|
|
|
|
// 添加录制文件管理器 |
|
|
|
private var recordingFile: RecordingFile? |
|
|
|
|
|
|
|
@ -418,10 +419,11 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
|
|
|
|
// 清空特征引用,强制重新发现 |
|
|
|
writeCharacteristic = nil |
|
|
|
callAudioCharacteristic = nil |
|
|
|
callWriteCharacteristic = nil |
|
|
|
notifyCharacteristic = nil |
|
|
|
audioCharacteristic = nil |
|
|
|
|
|
|
|
callAudioCharacteristic = nil |
|
|
|
|
|
|
|
os_log("🔍 恢复会话时重新发现服务和特征", log: logger, type: .info) |
|
|
|
peripheral.discoverServices([BleConst.PRIMARY_SERVICE_UUID, BleConst.AUDIO_SERVICE_UUID, BleConst.CALL_AUDIO_SERVICE_UUID]) |
|
|
|
|
|
|
|
@ -1151,11 +1153,12 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
|
|
|
|
case BleConst.CMD_WAKE_UP: |
|
|
|
os_log("📢 收到唤醒指令", log: logger, type: .info) |
|
|
|
os_log("🔍 当前状态检查: 连接状态=%d, writeCharacteristic=%@, notifyCharacteristic=%@, audioCharacteristic=%@", |
|
|
|
os_log("🔍 当前状态检查: 连接状态=%d, writeCharacteristic=%@, notifyCharacteristic=%@, callAudioCharacteristic=%@, audioCharacteristic=%@", |
|
|
|
log: logger, type: .debug, |
|
|
|
connectionState, |
|
|
|
writeCharacteristic != nil ? "✅" : "❌", |
|
|
|
notifyCharacteristic != nil ? "✅" : "❌", |
|
|
|
callAudioCharacteristic != nil ? "✅" : "❌", |
|
|
|
audioCharacteristic != nil ? "✅" : "❌") |
|
|
|
|
|
|
|
|
|
|
|
@ -1250,7 +1253,7 @@ extension BleService: SwiftOpusAudioProcessor.AudioDataCallback { |
|
|
|
os_log("🎵 接收到编码音频数据: %d bytes", log: logger, type: .info, data.count) |
|
|
|
|
|
|
|
// 修复条件绑定语句 |
|
|
|
guard let characteristic = callAudioCharacteristic, |
|
|
|
guard let characteristic = callWriteCharacteristic, |
|
|
|
let peripheral = peripheralDevice, |
|
|
|
connectionState == BleConst.STATE_CONNECTED else { |
|
|
|
os_log("通话音频特征未设置或设备未连接", log: logger, type: .error) |
|
|
|
@ -1358,7 +1361,7 @@ extension BleService: SwiftOpusAudioProcessor.AudioDataCallback { |
|
|
|
* @param data 要发送的音频数据 |
|
|
|
*/ |
|
|
|
private func sendAudioChunk(_ data: Data) { |
|
|
|
guard let characteristic = callAudioCharacteristic, |
|
|
|
guard let characteristic = callWriteCharacteristic, |
|
|
|
let peripheral = peripheralDevice, |
|
|
|
connectionState == BleConst.STATE_CONNECTED else { |
|
|
|
os_log("通话音频特征未设置或设备未连接,无法发送音频数据", log: logger, type: .error) |
|
|
|
@ -1764,7 +1767,7 @@ extension BleService: CBPeripheralDelegate { |
|
|
|
}else if service.uuid == BleConst.CALL_AUDIO_SERVICE_UUID { |
|
|
|
os_log("🔍 发现通话音频服务,开始发现通话音频特征...", log: logger, type: .debug) |
|
|
|
// 通话音频服务,发现通话音频特征 |
|
|
|
peripheral.discoverCharacteristics([BleConst.CALL_WRITE_AUDIO_CHAR_UUID], for: service) |
|
|
|
peripheral.discoverCharacteristics([BleConst.CALL_WRITE_AUDIO_CHAR_UUID, BleConst.CALL_RECEIVE_AUDIO_CHAR_UUID], for: service) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -1796,15 +1799,21 @@ extension BleService: CBPeripheralDelegate { |
|
|
|
os_log("🎵 音频特征已设置,开始发现描述符...", log: logger, type: .info) |
|
|
|
// 先发现描述符,再配置通知 |
|
|
|
peripheral.discoverDescriptors(for: characteristic) |
|
|
|
}else if characteristic.uuid == BleConst.CALL_WRITE_AUDIO_CHAR_UUID { |
|
|
|
}else if characteristic.uuid == BleConst.CALL_RECEIVE_AUDIO_CHAR_UUID { |
|
|
|
callAudioCharacteristic = characteristic |
|
|
|
os_log("🎵 通话音频特征已设置,开始发现描述符...", log: logger, type: .info) |
|
|
|
// 先发现描述符,再配置通知 |
|
|
|
peripheral.discoverDescriptors(for: characteristic) |
|
|
|
} |
|
|
|
else if characteristic.uuid == BleConst.CALL_WRITE_AUDIO_CHAR_UUID { |
|
|
|
callWriteCharacteristic = characteristic |
|
|
|
os_log("🎵 通话音频特征已设置,开始发现描述符...", log: logger, type: .info) |
|
|
|
// 先发现描述符,再配置通知 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 检查所有必要特征是否已准备就绪 |
|
|
|
let isReady = writeCharacteristic != nil && notifyCharacteristic != nil && audioCharacteristic != nil |
|
|
|
let isReady = writeCharacteristic != nil && notifyCharacteristic != nil && audioCharacteristic != nil && callAudioCharacteristic != nil |
|
|
|
if isReady { |
|
|
|
os_log("🚀 所有BLE特征已准备就绪,可以发送命令", log: logger, type: .info) |
|
|
|
} else { |
|
|
|
@ -1812,6 +1821,7 @@ extension BleService: CBPeripheralDelegate { |
|
|
|
log: logger, type: .debug, |
|
|
|
writeCharacteristic != nil ? "✅" : "❌", |
|
|
|
notifyCharacteristic != nil ? "✅" : "❌", |
|
|
|
callAudioCharacteristic != nil ? "✅" : "❌", |
|
|
|
audioCharacteristic != nil ? "✅" : "❌") |
|
|
|
} |
|
|
|
} |
|
|
|
@ -1829,6 +1839,9 @@ extension BleService: CBPeripheralDelegate { |
|
|
|
} else if characteristic.uuid == BleConst.RECEIVE_AUDIO_CHAR_UUID { |
|
|
|
// 处理音频数据 |
|
|
|
processAudioData(data) |
|
|
|
}else if characteristic.uuid == BleConst.CALL_RECEIVE_AUDIO_CHAR_UUID { |
|
|
|
// 处理音频数据 |
|
|
|
processAudioData(data) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1850,7 +1863,7 @@ extension BleService: CBPeripheralDelegate { |
|
|
|
characteristic.uuid.uuidString, characteristic.descriptors?.count ?? 0) |
|
|
|
|
|
|
|
// 对通知特征和音频特征配置通知 |
|
|
|
if characteristic.uuid == BleConst.NOTIFY_CHAR_UUID || characteristic.uuid == BleConst.RECEIVE_AUDIO_CHAR_UUID { |
|
|
|
if characteristic.uuid == BleConst.NOTIFY_CHAR_UUID || characteristic.uuid == BleConst.RECEIVE_AUDIO_CHAR_UUID || characteristic.uuid == BleConst.CALL_RECEIVE_AUDIO_CHAR_UUID { |
|
|
|
setupNotification(for: peripheral, characteristic: characteristic) |
|
|
|
} |
|
|
|
} |
|
|
|
|