Commit 22694b44ad3f523a43783c4fd883bc3f5ee03165
1 parent
2db9fbc3
feat:default response data parse
Showing
1 changed file
with
61 additions
and
27 deletions
HDFwear/Tools/BluetoothManager.swift
@@ -1226,6 +1226,32 @@ class BluetoothManager: NSObject { | @@ -1226,6 +1226,32 @@ class BluetoothManager: NSObject { | ||
1226 | AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) | 1226 | AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) |
1227 | } | 1227 | } |
1228 | 1228 | ||
1229 | + //MARK: - 新解析数据 | ||
1230 | + private func newParseDefaultResponseData(_ bytes: [UInt8]) { | ||
1231 | + guard bytes.count == 17, bytes[9] == 5 else { | ||
1232 | + return | ||
1233 | + } | ||
1234 | + let newcmd = NewCmd(rawValue: bytes[13]) | ||
1235 | + switch newcmd { | ||
1236 | + case .setTime: | ||
1237 | + if setCmdClosure != nil { | ||
1238 | + let success = bytes[14] == 0 | ||
1239 | + if success { | ||
1240 | + setCmdClosure?(nil) | ||
1241 | + }else { | ||
1242 | + setCmdClosure?((Int)(bytes[14])) | ||
1243 | + } | ||
1244 | + setCmdClosure = nil | ||
1245 | + } | ||
1246 | + case .none: | ||
1247 | + break | ||
1248 | + } | ||
1249 | + | ||
1250 | + } | ||
1251 | + | ||
1252 | + | ||
1253 | + //MARK: - 发送数据 | ||
1254 | + | ||
1229 | private func sendData(_ data: Data) { | 1255 | private func sendData(_ data: Data) { |
1230 | if peripheral == nil { | 1256 | if peripheral == nil { |
1231 | print("peripheral == nil") | 1257 | print("peripheral == nil") |
@@ -1552,7 +1578,7 @@ extension BluetoothManager: ClientProfileDelegate { | @@ -1552,7 +1578,7 @@ extension BluetoothManager: ClientProfileDelegate { | ||
1552 | } | 1578 | } |
1553 | } | 1579 | } |
1554 | case BLEConfig.ReadUUID: | 1580 | case BLEConfig.ReadUUID: |
1555 | -// print("?????????????\([UInt8](data))") | 1581 | + print("?????????????\([UInt8](data))") |
1556 | mergeData(data) | 1582 | mergeData(data) |
1557 | default: | 1583 | default: |
1558 | mergeData(data) | 1584 | mergeData(data) |
@@ -1563,19 +1589,20 @@ extension BluetoothManager: ClientProfileDelegate { | @@ -1563,19 +1589,20 @@ extension BluetoothManager: ClientProfileDelegate { | ||
1563 | let bytes = [UInt8](data) | 1589 | let bytes = [UInt8](data) |
1564 | print(bytes) | 1590 | print(bytes) |
1565 | isSyncReceived = true | 1591 | isSyncReceived = true |
1566 | - if bytes[0] == 0xBA && (bytes[1] == 48 || bytes[1] == 112) { | ||
1567 | - if bytes.count > 10, BleCmd(rawValue: bytes[8]) == .sync { | ||
1568 | - if isSync { | ||
1569 | - if SyncCmd(rawValue: bytes[10]) != isSyncCmd { | ||
1570 | - syncReceiveFail() | ||
1571 | - } else { | ||
1572 | - isNextSync = bytes[1] == 112 | ||
1573 | - startSyncTimer() | ||
1574 | - isSyncReceived = true | ||
1575 | - } | ||
1576 | - } | ||
1577 | - } | ||
1578 | - totalLength = Int(UInt16(bytes[6]) << 8 | UInt16(bytes[7])) + 8 | 1592 | + if bytes[0] == 0xED && bytes[1] == 0x7E { |
1593 | +// if bytes.count > 10, BleCmd(rawValue: bytes[8]) == .sync { | ||
1594 | +// if isSync { | ||
1595 | +// if SyncCmd(rawValue: bytes[10]) != isSyncCmd { | ||
1596 | +// syncReceiveFail() | ||
1597 | +// } else { | ||
1598 | +// isNextSync = bytes[1] == 112 | ||
1599 | +// startSyncTimer() | ||
1600 | +// isSyncReceived = true | ||
1601 | +// } | ||
1602 | +// } | ||
1603 | +// } | ||
1604 | + // 内容长度加上其他位的长度 | ||
1605 | + totalLength = Int(UInt16(bytes[8]) << 8 | UInt16(bytes[9])) + 12 | ||
1579 | if data.count == totalLength { | 1606 | if data.count == totalLength { |
1580 | parseData(bytes) | 1607 | parseData(bytes) |
1581 | } else { | 1608 | } else { |
@@ -1604,21 +1631,28 @@ extension BluetoothManager: ClientProfileDelegate { | @@ -1604,21 +1631,28 @@ extension BluetoothManager: ClientProfileDelegate { | ||
1604 | totalLength = 0 | 1631 | totalLength = 0 |
1605 | mergeLength = 0 | 1632 | mergeLength = 0 |
1606 | totalBytes = [] | 1633 | totalBytes = [] |
1607 | - guard bytes.count > 10, let cmd = BleCmd(rawValue: bytes[8]) else { return } | 1634 | + guard bytes.count > 12 else { return } |
1635 | + let msgId = Int(UInt16(bytes[4]) << 8 | UInt16(bytes[5])) | ||
1608 | let data = Data(bytes: bytes, count: bytes.count) | 1636 | let data = Data(bytes: bytes, count: bytes.count) |
1609 | - switch cmd { | ||
1610 | - case .set, .find, .device, .remind, .firmware: | ||
1611 | - parseSetData(SetCmd(rawValue: bytes[10]), data: data) | ||
1612 | - case .sync: | ||
1613 | - let valueBytes = Array(bytes[13..<bytes.count]) | ||
1614 | - print(valueBytes) | ||
1615 | - parseSyncData(SyncCmd(rawValue: bytes[10]), bytes: valueBytes) | ||
1616 | - case .weather: | ||
1617 | - if setCmdClosure != nil { | ||
1618 | - setCmdClosure?(nil) | ||
1619 | - setCmdClosure = nil | ||
1620 | - } | 1637 | + switch msgId { |
1638 | + case 0x8001: | ||
1639 | + newParseDefaultResponseData(bytes) | ||
1640 | + default: | ||
1641 | + break | ||
1621 | } | 1642 | } |
1643 | +// switch cmd { | ||
1644 | +// case .set, .find, .device, .remind, .firmware: | ||
1645 | +// parseSetData(SetCmd(rawValue: bytes[10]), data: data) | ||
1646 | +// case .sync: | ||
1647 | +// let valueBytes = Array(bytes[13..<bytes.count]) | ||
1648 | +// print(valueBytes) | ||
1649 | +// parseSyncData(SyncCmd(rawValue: bytes[10]), bytes: valueBytes) | ||
1650 | +// case .weather: | ||
1651 | +// if setCmdClosure != nil { | ||
1652 | +// setCmdClosure?(nil) | ||
1653 | +// setCmdClosure = nil | ||
1654 | +// } | ||
1655 | +// } | ||
1622 | } | 1656 | } |
1623 | 1657 | ||
1624 | 1658 |