Browse Source

ios加入回复失败

weicu
fdp 11 months ago
parent
commit
dbd298a872
  1. 69
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

69
local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

@ -891,8 +891,13 @@ private var cmdReplyType: UInt8 = 0
} }
/// 处理接收到的响应数据 /// 处理接收到的响应数据
///
/// - 功能:
/// - 解析设备响应帧,校验头、长度与 CRC
/// - 与 Android 保持一致的失败判定:当长度字段与实际长度不匹配时,上报 rawData = [length, data[data.count-2]]
/// - 在 CMD_CONTROL_CODEC 分支对齐安卓对错误响应的处理逻辑
private func handleResponseData(_ data: Data) { private func handleResponseData(_ data: Data) {
guard let characteristic = writeCharacteristic, guard let characteristic = writeCharacteristic,
connectionState == BleConst.STATE_CONNECTED else { connectionState == BleConst.STATE_CONNECTED else {
os_log(" 设备未连接", log: logger, type: .error) os_log(" 设备未连接", log: logger, type: .error)
return return
@ -920,9 +925,33 @@ private var cmdReplyType: UInt8 = 0
var hexString = data.map { String(format: "0x%02X", $0) }.joined(separator: ", ") var hexString = data.map { String(format: "0x%02X", $0) }.joined(separator: ", ")
os_log("收到设备响应: %@", log: logger, type: .debug, hexString) os_log("收到设备响应: %@", log: logger, type: .debug, hexString)
// 检查数据长度 // 检查数据长度(与 Android 统一失败判定保持一致)
if data.count != Int(length) + 4 { // 帧头 + 命令 + 长度 + 数据 + CRC if data.count != Int(length) + 4 { // 帧头 + 命令 + 长度 + 数据 + CRC
os_log("设备响应数据长度不匹配: 期望=%d, 实际=%d", log: logger, type: .error, Int(length) + 4, data.count) os_log("设备响应数据长度不匹配: 期望=%d, 实际=%d", log: logger, type: .error, Int(length) + 4, data.count)
// 与 Android 保持一致:构造 rawData 为 [length, 倒数第二位(payload 最后一位)]
var raw: [UInt8] = []
raw.append(length)
if data.count >= 2 {
raw.append(data[data.count - 2])
}
let resultMap: [String: Any] = [
"success": true, // 与 Android 对齐(Dart 层按 rawData 判断)
"command": Int(cmd),
"rawData": Data(raw)
]
// 推进命令队列(与后续正常路径一致)
isReply = true
cmdReplyType = cmd
if !commandQueue.isEmpty {
let nextCmd = commandQueue.removeFirst()
peripheralDevice?.writeValue(nextCmd, for: characteristic, type: .withoutResponse)
}
// 上报到 Dart 层
notifyDeviceInfoReceived(infoType: Int(cmd), infoData: resultMap)
return return
} }
@ -938,12 +967,12 @@ private var cmdReplyType: UInt8 = 0
// 提取响应数据 // 提取响应数据
let responseData = length > 0 ? data.subdata(in: 3..<(3 + Int(length))) : Data() let responseData = length > 0 ? data.subdata(in: 3..<(3 + Int(length))) : Data()
isReply = true; isReply = true
cmdReplyType = cmd; cmdReplyType = cmd
if !commandQueue.isEmpty { if !commandQueue.isEmpty {
let nextCmd = commandQueue.removeFirst() let nextCmd = commandQueue.removeFirst()
peripheralDevice?.writeValue(nextCmd, for: characteristic, type: .withoutResponse) peripheralDevice?.writeValue(nextCmd, for: characteristic, type: .withoutResponse)
} }
// 根据命令类型处理设备响应 // 根据命令类型处理设备响应
switch cmd { switch cmd {
@ -955,10 +984,9 @@ private var cmdReplyType: UInt8 = 0
] ]
if !responseData.isEmpty { if !responseData.isEmpty {
// 如果无法解析为字符串,以点分隔的数字表示版本
let versionNums = responseData let versionNums = responseData
.map { $0 } .map { $0 }
.filter { $0 != 0xFF } // 过滤掉无效字节 .filter { $0 != 0xFF }
.map { String($0) } .map { String($0) }
.joined(separator: ".") .joined(separator: ".")
resultMap["version"] = versionNums resultMap["version"] = versionNums
@ -1005,7 +1033,6 @@ private var cmdReplyType: UInt8 = 0
] ]
if responseData.count >= 3 { if responseData.count >= 3 {
// 处理电池信息 - 左耳、右耳、充电仓
let leftEarBattery = Int(responseData[0] & 0x7F) let leftEarBattery = Int(responseData[0] & 0x7F)
let leftEarCharging = (responseData[0] & 0x80) != 0 let leftEarCharging = (responseData[0] & 0x80) != 0
@ -1015,14 +1042,13 @@ private var cmdReplyType: UInt8 = 0
let caseBattery = Int(responseData[2] & 0x7F) let caseBattery = Int(responseData[2] & 0x7F)
let caseCharging = (responseData[2] & 0x80) != 0 let caseCharging = (responseData[2] & 0x80) != 0
// 添加到结果Map
resultMap["leftEarBattery"] = leftEarBattery resultMap["leftEarBattery"] = leftEarBattery
resultMap["leftEarCharging"] = leftEarCharging resultMap["leftEarCharging"] = leftEarCharging
resultMap["rightEarBattery"] = rightEarBattery resultMap["rightEarBattery"] = rightEarBattery
resultMap["rightEarCharging"] = rightEarCharging resultMap["rightEarCharging"] = rightEarCharging
resultMap["caseBattery"] = caseBattery resultMap["caseBattery"] = caseBattery
resultMap["caseCharging"] = caseCharging resultMap["caseCharging"] = caseCharging
resultMap["batteryLevel"] = leftEarBattery // 整体电量用左耳表示 resultMap["batteryLevel"] = leftEarBattery
os_log("接收到电量信息: 左耳=%d%% (%@), 右耳=%d%% (%@), 充电仓=%d%% (%@)", os_log("接收到电量信息: 左耳=%d%% (%@), 右耳=%d%% (%@), 充电仓=%d%% (%@)",
log: logger, type: .info, log: logger, type: .info,
@ -1030,7 +1056,6 @@ private var cmdReplyType: UInt8 = 0
rightEarBattery, rightEarCharging ? "充电中" : "未充电", rightEarBattery, rightEarCharging ? "充电中" : "未充电",
caseBattery, caseCharging ? "充电中" : "未充电") caseBattery, caseCharging ? "充电中" : "未充电")
} else if !responseData.isEmpty { } else if !responseData.isEmpty {
// 兼容单字节电量
let batteryLevel = Int(responseData[0]) let batteryLevel = Int(responseData[0])
resultMap["batteryLevel"] = batteryLevel resultMap["batteryLevel"] = batteryLevel
os_log("接收到电量信息(简化): %d%%", log: logger, type: .info, batteryLevel) os_log("接收到电量信息(简化): %d%%", log: logger, type: .info, batteryLevel)
@ -1045,11 +1070,10 @@ private var cmdReplyType: UInt8 = 0
"rawData": responseData "rawData": responseData
] ]
if responseData.count >= 2 { if !responseData.isEmpty && responseData.count >= 2 {
let codecStatus = Int(responseData[0]) let codecStatus = Int(responseData[0])
let channelMode = Int(responseData[1]) let channelMode = Int(responseData[1])
// 解码状态描述
let statusDesc: String let statusDesc: String
switch codecStatus { switch codecStatus {
case Int(BleConst.CODEC_CONTROL_CLOSE): case Int(BleConst.CODEC_CONTROL_CLOSE):
@ -1066,7 +1090,6 @@ private var cmdReplyType: UInt8 = 0
statusDesc = "未知状态(\(codecStatus))" statusDesc = "未知状态(\(codecStatus))"
} }
// 声道模式描述
let channelDesc: String let channelDesc: String
switch channelMode { switch channelMode {
case Int(BleConst.AUDIO_CHANNEL_LEFT): case Int(BleConst.AUDIO_CHANNEL_LEFT):
@ -1085,12 +1108,22 @@ private var cmdReplyType: UInt8 = 0
resultMap["channelDesc"] = channelDesc resultMap["channelDesc"] = channelDesc
os_log("编解码控制响应: %@, %@", log: logger, type: .info, statusDesc, channelDesc) os_log("编解码控制响应: %@, %@", log: logger, type: .info, statusDesc, channelDesc)
} else if !responseData.isEmpty && responseData.count == 1 {
// 与 Android 保持一致:单字节且为 0 判定为错误
let codecStatus = Int(responseData[0])
if codecStatus == 0x00 {
os_log("收到设备响应: 失败, codecStatus: %d", log: logger, type: .debug, codecStatus)
resultMap = [
"success": true, // 与 Android 对齐(Dart 层按 rawData 判断)
"command": "Error",
"rawData": responseData
]
}
} }
notifyDeviceInfoReceived(infoType: Int(cmd), infoData: resultMap) notifyDeviceInfoReceived(infoType: Int(cmd), infoData: resultMap)
default: default:
// 对于未知命令,返回原始数据
let resultMap: [String: Any] = [ let resultMap: [String: Any] = [
"success": true, "success": true,
"command": Int(cmd), "command": Int(cmd),

Loading…
Cancel
Save