// // HeightBodyFatScaleVC.m // AILinkBleSDK_Example // // Created by LarryZhang on 2023/2/4. // Copyright © 2023 zhengzida. All rights reserved. // #import "HeightBodyFatScaleVC.h" #import #import #import "ELDemoButton.h" @interface HeightBodyFatScaleVC () @property (weak, nonatomic) IBOutlet UILabel *bleStatusLabel; //work mode @property (weak, nonatomic) IBOutlet ELDemoButton *weightModeButton; @property (weak, nonatomic) IBOutlet ELDemoButton *weightHeightModeButton; @property (weak, nonatomic) IBOutlet ELDemoButton *weightHeightFatModeButton; @property (weak, nonatomic) IBOutlet ELDemoButton *withBabyModeButton; //Weight @property (weak, nonatomic) IBOutlet UILabel *weightValueLabel; //Height @property (weak, nonatomic) IBOutlet UILabel *heightValueLabel; //Setting @property (weak, nonatomic) IBOutlet ELDemoButton *voiceSettingButton; @property (weak, nonatomic) IBOutlet ELDemoButton *uintSettingButton; //workMode @property (nonatomic, assign) HeightBodyFatScale_WorkModeType workMode; //voice on/of @property (nonatomic, assign) BOOL voiceOn; //unit @property (nonatomic, assign) ELDeviceWeightUnit weightUnit; @property (nonatomic, assign) ELDeviceHeightUnit heightUnit; //report @property (weak, nonatomic) IBOutlet UILabel *reportValueLabel; //baby mode value @property (weak, nonatomic) IBOutlet UILabel *babyWeightValueLabel; @property (weak, nonatomic) IBOutlet UILabel *adultWeightValueLabel; @property (weak, nonatomic) IBOutlet UILabel *babyAdultWeightValueLabel; /** 抱婴模式下的测量状态下标 */ @property (nonatomic, assign) NSInteger babyModeIndex; @end @implementation HeightBodyFatScaleVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.weightUnit = ELDeviceWeightUnit_KG; self.heightUnit = ELDeviceHeightUnit_CM; [self uiSetUnit]; self.babyModeIndex = 0; [self bleScan]; } - (void)dealloc { [ELHeightBodyFatScaleBleManager shareManager].heightBodyFatScaleDelegate = nil; [[ELHeightBodyFatScaleBleManager shareManager] disconnectPeripheral]; } - (void)bleScan { //创建蓝牙 [ELHeightBodyFatScaleBleManager shareManager].heightBodyFatScaleDelegate = self; [[ELHeightBodyFatScaleBleManager shareManager] startScan]; } //发送用户信息 - (void)sendUserData { //// 0 female、1 male & 178(cm) [[ELHeightBodyFatScaleBleManager shareManager] sendUserDataWithGender:1 age:20 heightInCm:178]; } #pragma mark -UI - (void)uiSetWorkMode:(HeightBodyFatScale_WorkModeType)mode { self.weightModeButton.lighted = NO; self.weightHeightModeButton.lighted = NO; self.weightHeightFatModeButton.lighted = NO; self.withBabyModeButton.lighted = NO; if (mode == HeightBodyFatScale_WorkModeType_WeightMode) { self.weightModeButton.lighted = YES; } else if (mode == HeightBodyFatScale_WorkModeType_WeightHeightMode) { self.weightHeightModeButton.lighted = YES; } else if (mode == HeightBodyFatScale_WorkModeType_HeightBodyFatMode) { self.weightHeightFatModeButton.lighted = YES; } else if (mode == HeightBodyFatScale_WorkModeType_BabyMode) { self.withBabyModeButton.lighted = YES; } } - (void)uiSetVoiceOn:(BOOL)voiceOn { self.voiceSettingButton.lighted = voiceOn; if (voiceOn) { [self.voiceSettingButton setTitle:@"Voice: On" forState:UIControlStateNormal]; } else { [self.voiceSettingButton setTitle:@"Voice: Off" forState:UIControlStateNormal]; } } - (void)uiSetUnit { if (self.weightUnit == ELDeviceWeightUnit_KG) { [self.uintSettingButton setTitle:[NSString stringWithFormat:@"Unit: kg & cm"] forState:UIControlStateNormal]; } else { [self.uintSettingButton setTitle:[NSString stringWithFormat:@"Unit: lb & ft"] forState:UIControlStateNormal]; } } #pragma mark -Setter Getter - (void)setWorkMode:(HeightBodyFatScale_WorkModeType)workMode { _workMode = workMode; [self uiSetWorkMode:workMode]; } - (void)setVoiceOn:(BOOL)voiceOn { _voiceOn = voiceOn; [self uiSetVoiceOn:voiceOn]; } #pragma mark -Actions - (IBAction)weightButtonAction:(id)sender { [[ELHeightBodyFatScaleBleManager shareManager] sendDeviceWorkMode:HeightBodyFatScale_WorkModeType_WeightMode]; self.workMode = HeightBodyFatScale_WorkModeType_WeightMode; } - (IBAction)weightHeightButtonAction:(id)sender { [[ELHeightBodyFatScaleBleManager shareManager] sendDeviceWorkMode:HeightBodyFatScale_WorkModeType_WeightHeightMode]; self.workMode = HeightBodyFatScale_WorkModeType_WeightHeightMode; } - (IBAction)weightHeightFatButtonAction:(id)sender { [[ELHeightBodyFatScaleBleManager shareManager] sendDeviceWorkMode:HeightBodyFatScale_WorkModeType_HeightBodyFatMode]; self.workMode = HeightBodyFatScale_WorkModeType_HeightBodyFatMode; } - (IBAction)withBabyButtonAction:(id)sender { [[ELHeightBodyFatScaleBleManager shareManager] sendDeviceWorkMode:HeightBodyFatScale_WorkModeType_BabyMode]; self.workMode = HeightBodyFatScale_WorkModeType_BabyMode; } - (IBAction)voiceSettingButtonAction:(id)sender { BOOL voiceOn = !self.voiceOn; [[ELHeightBodyFatScaleBleManager shareManager] sendVoiceSetting:voiceOn]; self.voiceOn = voiceOn; } - (IBAction)unitSettingButtonAction:(id)sender { if (self.weightUnit == ELDeviceWeightUnit_KG) { self.weightUnit = ELDeviceWeightUnit_LB; self.heightUnit = ELDeviceHeightUnit_FT_IN; [[ELHeightBodyFatScaleBleManager shareManager] sendUnitWithHeightUnit:ELDeviceHeightUnit_FT_IN weightUnit:ELDeviceWeightUnit_LB]; } else { self.weightUnit = ELDeviceWeightUnit_KG; self.heightUnit = ELDeviceHeightUnit_CM; [[ELHeightBodyFatScaleBleManager shareManager] sendUnitWithHeightUnit:ELDeviceHeightUnit_CM weightUnit:ELDeviceWeightUnit_KG]; } [self uiSetUnit]; } #pragma mark - util + (NSString *)getWeightStringWithWeight:(NSInteger)weight unit:(ELDeviceWeightUnit)unit point:(NSInteger)point { NSString *formatStr = [NSString stringWithFormat:@"%%.%luf",(unsigned long)point]; switch (unit) { case ELDeviceWeightUnit_KG: { double kgNumber = (double)weight/pow(10, point); NSString *weightString = [NSString stringWithFormat:formatStr,kgNumber]; return weightString; } case ELDeviceWeightUnit_JIN: { double jinNumber = (double)weight/pow(10, point); NSString *jinString = [NSString stringWithFormat:formatStr,jinNumber]; return jinString; } case ELDeviceWeightUnit_LB_OZ: { //返回小单位oz double ozNumber = (double)weight/pow(10, point); NSInteger lbNumber = ozNumber / 16; NSString *ozString = [NSString stringWithFormat:formatStr,(ozNumber-lbNumber*16)]; return [NSString stringWithFormat:@"%zd:%@",lbNumber,ozString]; } case ELDeviceWeightUnit_OZ: { double ozNumber = (double)weight/pow(10, point); NSString *ozString = [NSString stringWithFormat:formatStr,ozNumber]; return ozString; } case ELDeviceWeightUnit_ST_LB: { //返回小单位lb double lbNumber = (double)weight/pow(10, point); NSInteger stNumber = lbNumber/14; NSString *lbString = [NSString stringWithFormat:formatStr,(lbNumber-stNumber*14)]; return [NSString stringWithFormat:@"%zd:%@",stNumber,lbString]; } case ELDeviceWeightUnit_G: { double gNunber = (double)weight/pow(10, point); NSString *gString = [NSString stringWithFormat:formatStr,gNunber]; return gString; } case ELDeviceWeightUnit_LB: { double lbNumber = (double)weight/pow(10, point); NSString *lbString = [NSString stringWithFormat:formatStr,lbNumber]; return lbString; } default: { //不支持的单位 return @""; } } return @""; } + (NSString *)getHeightStringWithHeight:(NSInteger)height unit:(ELDeviceHeightUnit)unit point:(NSInteger)point { NSString *formatStr = [NSString stringWithFormat:@"%%.%luf", (unsigned long)point]; switch (unit) { case ELDeviceHeightUnit_CM: { double cmNumber = (double)height/pow(10, point); NSString *cmString = [NSString stringWithFormat:formatStr,cmNumber]; return cmString; } case ELDeviceHeightUnit_Inch: { double inchNumber = (double)height/pow(10, point); NSString *inchString = [NSString stringWithFormat:formatStr,inchNumber]; return inchString; } case ELDeviceHeightUnit_FT_IN: { //返回小单位inch double inchNumber = (double)height/pow(10, point); NSInteger ftNumber = inchNumber/12; NSString *inchString = [NSString stringWithFormat:formatStr, (inchNumber-ftNumber*12)]; return [NSString stringWithFormat:@"%zd′%@″", ftNumber, inchString]; } case ELDeviceHeightUnit_M: { double mNumber = (double)height/pow(10, point); NSString *mString = [NSString stringWithFormat:formatStr, mNumber]; return mString; } default: { //不支持的单位 return @""; } } return @""; } @end @implementation HeightBodyFatScaleVC (ble) #pragma mark - ble Delegate ///返回设备连接状态 - (void)heightBodyFatScaleManagerUpdateState:(ELBluetoothState)state { NSLog(@"heightBodyFatScaleManagerUpdateState() state:%@", @(state)); if (state == ELBluetoothStateDidValidationPass) { //获取设备状态 [[ELHeightBodyFatScaleBleManager shareManager] getWorkState]; //设置用户信息 [self sendUserData]; } if (state == ELBluetoothStateScaning) { self.bleStatusLabel.text = @"BLE connecting"; } else if (state == ELBluetoothStateConnectFail) { self.bleStatusLabel.text = @"BLE failed"; } else if (state == ELBluetoothStateDidValidationPass) { self.bleStatusLabel.text = @"BLE connected"; } else if (state == ELBluetoothStateDidDisconnect) { self.bleStatusLabel.text = @"BLE did disconnect"; } } ///回调扫描到的身高体脂秤 - (void)heightBodyFatScaleManagerScanDevices:(NSArray *)scanDevices { for (ELPeripheralModel *model in scanDevices) { if ([model.macAddress isEqualToString:self.per.macAddressString]) { [[ELHeightBodyFatScaleBleManager shareManager] stopScan]; [[ELHeightBodyFatScaleBleManager shareManager] connectPeripheral:model]; } } } ///回调用户信息设置结果 - (void)heightBodyFatScaleManagerSetUserDataResult:(HeightBodyFatScale_SetupResult)result { if (result == HeightBodyFatScale_SetupResult_Failure) { //重新设置 [self sendUserData]; } } ///MCU主动请求用户信息 - (void)heightBodyFatScaleManagerRequestUserData { //设置用户信息 [self sendUserData]; } ///MCU设置单位 - (void)heightBodyFatScaleManagerMCUSetHeightUnit:(ELDeviceHeightUnit)heightUnit weightUnit:(ELDeviceWeightUnit)weightUnit { NSLog(@"weightUnit: %@", @(weightUnit)); NSLog(@"heightUnit: %@", @(heightUnit)); self.weightUnit = weightUnit; self.heightUnit = heightUnit; [self uiSetUnit]; } ///MCU回复单位设置结果 - (void)heightBodyFatScaleManagerReportUnitResult:(HeightBodyFatScale_SetupResult)result { } ///MCU回复工作模式设置结果 - (void)heightBodyFatScaleManagerWorkModeResult:(HeightBodyFatScale_SetupResult)result { } #pragma mark MCU回复设备状态 - (void)heightBodyFatScaleManagerReportDevStateWithWorkMode:(HeightBodyFatScale_WorkModeType)mode power:(NSInteger)power charging:(BOOL)charging weightUnit:(ELDeviceWeightUnit)weightUnit height:(ELDeviceHeightUnit)heightUnit voiceState:(BOOL)voice { self.workMode = mode; self.voiceOn = voice; self.weightUnit = weightUnit; self.heightUnit = heightUnit; [self uiSetUnit]; //防止连接设备后设备已经在测量而导致测量失败的问题,新协议设置模式之后会让设备重新进行测量数据 [[ELHeightBodyFatScaleBleManager shareManager] sendDeviceWorkMode:mode]; NSLog(@"heightBodyFatScaleManagerReportDevStateWithWorkMode() workMode: %@", @(mode)); NSLog(@"heightBodyFatScaleManagerReportDevStateWithWorkMode() power: %@", @(power)); NSLog(@"heightBodyFatScaleManagerReportDevStateWithWorkMode() charging: %@", @(charging)); NSLog(@"heightBodyFatScaleManagerReportDevStateWithWorkMode() weightUnit: %@", @(weightUnit)); NSLog(@"heightBodyFatScaleManagerReportDevStateWithWorkMode() heightUnit: %@", @(heightUnit)); NSLog(@"heightBodyFatScaleManagerReportDevStateWithWorkMode() voiceState: %@", @(voice)); } #pragma mark 身高体脂模式 ///MCU上报称重状态 - (void)heightBodyFatScaleManagerReportWeighingState:(HeightBodyFatScale_WeightStableType)stable weightNumber:(NSInteger)weight weightPoint:(NSInteger)point unit:(ELDeviceWeightUnit)unit { NSString *unitString = AiLinkBleWeightUnitDic[@(unit)]; NSString *dataString = [self.class getWeightStringWithWeight:weight unit:unit point:point]; self.weightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; NSLog(@"heightBodyFatScaleManagerReportWeighingState() stable: %@", @(stable)); NSLog(@"heightBodyFatScaleManagerReportWeighingState() weight: %@", @(weight)); NSLog(@"heightBodyFatScaleManagerReportWeighingState() point: %@", @(point)); NSLog(@"heightBodyFatScaleManagerReportWeighingState() point: %@", @(unit)); } ///MCU上报阻抗数据 - (void)heightBodyFatScaleManagerReportAdcDataWithImpedanceState:(HeightBodyFatScale_AdcStateType)state aisle:(HeightBodyFatScale_AdcAisleType)aisle adc:(NSInteger)adc algorithmId:(NSInteger)algorithmId { if (state == HeightBodyFatScale_AdcStateType_Over) { NSLog(@"adc: %@", @(adc)); NSLog(@"algorithmId: %@", @(algorithmId)); } } ///MCU上报心率数据 - (void)heightBodyFatScaleManagerReportHeartRateDataWithState:(HeightBodyFatScale_HeartRateStateType)state bpm:(NSInteger)bpm { if (state == HeightBodyFatScale_HeartRateStateType_Success) { NSLog(@"HeartRate bpm: %@", @(bpm)); } } ///MCU上报温度数据 - (void)heightBodyFatScaleManagerReportTemDataWithNegative:(BOOL)negative tem:(NSInteger)tem point:(NSInteger)point unit:(ELDeviceTemperatureUnit)unit { NSLog(@"negative: %@", @(negative)); NSLog(@"tem: %@", @(tem)); NSLog(@"point: %@", @(point)); NSLog(@"unit: %@", @(unit)); } ///MCU上报身高数据 - (void)heightBodyFatScaleManagerReportHeightDataWithHeight:(NSInteger)height unit:(ELDeviceHeightUnit)unit point:(NSInteger)point { NSString *unitString = AiLinkBleHeightUnitDic[@(unit)]; NSString *dataString = [self.class getHeightStringWithHeight:height unit:unit point:point]; self.heightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; NSLog(@"height: %@", @(height)); NSLog(@"unit: %@", @(unit)); NSLog(@"point: %@", @(point)); } /// MCU上报体脂数据 - (void)heightBodyFatScaleManagerReportBodyFatDataWithDataModel:(ELHeightBodyFatScaleBleWeightBodyModel *)model { NSLog(@"model: %@", model); self.reportValueLabel.text = [NSString stringWithFormat:@"%@", model]; } #pragma mark 抱婴模式 - (void)heightBodyFatScaleManagerReportBabyModetWithWeighingState:(HeightBodyFatScale_WeightStableType)stable adultWeight:(NSInteger)adultWeight allWeight:(NSInteger)allWeight babyWeight:(NSInteger)babyWeight point:(NSInteger)point unit:(ELDeviceWeightUnit)unit { NSLog(@"workMode: HeightBodyFatScale_WorkModeType_BabyMode"); // self.workMode = HeightBodyFatScale_WorkModeType_BabyMode; // [self uiSetWorkMode:self.workMode]; NSLog(@"stable: %@", @(stable)); NSLog(@"unit: %@", @(unit)); NSLog(@"point: %@", @(point)); NSString *unitString = AiLinkBleWeightUnitDic[@(unit)]; if (babyWeight > 0) { NSLog(@"babyWeight: %@", @(babyWeight)); NSString *dataString = [self.class getWeightStringWithWeight:babyWeight unit:unit point:point]; self.babyWeightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; } else if (allWeight > 0) { NSLog(@"allWeight: %@", @(allWeight)); NSString *dataString = [self.class getWeightStringWithWeight:allWeight unit:unit point:point]; self.babyAdultWeightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; } else { NSLog(@"adultWeight: %@", @(adultWeight)); NSString *dataString = [self.class getWeightStringWithWeight:adultWeight unit:unit point:point]; self.adultWeightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; } if (self.babyModeIndex == 0) { NSLog(@"course_adult, 正在测量成人体重…"); } else if (self.babyModeIndex == 1) { NSLog(@"course_withbaby, 请抱着宝宝重新上秤…"); } if (stable == HeightBodyFatScale_WeightStableType_Stable) { if (self.babyModeIndex == 0) { self.babyModeIndex = 1; NSLog(@"course_withbaby, 请抱着宝宝重新上秤…"); } } if (stable == HeightBodyFatScale_WeightStableType_Stable && babyWeight > 0) { // TODO: save data (babyWeight unit:unit point:point) } } #pragma mark 体重模式 - (void)heightBodyFatScaleManagerReportWeightModeWithWeighingState:(HeightBodyFatScale_WeightStableType)stable weightNumber:(NSInteger)weight point:(NSInteger)point unit:(ELDeviceWeightUnit)unit { NSString *unitString = AiLinkBleWeightUnitDic[@(unit)]; NSString *dataString = [self.class getWeightStringWithWeight:weight unit:unit point:point]; self.weightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; NSLog(@"workMode: HeightBodyFatScale_WorkModeType_WeightMode"); // self.workMode = HeightBodyFatScale_WorkModeType_WeightMode; // [self uiSetWorkMode:self.workMode]; NSLog(@"stable: %@", @(stable)); NSLog(@"weight: %@", @(weight)); NSLog(@"unit: %@", @(unit)); NSLog(@"point: %@", @(point)); if (stable == HeightBodyFatScale_WeightStableType_Stable) { // TODO: save data (weight unit:unit point:point) } } #pragma mark 体重身高模式 //MCU上报体重数据 - (void)heightBodyFatScaleManagerReportWeightHeightModeWithWeighingState:(HeightBodyFatScale_WeightStableType)stable weightNumber:(NSInteger)weight point:(NSInteger)point unit:(ELDeviceWeightUnit)unit { NSString *unitString = AiLinkBleWeightUnitDic[@(unit)]; NSString *dataString = [self.class getWeightStringWithWeight:weight unit:unit point:point]; self.weightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; NSLog(@"workMode: HeightBodyFatScale_WorkModeType_WeightHeightMode"); // self.workMode = HeightBodyFatScale_WorkModeType_WeightHeightMode; // [self uiSetWorkMode:self.workMode]; NSLog(@"stable: %@", @(stable)); NSLog(@"weight: %@", @(weight)); NSLog(@"unit: %@", @(unit)); NSLog(@"point: %@", @(point)); if (stable == HeightBodyFatScale_WeightStableType_Stable) { // TODO: save data (weight unit:unit point:point) } } //MCU上报身高数据 - (void)heightBodyFatScaleManagerReportWeightHeightMOdeWithHeightNumber:(NSInteger)height point:(NSInteger)point unit:(ELDeviceHeightUnit)unit { NSString *unitString = AiLinkBleHeightUnitDic[@(unit)]; NSString *dataString = [self.class getHeightStringWithHeight:height unit:unit point:point]; self.heightValueLabel.text = [NSString stringWithFormat:@"%@ %@", dataString, unitString]; NSLog(@"workMode: HeightBodyFatScale_WorkModeType_WeightHeightMode"); // self.workMode = HeightBodyFatScale_WorkModeType_WeightHeightMode; // [self uiSetWorkMode:self.workMode]; NSLog(@"height: %@", @(height)); NSLog(@"unit: %@", @(unit)); NSLog(@"point: %@", @(point)); } #pragma mark 测量完成 - (void)heightBodyFatScaleManagerSendCompletedWithWorkMode:(HeightBodyFatScale_WorkModeType)type { NSLog(@"heightBodyFatScaleManagerSendCompletedWithWorkMode() WorkMode type: %@", @(type)); } @end