Commit 5e73db330972d6ae1a870b615ed155ee83bbb287
1 parent
1605ea7d
feat:parse device info
Showing
2 changed files
with
61 additions
and
0 deletions
HDFwear/Tools/Bluetooth+Types.swift
@@ -182,3 +182,16 @@ enum SyncDay: Int { | @@ -182,3 +182,16 @@ enum SyncDay: Int { | ||
182 | // case openCamera = 0x46 | 182 | // case openCamera = 0x46 |
183 | // case exitCamera = 0x48 | 183 | // case exitCamera = 0x48 |
184 | //} | 184 | //} |
185 | + | ||
186 | +// 0x01 设备ID | ||
187 | +// 0x02 设备名称 | ||
188 | +// 0x03 设备mac地址 | ||
189 | +// 0x04 MCU版本号 | ||
190 | +// 0x05 电量 | ||
191 | +enum deviceInfoType: UInt8 { | ||
192 | + case deviceId = 0x01 | ||
193 | + case deviceName = 0x02 | ||
194 | + case deviceMac = 0x03 | ||
195 | + case mcuVersion = 0x04 | ||
196 | + case battery = 0x05 | ||
197 | +} |
HDFwear/Tools/BluetoothManager+Function.swift
@@ -311,6 +311,8 @@ extension BluetoothManager { | @@ -311,6 +311,8 @@ extension BluetoothManager { | ||
311 | parseDefaultResponseData(bytes) | 311 | parseDefaultResponseData(bytes) |
312 | case 0x8002:// 设备信息查询应答 | 312 | case 0x8002:// 设备信息查询应答 |
313 | print("设备信息查询应答") | 313 | print("设备信息查询应答") |
314 | + let content = parseContentFromBytes(bytes) | ||
315 | + parseDeviceInfoData(content) | ||
314 | case 0x8009:// 实时步数、卡路里、距离自动上报 | 316 | case 0x8009:// 实时步数、卡路里、距离自动上报 |
315 | print("实时步数、卡路里、距离自动上报") | 317 | print("实时步数、卡路里、距离自动上报") |
316 | case 0x8010:// 电量变化自动上报 | 318 | case 0x8010:// 电量变化自动上报 |
@@ -368,6 +370,15 @@ extension BluetoothManager { | @@ -368,6 +370,15 @@ extension BluetoothManager { | ||
368 | // } | 370 | // } |
369 | } | 371 | } |
370 | 372 | ||
373 | + private func parseContentFromBytes(_ bytes: [UInt8]) -> [UInt8] { | ||
374 | + let contentLength = Int(bytes[10]) // 获取内容长度 | ||
375 | + guard bytes.count >= 11 + contentLength else { | ||
376 | + return [] // 如果字节数组长度不足以包含内容,返回空数组 | ||
377 | + } | ||
378 | + let content = Array(bytes[11..<(11 + contentLength)]) // 获取内容部分 | ||
379 | + return content | ||
380 | + } | ||
381 | + | ||
371 | private func parseDefaultResponseData(_ bytes: [UInt8]) { | 382 | private func parseDefaultResponseData(_ bytes: [UInt8]) { |
372 | guard bytes.count == 17, bytes[9] == 5 else { | 383 | guard bytes.count == 17, bytes[9] == 5 else { |
373 | return | 384 | return |
@@ -390,4 +401,41 @@ extension BluetoothManager { | @@ -390,4 +401,41 @@ extension BluetoothManager { | ||
390 | break | 401 | break |
391 | } | 402 | } |
392 | } | 403 | } |
404 | + | ||
405 | + private func parseDeviceInfoData (_ content:[UInt8]) { | ||
406 | + guard content.count > 0 else { | ||
407 | + print("无有效的信息") | ||
408 | + return | ||
409 | + } | ||
410 | + let totalInfoCount = Int(content[0]) | ||
411 | + var currentIndex = 1 | ||
412 | + | ||
413 | + // 检查总信息数是否大于0 | ||
414 | + guard totalInfoCount > 0 else { | ||
415 | + print("无有效的信息") | ||
416 | + return | ||
417 | + } | ||
418 | + for _ in 0..<totalInfoCount { | ||
419 | + // 检查是否有足够的字节用于解析信息 | ||
420 | + guard currentIndex + 2 < content.count else { | ||
421 | + print("数据不完整") | ||
422 | + return | ||
423 | + } | ||
424 | + let infoID = Int(content[currentIndex]) | ||
425 | + currentIndex += 1 | ||
426 | + let infoLength = Int(content[currentIndex]) | ||
427 | + currentIndex += 1 | ||
428 | + // 检查内容长度是否与实际字节匹配 | ||
429 | + guard currentIndex + infoLength <= content.count else { | ||
430 | + print("信息内容长度不匹配") | ||
431 | + return | ||
432 | + } | ||
433 | + let info = content[currentIndex..<(currentIndex + infoLength)] | ||
434 | + currentIndex += infoLength | ||
435 | + | ||
436 | + // 在此处执行对每个info的操作 | ||
437 | + print("ID: \(infoID)") | ||
438 | + print("内容: \(info)") | ||
439 | + } | ||
440 | + } | ||
393 | } | 441 | } |