diff --git a/HDFwear/Tools/Bluetooth+Types.swift b/HDFwear/Tools/Bluetooth+Types.swift index 3221ccd..270f4b6 100644 --- a/HDFwear/Tools/Bluetooth+Types.swift +++ b/HDFwear/Tools/Bluetooth+Types.swift @@ -182,3 +182,16 @@ enum SyncDay: Int { // case openCamera = 0x46 // case exitCamera = 0x48 //} + +// 0x01 设备ID +// 0x02 设备名称 +// 0x03 设备mac地址 +// 0x04 MCU版本号 +// 0x05 电量 +enum deviceInfoType: UInt8 { + case deviceId = 0x01 + case deviceName = 0x02 + case deviceMac = 0x03 + case mcuVersion = 0x04 + case battery = 0x05 +} diff --git a/HDFwear/Tools/BluetoothManager+Function.swift b/HDFwear/Tools/BluetoothManager+Function.swift index 83d871f..54bea4d 100644 --- a/HDFwear/Tools/BluetoothManager+Function.swift +++ b/HDFwear/Tools/BluetoothManager+Function.swift @@ -311,6 +311,8 @@ extension BluetoothManager { parseDefaultResponseData(bytes) case 0x8002:// 设备信息查询应答 print("设备信息查询应答") + let content = parseContentFromBytes(bytes) + parseDeviceInfoData(content) case 0x8009:// 实时步数、卡路里、距离自动上报 print("实时步数、卡路里、距离自动上报") case 0x8010:// 电量变化自动上报 @@ -368,6 +370,15 @@ extension BluetoothManager { // } } + private func parseContentFromBytes(_ bytes: [UInt8]) -> [UInt8] { + let contentLength = Int(bytes[10]) // 获取内容长度 + guard bytes.count >= 11 + contentLength else { + return [] // 如果字节数组长度不足以包含内容,返回空数组 + } + let content = Array(bytes[11..<(11 + contentLength)]) // 获取内容部分 + return content + } + private func parseDefaultResponseData(_ bytes: [UInt8]) { guard bytes.count == 17, bytes[9] == 5 else { return @@ -390,4 +401,41 @@ extension BluetoothManager { break } } + + private func parseDeviceInfoData (_ content:[UInt8]) { + guard content.count > 0 else { + print("无有效的信息") + return + } + let totalInfoCount = Int(content[0]) + var currentIndex = 1 + + // 检查总信息数是否大于0 + guard totalInfoCount > 0 else { + print("无有效的信息") + return + } + for _ in 0..