From 22694b44ad3f523a43783c4fd883bc3f5ee03165 Mon Sep 17 00:00:00 2001 From: daifengyi Date: Tue, 27 Jun 2023 15:01:24 +0800 Subject: [PATCH] feat:default response data parse --- HDFwear/Tools/BluetoothManager.swift | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/HDFwear/Tools/BluetoothManager.swift b/HDFwear/Tools/BluetoothManager.swift index 5aef3ba..b21713a 100644 --- a/HDFwear/Tools/BluetoothManager.swift +++ b/HDFwear/Tools/BluetoothManager.swift @@ -1226,6 +1226,32 @@ class BluetoothManager: NSObject { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) } + //MARK: - 新解析数据 + private func newParseDefaultResponseData(_ bytes: [UInt8]) { + guard bytes.count == 17, bytes[9] == 5 else { + return + } + let newcmd = NewCmd(rawValue: bytes[13]) + switch newcmd { + case .setTime: + if setCmdClosure != nil { + let success = bytes[14] == 0 + if success { + setCmdClosure?(nil) + }else { + setCmdClosure?((Int)(bytes[14])) + } + setCmdClosure = nil + } + case .none: + break + } + + } + + + //MARK: - 发送数据 + private func sendData(_ data: Data) { if peripheral == nil { print("peripheral == nil") @@ -1552,7 +1578,7 @@ extension BluetoothManager: ClientProfileDelegate { } } case BLEConfig.ReadUUID: -// print("?????????????\([UInt8](data))") + print("?????????????\([UInt8](data))") mergeData(data) default: mergeData(data) @@ -1563,19 +1589,20 @@ extension BluetoothManager: ClientProfileDelegate { let bytes = [UInt8](data) print(bytes) isSyncReceived = true - if bytes[0] == 0xBA && (bytes[1] == 48 || bytes[1] == 112) { - if bytes.count > 10, BleCmd(rawValue: bytes[8]) == .sync { - if isSync { - if SyncCmd(rawValue: bytes[10]) != isSyncCmd { - syncReceiveFail() - } else { - isNextSync = bytes[1] == 112 - startSyncTimer() - isSyncReceived = true - } - } - } - totalLength = Int(UInt16(bytes[6]) << 8 | UInt16(bytes[7])) + 8 + if bytes[0] == 0xED && bytes[1] == 0x7E { +// if bytes.count > 10, BleCmd(rawValue: bytes[8]) == .sync { +// if isSync { +// if SyncCmd(rawValue: bytes[10]) != isSyncCmd { +// syncReceiveFail() +// } else { +// isNextSync = bytes[1] == 112 +// startSyncTimer() +// isSyncReceived = true +// } +// } +// } + // 内容长度加上其他位的长度 + totalLength = Int(UInt16(bytes[8]) << 8 | UInt16(bytes[9])) + 12 if data.count == totalLength { parseData(bytes) } else { @@ -1604,21 +1631,28 @@ extension BluetoothManager: ClientProfileDelegate { totalLength = 0 mergeLength = 0 totalBytes = [] - guard bytes.count > 10, let cmd = BleCmd(rawValue: bytes[8]) else { return } + guard bytes.count > 12 else { return } + let msgId = Int(UInt16(bytes[4]) << 8 | UInt16(bytes[5])) let data = Data(bytes: bytes, count: bytes.count) - switch cmd { - case .set, .find, .device, .remind, .firmware: - parseSetData(SetCmd(rawValue: bytes[10]), data: data) - case .sync: - let valueBytes = Array(bytes[13..