diff --git a/HDFwear/Home/Model/HealthModel.swift b/HDFwear/Home/Model/HealthModel.swift index 985d425..e084f16 100644 --- a/HDFwear/Home/Model/HealthModel.swift +++ b/HDFwear/Home/Model/HealthModel.swift @@ -47,7 +47,10 @@ class StepModel: Object { class func toStepModel(_ bytes: [UInt8]) -> StepModel { // let step = bytesToInt(Array(bytes[0..<4])) // print((Float(step)*(0.400 * 175)/100000)) - return StepModel(number: bytesToInt(Array(bytes[0..<4])), calorie: bytesToFloat(Array(bytes[4..<8])), distance: bytesToFloat(Array(bytes[8..<12])), date: DateInRegion().date) + let hexValue = bytes.prefix(4).reduce(0) { ($0 << 8) + UInt32($1) } + let timestamp = TimeInterval(hexValue) + let date = Date(timeIntervalSince1970: timestamp) + return StepModel(number: bytesToInt(Array(bytes[4..<8])), calorie: bytesToFloat(Array(bytes[8..<12])), distance: bytesToFloat(Array(bytes[12..<16])), date: date) } private class func stepToCalorie(_ step: Int) -> Float { diff --git a/HDFwear/Tools/BluetoothManager+Function.swift b/HDFwear/Tools/BluetoothManager+Function.swift index b3235ab..26a1040 100644 --- a/HDFwear/Tools/BluetoothManager+Function.swift +++ b/HDFwear/Tools/BluetoothManager+Function.swift @@ -321,6 +321,8 @@ extension BluetoothManager { parseDeviceInfoData(content) case 0x8009:// 实时步数、卡路里、距离自动上报 print("实时步数、卡路里、距离自动上报") + let content = parseContentFromBytes(bytes) + parseStepCalorieDistanceData(content) case 0x8010:// 电量变化自动上报 print("电量变化自动上报") Battery = Int(bytes[10]) @@ -528,4 +530,19 @@ extension BluetoothManager { SleepModel.addArray(array)// 加入数据库 sleepClosure?(array, nil) } + + // 实时步数,卡路里,距离数据 + func parseStepCalorieDistanceData (_ content: [UInt8]) { + guard content.count == 16 else { + print("无有效的信息") + stepClosure?([], nil) + return + } + let step = StepModel.toStepModel(content) + step.realTimeAdd()// 加入数据库 + for delegate in syncDelegateList { + delegate.didReceiveStep() + } + stepClosure?([step], nil) + } }