Commit 0e8d5378e87f36b5519b5c9fe4f568dbaba49fec
1 parent
cc38043f
refactor
Showing
2 changed files
with
122 additions
and
106 deletions
HDFwear/Tools/BluetoothManager+Function.swift
... | ... | @@ -126,4 +126,122 @@ extension BluetoothManager { |
126 | 126 | self.setCmdClosure = completion |
127 | 127 | sendData(data) |
128 | 128 | } |
129 | + | |
130 | + //MARK: - 发送数据 | |
131 | + func sendData(_ data: Data) { | |
132 | + if peripheral == nil { | |
133 | + print("peripheral == nil") | |
134 | + return | |
135 | + } | |
136 | + | |
137 | + var serverUUID: String = "" | |
138 | + var writeUUID: String = "" | |
139 | + switch platform { | |
140 | + case ._828: | |
141 | + serverUUID = BLEConfig.ServerUUID | |
142 | + writeUUID = BLEConfig.WriteUUID | |
143 | + default: | |
144 | + break | |
145 | + } | |
146 | + | |
147 | + if let services = peripheral?.services { | |
148 | + for service in services { | |
149 | + if service.uuid.uuidString == serverUUID { | |
150 | + if service.characteristics == nil { | |
151 | + return | |
152 | + } | |
153 | + for char in service.characteristics! { | |
154 | + if char.uuid.uuidString == writeUUID { | |
155 | + if platform == ._828 { | |
156 | + if data.count <= 20 { | |
157 | + print("发送: \([UInt8](data))") | |
158 | + peripheral?.writeValue(data, for: char, type: .withoutResponse) | |
159 | + } else { | |
160 | + print("发送 拼: \([UInt8](data))") | |
161 | + let n = data.count/20 + (data.count%20 == 0 ? 0 : 1) | |
162 | + for i in 0..<n { | |
163 | + peripheral?.writeValue(data[i*20..<min(i*20+20, data.count)], for: char, type: .withoutResponse) | |
164 | + } | |
165 | + } | |
166 | + } else { | |
167 | + print("发送: \([UInt8](data))") | |
168 | + peripheral?.writeValue(data, for: char, type: .withResponse) | |
169 | + } | |
170 | + } | |
171 | + } | |
172 | + } | |
173 | + } | |
174 | + } | |
175 | + } | |
176 | + //MARK: - 新解析数据 | |
177 | + func parseData(_ bytes: [UInt8]) { | |
178 | + totalLength = 0 | |
179 | + mergeLength = 0 | |
180 | + totalBytes = [] | |
181 | + guard bytes.count > 12 else { return } | |
182 | + let msgId = Int(UInt16(bytes[4]) << 8 | UInt16(bytes[5])) | |
183 | + let data = Data(bytes: bytes, count: bytes.count) | |
184 | + switch msgId { | |
185 | + case 0x8001: | |
186 | + parseDefaultResponseData(bytes) | |
187 | + case 0x8002:// 设备信息查询应答 | |
188 | + print("设备信息查询应答") | |
189 | + case 0x8009:// 实时步数、卡路里、距离自动上报 | |
190 | + print("实时步数、卡路里、距离自动上报") | |
191 | + case 0x8010:// 电量变化自动上报 | |
192 | + print("电量变化自动上报") | |
193 | + case 0x8014:// 电话挂断 | |
194 | + print("电话挂断") | |
195 | + case 0x8015:// 找手机 | |
196 | + print("找手机") | |
197 | + case 0x8016:// 历史心率数据上报 | |
198 | + print("历史心率数据上报") | |
199 | + case 0x8017:// 历史血氧数据上报 | |
200 | + print("历史血氧数据上报") | |
201 | + case 0x8018:// 历史体温数据上报 | |
202 | + print("历史体温数据上报") | |
203 | + case 0x8019:// 睡眠数据上报 | |
204 | + print("睡眠数据上报") | |
205 | + case 0x8020:// 历史压力数据上报 | |
206 | + print("历史压力数据上报") | |
207 | + default: | |
208 | + break | |
209 | + } | |
210 | +// switch cmd { | |
211 | +// case .set, .find, .device, .remind, .firmware: | |
212 | +// parseSetData(SetCmd(rawValue: bytes[10]), data: data) | |
213 | +// case .sync: | |
214 | +// let valueBytes = Array(bytes[13..<bytes.count]) | |
215 | +// print(valueBytes) | |
216 | +// parseSyncData(SyncCmd(rawValue: bytes[10]), bytes: valueBytes) | |
217 | +// case .weather: | |
218 | +// if setCmdClosure != nil { | |
219 | +// setCmdClosure?(nil) | |
220 | +// setCmdClosure = nil | |
221 | +// } | |
222 | +// } | |
223 | + } | |
224 | + | |
225 | + private func parseDefaultResponseData(_ bytes: [UInt8]) { | |
226 | + guard bytes.count == 17, bytes[9] == 5 else { | |
227 | + return | |
228 | + } | |
229 | + let newcmd = NewCmd(rawValue: bytes[13]) | |
230 | + switch newcmd { | |
231 | + case .setTime, .setTimeFormat, .setTemperatureUnit, .setDistanceUnit, .setWristSense, .setTouchSense, .setLowPowerRemind, .setLanguage: | |
232 | + if setCmdClosure != nil { | |
233 | + let success = bytes[14] == 0 | |
234 | + if success { | |
235 | + setCmdClosure?(nil) | |
236 | + }else { | |
237 | + setCmdClosure?((Int)(bytes[14])) | |
238 | + } | |
239 | + setCmdClosure = nil | |
240 | + } | |
241 | + case .none: | |
242 | + break | |
243 | + default: | |
244 | + break | |
245 | + } | |
246 | + } | |
129 | 247 | } | ... | ... |
HDFwear/Tools/BluetoothManager.swift
... | ... | @@ -113,7 +113,7 @@ class BluetoothManager: NSObject { |
113 | 113 | // weak var delegate: BluetoothManagerDelegate? |
114 | 114 | |
115 | 115 | var manger: MTKBleManager? |
116 | - private var peripheral: CBPeripheral? = nil | |
116 | + var peripheral: CBPeripheral? = nil | |
117 | 117 | // private var conPeripheral: CBPeripheral? = nil //已连接的 |
118 | 118 | var platform: Platform = .other |
119 | 119 | |
... | ... | @@ -127,9 +127,9 @@ class BluetoothManager: NSObject { |
127 | 127 | |
128 | 128 | |
129 | 129 | //mergeData |
130 | - private var totalBytes: [UInt8] = [] | |
131 | - private var totalLength: Int = 0 | |
132 | - private var mergeLength: Int = 0 | |
130 | + var totalBytes: [UInt8] = [] | |
131 | + var totalLength: Int = 0 | |
132 | + var mergeLength: Int = 0 | |
133 | 133 | |
134 | 134 | //sync |
135 | 135 | private var isSyncReceived: Bool = false //是否收到同步data |
... | ... | @@ -1217,80 +1217,6 @@ class BluetoothManager: NSObject { |
1217 | 1217 | AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) |
1218 | 1218 | } |
1219 | 1219 | |
1220 | - //MARK: - 新解析数据 | |
1221 | - private func newParseDefaultResponseData(_ bytes: [UInt8]) { | |
1222 | - guard bytes.count == 17, bytes[9] == 5 else { | |
1223 | - return | |
1224 | - } | |
1225 | - let newcmd = NewCmd(rawValue: bytes[13]) | |
1226 | - switch newcmd { | |
1227 | - case .setTime, .setTimeFormat, .setTemperatureUnit, .setDistanceUnit, .setWristSense, .setTouchSense, .setLowPowerRemind, .setLanguage: | |
1228 | - if setCmdClosure != nil { | |
1229 | - let success = bytes[14] == 0 | |
1230 | - if success { | |
1231 | - setCmdClosure?(nil) | |
1232 | - }else { | |
1233 | - setCmdClosure?((Int)(bytes[14])) | |
1234 | - } | |
1235 | - setCmdClosure = nil | |
1236 | - } | |
1237 | - case .none: | |
1238 | - break | |
1239 | - default: | |
1240 | - break | |
1241 | - } | |
1242 | - | |
1243 | - } | |
1244 | - | |
1245 | - | |
1246 | - //MARK: - 发送数据 | |
1247 | - | |
1248 | - func sendData(_ data: Data) { | |
1249 | - if peripheral == nil { | |
1250 | - print("peripheral == nil") | |
1251 | - return | |
1252 | - } | |
1253 | - | |
1254 | - var serverUUID: String = "" | |
1255 | - var writeUUID: String = "" | |
1256 | - switch platform { | |
1257 | - case ._828: | |
1258 | - serverUUID = BLEConfig.ServerUUID | |
1259 | - writeUUID = BLEConfig.WriteUUID | |
1260 | - default: | |
1261 | - break | |
1262 | - } | |
1263 | - | |
1264 | - if let services = peripheral?.services { | |
1265 | - for service in services { | |
1266 | - if service.uuid.uuidString == serverUUID { | |
1267 | - if service.characteristics == nil { | |
1268 | - return | |
1269 | - } | |
1270 | - for char in service.characteristics! { | |
1271 | - if char.uuid.uuidString == writeUUID { | |
1272 | - if platform == ._828 { | |
1273 | - if data.count <= 20 { | |
1274 | - print("发送: \([UInt8](data))") | |
1275 | - peripheral?.writeValue(data, for: char, type: .withoutResponse) | |
1276 | - } else { | |
1277 | - print("发送 拼: \([UInt8](data))") | |
1278 | - let n = data.count/20 + (data.count%20 == 0 ? 0 : 1) | |
1279 | - for i in 0..<n { | |
1280 | - peripheral?.writeValue(data[i*20..<min(i*20+20, data.count)], for: char, type: .withoutResponse) | |
1281 | - } | |
1282 | - } | |
1283 | - } else { | |
1284 | - print("发送: \([UInt8](data))") | |
1285 | - peripheral?.writeValue(data, for: char, type: .withResponse) | |
1286 | - } | |
1287 | - } | |
1288 | - } | |
1289 | - } | |
1290 | - } | |
1291 | - } | |
1292 | - } | |
1293 | - | |
1294 | 1220 | private func sendCustomDialData(_ data: Data) { |
1295 | 1221 | if !isSyncDial { return } |
1296 | 1222 | if peripheral == nil || data.count == 0 { |
... | ... | @@ -1620,34 +1546,6 @@ extension BluetoothManager: ClientProfileDelegate { |
1620 | 1546 | |
1621 | 1547 | } |
1622 | 1548 | |
1623 | - func parseData(_ bytes: [UInt8]) { | |
1624 | - totalLength = 0 | |
1625 | - mergeLength = 0 | |
1626 | - totalBytes = [] | |
1627 | - guard bytes.count > 12 else { return } | |
1628 | - let msgId = Int(UInt16(bytes[4]) << 8 | UInt16(bytes[5])) | |
1629 | - let data = Data(bytes: bytes, count: bytes.count) | |
1630 | - switch msgId { | |
1631 | - case 0x8001: | |
1632 | - newParseDefaultResponseData(bytes) | |
1633 | - default: | |
1634 | - break | |
1635 | - } | |
1636 | -// switch cmd { | |
1637 | -// case .set, .find, .device, .remind, .firmware: | |
1638 | -// parseSetData(SetCmd(rawValue: bytes[10]), data: data) | |
1639 | -// case .sync: | |
1640 | -// let valueBytes = Array(bytes[13..<bytes.count]) | |
1641 | -// print(valueBytes) | |
1642 | -// parseSyncData(SyncCmd(rawValue: bytes[10]), bytes: valueBytes) | |
1643 | -// case .weather: | |
1644 | -// if setCmdClosure != nil { | |
1645 | -// setCmdClosure?(nil) | |
1646 | -// setCmdClosure = nil | |
1647 | -// } | |
1648 | -// } | |
1649 | - } | |
1650 | - | |
1651 | 1549 | |
1652 | 1550 | func onCharacteristicWrite() { |
1653 | 1551 | // print("已经连接5") | ... | ... |