Commit ae78ae3c8ed0ff6f4b5e26d59c05ee1760fbba23

Authored by daifengyi
1 parent a96e8729

feat:parse real step calorie distance

HDFwear/Home/Model/HealthModel.swift
... ... @@ -47,7 +47,10 @@ class StepModel: Object {
47 47 class func toStepModel(_ bytes: [UInt8]) -> StepModel {
48 48 // let step = bytesToInt(Array(bytes[0..<4]))
49 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 56 private class func stepToCalorie(_ step: Int) -> Float {
... ...
HDFwear/Tools/BluetoothManager+Function.swift
... ... @@ -321,6 +321,8 @@ extension BluetoothManager {
321 321 parseDeviceInfoData(content)
322 322 case 0x8009:// 实时步数、卡路里、距离自动上报
323 323 print("实时步数、卡路里、距离自动上报")
  324 + let content = parseContentFromBytes(bytes)
  325 + parseStepCalorieDistanceData(content)
324 326 case 0x8010:// 电量变化自动上报
325 327 print("电量变化自动上报")
326 328 Battery = Int(bytes[10])
... ... @@ -528,4 +530,19 @@ extension BluetoothManager {
528 530 SleepModel.addArray(array)// 加入数据库
529 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 }
... ...