Commit 920e260ec93c2c113a01e1bf43bde8bcff2ea306
1 parent
da94b947
feat:data package
Showing
2 changed files
with
198 additions
and
7 deletions
HDFwear/Tools/BleMessage.swift
... | ... | @@ -29,8 +29,12 @@ class BleMessage: NSObject { |
29 | 29 | |
30 | 30 | func getTimeCmd(format: TimeFormat) -> Data { |
31 | 31 | let date = DateInRegion().date |
32 | - let timeBytes: [UInt8] = [UInt8(date.year%100), UInt8(date.month), UInt8(date.day), UInt8(date.hour), UInt8(date.minute), UInt8(date.second), format.rawValue] | |
33 | - return getSendData(cmd: .set, key: .time, bytes: timeBytes) | |
32 | + var timeBytes: [UInt8] = [UInt8(date.year%100), UInt8(date.month), UInt8(date.day), UInt8(date.hour), UInt8(date.minute), UInt8(date.second)] | |
33 | +// return getSendData(cmd: .set, key: .time, bytes: timeBytes) | |
34 | + let a = getPackData(key: .setTime, contentBytes: timeBytes) | |
35 | + let b = createDataPacket(key: .setTime, bytes: timeBytes) | |
36 | + print("ddd") | |
37 | + return createDataPacket(key: .setTime, bytes: timeBytes) | |
34 | 38 | } |
35 | 39 | |
36 | 40 | func getCameraCmd(_ open: Bool) -> Data { |
... | ... | @@ -231,9 +235,185 @@ class BleMessage: NSObject { |
231 | 235 | |
232 | 236 | } |
233 | 237 | |
238 | + func createDataPacket(key: NewCmd, bytes: [UInt8]) -> Data { | |
239 | + let identifier: [UInt8] = [0xED, 0x7E] // 标识 | |
240 | + let version: [UInt8] = [0x00, 0x01] // 协议版本号 | |
241 | + let messageID: [UInt8] = [0x00, key.rawValue] // 消息ID | |
242 | + let messageSequence: [UInt8] = [0x00, 0x01] // 消息序号(示例为固定值) | |
243 | + let contentLength: [UInt8] = [UInt8(bytes.count >> 8), UInt8(bytes.count & 0xFF)] // 内容长度 | |
244 | + let toCrc: [UInt8] = identifier + version + messageID + messageSequence + contentLength + bytes | |
245 | + let crc16 = crc16(toCrc) // 计算CRC-16校验值 | |
246 | + let crc: [UInt8] = [UInt8(crc16 >> 8), UInt8(crc16 & 0xFF)] // 帧校验值 | |
247 | + | |
248 | + // 拼接各个数据段 | |
249 | + let dataSegments = [identifier, version, messageID, messageSequence, contentLength, bytes, crc] | |
250 | + | |
251 | + // 合并数据段为单个数据包 | |
252 | + let packetData = dataSegments.reduce(Data()) { $0 + Data($1) } | |
253 | + return packetData | |
254 | + } | |
234 | 255 | |
256 | + let CRC16_TABLE: [UInt16] = [ | |
257 | + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, | |
258 | + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, | |
259 | + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, | |
260 | + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, | |
261 | + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, | |
262 | + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, | |
263 | + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, | |
264 | + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, | |
265 | + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, | |
266 | + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, | |
267 | + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, | |
268 | + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, | |
269 | + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, | |
270 | + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, | |
271 | + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, | |
272 | + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, | |
273 | + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, | |
274 | + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, | |
275 | + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, | |
276 | + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, | |
277 | + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, | |
278 | + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, | |
279 | + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, | |
280 | + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, | |
281 | + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, | |
282 | + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, | |
283 | + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, | |
284 | + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, | |
285 | + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, | |
286 | + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, | |
287 | + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, | |
288 | + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 | |
289 | + ] | |
290 | + | |
291 | + func crc16(_ bytes: [UInt8]) -> UInt16 { | |
292 | + var crc: UInt16 = 0x0000 | |
293 | + for b in bytes { | |
294 | + crc = (crc << 8) ^ UInt16(CRC16_TABLE[Int((crc >> 8) ^ UInt16(b)) & 0xFF]) | |
295 | + } | |
296 | + return crc & 0xFFFF | |
297 | + } | |
298 | + | |
299 | + func crc16(_ bytes: [UInt8], start: Int, len: Int) -> UInt16 { | |
300 | + var crc: UInt16 = 0x0000 | |
301 | + let end = min(start + len, bytes.count) | |
302 | + for i in start..<end { | |
303 | + crc = (crc << 8) ^ UInt16(CRC16_TABLE[Int((crc >> 8) ^ UInt16(bytes[i])) & 0xFF]) | |
304 | + } | |
305 | + return crc & 0xFFFF | |
306 | + } | |
235 | 307 | |
308 | + | |
309 | + // CRC-16校验算法 | |
310 | +// func calculateCRC16(_ data: Data) -> UInt16 { | |
311 | +// var crc: UInt16 = 0x0000 | |
312 | +// | |
313 | +// for byte in data { | |
314 | +// crc = crc ^ UInt16(byte) | |
315 | +// | |
316 | +// for _ in 0..<8 { | |
317 | +// if (crc & 0x0001) != 0 { | |
318 | +// crc = (crc >> 1) ^ 0xA001 | |
319 | +// } else { | |
320 | +// crc = crc >> 1 | |
321 | +// } | |
322 | +// } | |
323 | +// } | |
324 | +// | |
325 | +// return crc | |
326 | +// } | |
327 | +// | |
328 | +// func CCITT_CRC16_FFFF(_ data: [UInt8]) -> UInt16 { | |
329 | + | |
330 | +// | |
331 | +// var nAccum: UInt16 = 0x0000 | |
332 | +// | |
333 | +// for byte in data { | |
334 | +// let index = Int((nAccum >> 8) ^ UInt16(byte)) | |
335 | +// nAccum = (nAccum << 8) ^ Table_CRC16_CCITT1021[index] | |
336 | +// } | |
337 | +// | |
338 | +// return nAccum | |
339 | +// } | |
236 | 340 | |
237 | 341 | |
238 | - | |
342 | +// func getPackData(key: String, contentBytes: [UInt8]) -> [UInt8] { | |
343 | +// let messageNumber = 0xed7e | |
344 | +// var messageIndexByte: [UInt8] = [0, 0] | |
345 | +// messageIndexByte[1] = UInt8(messageNumber & 0x00ff) | |
346 | +// messageIndexByte[0] = UInt8((messageNumber & 0xff00) >> 8) | |
347 | +// | |
348 | +// let contentLength = contentBytes.count | |
349 | +// var contentLengthByte: [UInt8] = [0, 0] | |
350 | +// contentLengthByte[1] = UInt8(contentLength & 0x00ff) | |
351 | +// contentLengthByte[0] = UInt8((contentLength & 0xff00) >> 8) | |
352 | +// | |
353 | +// let messageIdByte = Array<UInt8>(hexString: key) | |
354 | +// | |
355 | +// var length = headBytes.count + messageIdByte.count + messageIndexByte.count + contentLengthByte.count + contentBytes.count | |
356 | +// | |
357 | +// var paramsByte = [UInt8](repeating: 0, count: length) | |
358 | +// var insertLength = 0 | |
359 | +// | |
360 | +// paramsByte[insertLength..<insertLength+headBytes.count] = headBytes | |
361 | +// insertLength += headBytes.count | |
362 | +// | |
363 | +// paramsByte[insertLength..<insertLength+messageIdByte.count] = messageIdByte | |
364 | +// insertLength += messageIdByte.count | |
365 | +// | |
366 | +// paramsByte[insertLength..<insertLength+messageIndexByte.count] = messageIndexByte | |
367 | +// insertLength += messageIndexByte.count | |
368 | +// | |
369 | +// paramsByte[insertLength..<insertLength+contentLengthByte.count] = contentLengthByte | |
370 | +// insertLength += contentLengthByte.count | |
371 | +// | |
372 | +// paramsByte[insertLength..<insertLength+contentBytes.count] = contentBytes | |
373 | +// insertLength += contentBytes.count | |
374 | +// | |
375 | +// let crcResult = CRCUtils.crc16(paramsByte) | |
376 | +// var crc: [UInt8] = [0, 0] | |
377 | +// crc[0] = UInt8((crcResult >> 8) & 0xff) | |
378 | +// crc[1] = UInt8(crcResult & 0xff) | |
379 | +// | |
380 | +// length += 2 | |
381 | +// | |
382 | +// var total = [UInt8](repeating: 0, count: length) | |
383 | +// total[0..<paramsByte.count] = paramsByte | |
384 | +// total[paramsByte.count..<total.count] = crc | |
385 | +// | |
386 | +// return total | |
387 | +// } | |
388 | + | |
389 | + func getPackData(key: NewCmd, contentBytes: [UInt8]) -> [UInt8] { | |
390 | + let headBytes: [UInt8] = [0xED, 0x7E, 0x00, 0x01] | |
391 | + let messageNumber: UInt16 = 1 | |
392 | + var messageIndexByte: [UInt8] = [0, 0] | |
393 | + messageIndexByte[1] = UInt8(messageNumber & 0x00ff) | |
394 | + messageIndexByte[0] = UInt8((messageNumber & 0xff00) >> 8) | |
395 | + | |
396 | + let contentLength = contentBytes.count | |
397 | + var contentLengthByte: [UInt8] = [0, 0] | |
398 | + contentLengthByte[1] = UInt8(contentLength & 0x00ff) | |
399 | + contentLengthByte[0] = UInt8((contentLength & 0xff00) >> 8) | |
400 | + | |
401 | + let messageIdByte: [UInt8] = [0x00, key.rawValue] // 消息ID | |
402 | + | |
403 | + var paramsByte: [UInt8] = [] | |
404 | + paramsByte.append(contentsOf: headBytes) | |
405 | + paramsByte.append(contentsOf: messageIdByte) | |
406 | + paramsByte.append(contentsOf: messageIndexByte) | |
407 | + paramsByte.append(contentsOf: contentLengthByte) | |
408 | + paramsByte.append(contentsOf: contentBytes) | |
409 | + | |
410 | + let crc_result = crc16(paramsByte) | |
411 | + let crc: [UInt8] = [UInt8((crc_result >> 8) & 0xff), UInt8(crc_result & 0xff)] | |
412 | + | |
413 | + var total: [UInt8] = [] | |
414 | + total.append(contentsOf: paramsByte) | |
415 | + total.append(contentsOf: crc) | |
416 | + | |
417 | + return total | |
418 | + } | |
239 | 419 | } | ... | ... |
HDFwear/Tools/Bluetooth+Types.swift
... | ... | @@ -22,12 +22,19 @@ struct ScanDevice { |
22 | 22 | |
23 | 23 | |
24 | 24 | |
25 | - | |
25 | +//jtd! 还需要校对 | |
26 | 26 | public struct BLEConfig { |
27 | + // 新 | |
28 | + static let ServerUUID = "0000FFF0-0000-1000-8000-00805F9B34FB" //主服务UUID | |
29 | + static let ReadUUID = "0000FFF3-0000-1000-8000-00805F9B34FB" //读取数据特征值 | |
30 | + static let WriteUUID = "0000FFF2-0000-1000-8000-00805F9B34FB" //写入数据特征值 | |
31 | + | |
32 | +// 0000FFF4-0000-1000-8000-00805F9B34FB // 转换特征值 | |
27 | 33 | |
28 | - static let ServerUUID = "C3E6FEA0-E966-1000-8000-BE99C223DF6A" //主服务UUID | |
29 | - static let ReadUUID = "C3E6FEA2-E966-1000-8000-BE99C223DF6A" //读取数据特征值 | |
30 | - static let WriteUUID = "C3E6FEA1-E966-1000-8000-BE99C223DF6A" //写入数据特征值 | |
34 | + // 旧 | |
35 | +// static let ServerUUID = "C3E6FEA0-E966-1000-8000-BE99C223DF6A" //主服务UUID | |
36 | +// static let ReadUUID = "C3E6FEA2-E966-1000-8000-BE99C223DF6A" //读取数据特征值 | |
37 | +// static let WriteUUID = "C3E6FEA1-E966-1000-8000-BE99C223DF6A" //写入数据特征值 | |
31 | 38 | |
32 | 39 | |
33 | 40 | static let MTKBasServerUUID = "0000180F-0000-1000-8000-00805F9B34FB" //mtk2502 Bas主服务UUID |
... | ... | @@ -107,6 +114,10 @@ enum SetCmd: UInt8 { |
107 | 114 | case set_return = 0x2F |
108 | 115 | } |
109 | 116 | |
117 | +enum NewCmd: UInt8 { | |
118 | + case setTime = 0x09 | |
119 | +} | |
120 | + | |
110 | 121 | enum SyncCmd: UInt8 { |
111 | 122 | case sleep = 0xA2 |
112 | 123 | case step = 0xA3 | ... | ... |