Commit ae78ae3c8ed0ff6f4b5e26d59c05ee1760fbba23
1 parent
a96e8729
feat:parse real step calorie distance
Showing
2 changed files
with
21 additions
and
1 deletions
HDFwear/Home/Model/HealthModel.swift
@@ -47,7 +47,10 @@ class StepModel: Object { | @@ -47,7 +47,10 @@ class StepModel: Object { | ||
47 | class func toStepModel(_ bytes: [UInt8]) -> StepModel { | 47 | class func toStepModel(_ bytes: [UInt8]) -> StepModel { |
48 | // let step = bytesToInt(Array(bytes[0..<4])) | 48 | // let step = bytesToInt(Array(bytes[0..<4])) |
49 | // print((Float(step)*(0.400 * 175)/100000)) | 49 | // print((Float(step)*(0.400 * 175)/100000)) |
50 | - return StepModel(number: bytesToInt(Array(bytes[0..<4])), calorie: bytesToFloat(Array(bytes[4..<8])), distance: bytesToFloat(Array(bytes[8..<12])), date: DateInRegion().date) | 50 | + let hexValue = bytes.prefix(4).reduce(0) { ($0 << 8) + UInt32($1) } |
51 | + let timestamp = TimeInterval(hexValue) | ||
52 | + let date = Date(timeIntervalSince1970: timestamp) | ||
53 | + return StepModel(number: bytesToInt(Array(bytes[4..<8])), calorie: bytesToFloat(Array(bytes[8..<12])), distance: bytesToFloat(Array(bytes[12..<16])), date: date) | ||
51 | } | 54 | } |
52 | 55 | ||
53 | private class func stepToCalorie(_ step: Int) -> Float { | 56 | private class func stepToCalorie(_ step: Int) -> Float { |
HDFwear/Tools/BluetoothManager+Function.swift
@@ -321,6 +321,8 @@ extension BluetoothManager { | @@ -321,6 +321,8 @@ extension BluetoothManager { | ||
321 | parseDeviceInfoData(content) | 321 | parseDeviceInfoData(content) |
322 | case 0x8009:// 实时步数、卡路里、距离自动上报 | 322 | case 0x8009:// 实时步数、卡路里、距离自动上报 |
323 | print("实时步数、卡路里、距离自动上报") | 323 | print("实时步数、卡路里、距离自动上报") |
324 | + let content = parseContentFromBytes(bytes) | ||
325 | + parseStepCalorieDistanceData(content) | ||
324 | case 0x8010:// 电量变化自动上报 | 326 | case 0x8010:// 电量变化自动上报 |
325 | print("电量变化自动上报") | 327 | print("电量变化自动上报") |
326 | Battery = Int(bytes[10]) | 328 | Battery = Int(bytes[10]) |
@@ -528,4 +530,19 @@ extension BluetoothManager { | @@ -528,4 +530,19 @@ extension BluetoothManager { | ||
528 | SleepModel.addArray(array)// 加入数据库 | 530 | SleepModel.addArray(array)// 加入数据库 |
529 | sleepClosure?(array, nil) | 531 | sleepClosure?(array, nil) |
530 | } | 532 | } |
533 | + | ||
534 | + // 实时步数,卡路里,距离数据 | ||
535 | + func parseStepCalorieDistanceData (_ content: [UInt8]) { | ||
536 | + guard content.count == 16 else { | ||
537 | + print("无有效的信息") | ||
538 | + stepClosure?([], nil) | ||
539 | + return | ||
540 | + } | ||
541 | + let step = StepModel.toStepModel(content) | ||
542 | + step.realTimeAdd()// 加入数据库 | ||
543 | + for delegate in syncDelegateList { | ||
544 | + delegate.didReceiveStep() | ||
545 | + } | ||
546 | + stepClosure?([step], nil) | ||
547 | + } | ||
531 | } | 548 | } |