// // ViewController.m // iFreshDemo // // Created by zhang on 16/10/8. // Copyright © 2016年 taolei. All rights reserved. // #import "ViewController.h" #import @interface ViewController () { NSIndexPath *selectIndexPath; //Record the currently selected location NSString *unit; } @property (weak, nonatomic) IBOutlet UILabel *unitLabel; @property (weak, nonatomic) IBOutlet UILabel *bleStatus; @property (weak, nonatomic) IBOutlet UILabel *weightLabel; @property (weak, nonatomic) IBOutlet UITableView *unitTabView; @property (weak, nonatomic) IBOutlet UIButton *zerobutton; @property (nonatomic,strong) NSArray *unitArray; @property (nonatomic,assign) NSInteger weightCount; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.unitArray = @[@"g",@"ml",@"lb",@"oz",@"kg",@"斤"]; [[iFreshSDK shareManager] setbleReturnValueDelegate:self]; [[iFreshSDK shareManager] bleDoScan]; _bleStatus.adjustsFontSizeToFitWidth = YES; _weightLabel.userInteractionEnabled = NO; // UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)]; button.frame = CGRectMake(20, self.view.frame.size.height - 50, self.view.frame.size.width-40, 40); [button setTitle:@"Unit setting" forState:(UIControlStateNormal)]; [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)]; button.backgroundColor = [UIColor blueColor]; button.titleLabel.font = [UIFont systemFontOfSize:15]; [self.view addSubview:button]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)]; } -(void)buttonAction:(UIButton *)sender{ UIAlertController * contrl = [UIAlertController alertControllerWithTitle:@"Please choose" message:@"" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"g" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[iFreshSDK shareManager] insertTheUnit:(UNIT_g)]; }]; [contrl addAction:action1]; UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"ml" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[iFreshSDK shareManager] insertTheUnit:(UNIT_ml)]; }]; [contrl addAction:action2]; UIAlertAction * action3 = [UIAlertAction actionWithTitle:@"lb" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[iFreshSDK shareManager] insertTheUnit:(UNIT_lb)]; }]; [contrl addAction:action3]; UIAlertAction * action4 = [UIAlertAction actionWithTitle:@"oz" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[iFreshSDK shareManager] insertTheUnit:(UNIT_oz)]; }]; [contrl addAction:action4]; UIAlertAction * action5 = [UIAlertAction actionWithTitle:@"kg" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[iFreshSDK shareManager] insertTheUnit:(UNIT_kg)]; }]; [contrl addAction:action5]; UIAlertAction * action6 = [UIAlertAction actionWithTitle:@"斤" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { [[iFreshSDK shareManager] insertTheUnit:(UNIT_jin)]; }]; [contrl addAction:action6]; UIAlertAction * action7 = [UIAlertAction actionWithTitle:@"Cancel" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { }]; [contrl addAction:action7]; [self presentViewController:contrl animated:YES completion:nil]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } #pragma mark ============ 蓝牙回调代理 ============== -(void)bluetoothManagerReturePenetrateData:(NSData *)data{ NSLog(@"bluetoothManagerReturePenetrateData = %@",data.description); } - (void)bleStatusupdate:(GN_BleStatus)bleStatus { if ( bleStatus == bleOpen || bleStatus == bleBreak) { _bleStatus.text = @"Not connected"; }else if (bleStatus == bleOff){ _bleStatus.text = @"Not turned on"; }else if (bleStatus == bleConnect){ _bleStatus.text = @"Connected"; } } - (void)bleReturnValueModel:(iFreshModel *)model{ //This code is written by the client beforehand. The app sends a set of data and sends a set of random data for total calories and calories. This code is closed when the data is written. self.weightLabel.text = model.value; NSLog(@"model.gValue=%ld",(long)model.gValue); if([_weightLabel.text integerValue] != 0) { _weightCount = [_weightLabel.text integerValue]; } _unitLabel.text = model.Unit; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[iFreshSDK shareManager] setWeight:_weightCount]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[iFreshSDK shareManager] setTotalCalorie:600]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[iFreshSDK shareManager] setCalorie:500]; }); }); }); } - (void)changeUnitWithBle:(GN_UnitEnum)unitChange{ if (unitChange == UNIT_g) { unit = _unitArray[0];; _unitLabel.text = _unitArray[0]; }else if (unitChange == UNIT_ml) { unit = _unitArray[1]; _unitLabel.text = _unitArray[1]; }else if (unitChange == UNIT_lb) { unit = _unitArray[2]; _unitLabel.text = _unitArray[2]; }else if (unitChange == UNIT_oz) { unit = _unitArray[3]; _unitLabel.text = _unitArray[3]; }else if (unitChange == UNIT_kg){ unit = _unitArray[4]; _unitLabel.text = _unitArray[4]; }else if (unitChange == UNIT_jin){ unit = _unitArray[5]; _unitLabel.text = _unitArray[5]; } //Get the current unit here NSLog(@"+++++++++++++%@",unit); [[NSUserDefaults standardUserDefaults] setObject:unit forKey:@"unitUnit"]; [_unitTabView reloadData]; } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.001f; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.unitArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell =[[UITableViewCell alloc] init]; NSString *identifier = @"Identifier"; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } cell.textLabel.text = self.unitArray[indexPath.row]; NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSString *str =[def objectForKey:@"unitUnit"]; if ([str isEqualToString:cell.textLabel.text]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; selectIndexPath = indexPath; }else{ cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUserDefaults *ud_Unit = [NSUserDefaults standardUserDefaults]; if (indexPath.row == 0) { _unitLabel.text = _unitArray[0] ; unit = _unitArray[0]; [[iFreshSDK shareManager] insertTheUnit:UNIT_g]; }else if (indexPath.row == 1) { _unitLabel.text = _unitArray[1] ; unit = _unitArray[1]; [[iFreshSDK shareManager] insertTheUnit:UNIT_ml]; }else if (indexPath.row == 2) { _unitLabel.text = _unitArray[2] ; unit = _unitArray[2]; [[iFreshSDK shareManager] insertTheUnit:UNIT_lb]; }else if (indexPath.row == 3) { _unitLabel.text = _unitArray[3] ; unit = _unitArray[3]; [[iFreshSDK shareManager] insertTheUnit:UNIT_oz]; }else if (indexPath.row == 4) { _unitLabel.text = _unitArray[4] ; unit = _unitArray[4]; [[iFreshSDK shareManager] insertTheUnit:UNIT_kg]; }else if (indexPath.row == 5) { _unitLabel.text = _unitArray[5] ; unit = _unitArray[5]; [[iFreshSDK shareManager] insertTheUnit:UNIT_jin]; } [ud_Unit setObject:unit forKey:@"unitUnit"]; [ud_Unit synchronize]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (selectIndexPath != indexPath) { cell.accessoryType = UITableViewCellAccessoryNone; }else{ cell.accessoryType = UITableViewCellAccessoryCheckmark; } [self.unitTabView reloadData]; } //2018-4-27 Weight setting - (IBAction)WeightInput_one:(UITextField *)sender { _WeightTF_one.text = sender.text; } - (IBAction)setWeight_one:(UIButton *)sender { [[iFreshSDK shareManager] setWeight:[_WeightTF_one.text integerValue]]; } - (IBAction)WeightInput_two:(UITextField *)sender { _WeightTF_two.text = sender.text; } - (IBAction)setWeight_two:(UIButton *)sender { [[iFreshSDK shareManager] setWeight:[_WeightTF_two.text integerValue]]; } - (IBAction)WeightInput_three:(UITextField *)sender { _WeightTF_three.text = sender.text; } - (IBAction)SetWeight_three:(id)sender { [[iFreshSDK shareManager] setWeight:[_WeightTF_three.text integerValue]]; } - (IBAction)WeightInput_four:(UITextField *)sender { _WeightTF_four.text = sender.text; } - (IBAction)setWeight_four:(id)sender { [[iFreshSDK shareManager] setWeight:[_WeightTF_four.text integerValue]]; } - (IBAction)zeroClick:(UIButton *)sender { [[iFreshSDK shareManager] zeroWriteBle]; } - (IBAction)breakConnect:(UIButton *)sender { if ([iFreshSDK shareManager].isBle_Link == YES) { [[iFreshSDK shareManager] closeBleAndDisconnect]; }else { return; } } - (IBAction)linkBle:(UIButton *)sender { if ([iFreshSDK shareManager].isBle_Link == NO) { [[iFreshSDK shareManager] bleDoScan]; }else { return; } } - (IBAction)setweight:(UIButton *)sender { //The analog input is 2000g, and the end return processing is 200g. You can set the weight value as needed. Can't exceed 65535 [[iFreshSDK shareManager] setWeight:[_dataTextFiled.text integerValue]]; } - (IBAction)setKcal:(UIButton *)sender { NSLog(@"Setting calories,,,"); [[iFreshSDK shareManager] setCalorie:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalKcal:(id)sender { [[iFreshSDK shareManager] setTotalCalorie:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalFat:(id)sender { [[iFreshSDK shareManager] setTotalFat:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalProtein:(id)sender { [[iFreshSDK shareManager] setTotalProtein:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalCarbohydrates:(id)sender { [[iFreshSDK shareManager] setTotalCarbohydrates:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalFiber:(id)sender { [[iFreshSDK shareManager] setTotalFiber:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalCholesterol:(id)sender { [[iFreshSDK shareManager] setTotalCholesterd:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalSodium:(id)sender { [[iFreshSDK shareManager] setTotalSodium:[_dataTextFiled.text integerValue]]; } - (IBAction)setTotalSuger:(id)sender { [[iFreshSDK shareManager] setTotalSugar:[_dataTextFiled.text integerValue]]; } - (IBAction)setFat:(id)sender { [[iFreshSDK shareManager] setFat:[_dataTextFiled.text integerValue]]; } - (IBAction)setProtein:(id)sender { [[iFreshSDK shareManager] setProtein:[_dataTextFiled.text integerValue]]; } - (IBAction)setCarbohydrates:(id)sender { [[iFreshSDK shareManager] setCarbohydrates:[_dataTextFiled.text integerValue]]; } - (IBAction)setCholesterol:(id)sender { [[iFreshSDK shareManager] setCholesterd:[_dataTextFiled.text integerValue]]; } - (IBAction)setFiber:(id)sender { [[iFreshSDK shareManager] setFiber:[_dataTextFiled.text integerValue]]; } - (IBAction)setSodium:(id)sender { [[iFreshSDK shareManager] setSodium:[_dataTextFiled.text integerValue]]; } - (IBAction)setSuger:(id)sender { [[iFreshSDK shareManager] setSugar:[_dataTextFiled.text integerValue]]; } - (IBAction)inputData:(UITextField *)sender { _dataTextFiled.text = sender.text; } - (IBAction)ForWeight:(UITextField *)sender { _ForWeight_TF.text = sender.text; } - (IBAction)ForKcal:(UITextField *)sender { _ForKcal_TF.text = sender.text; } - (IBAction)ForTotalKcal:(UITextField *)sender { _ForTotalKcal_TF.text = sender.text; } - (IBAction)StartSet:(id)sender { [[iFreshSDK shareManager] zeroWriteBle]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[iFreshSDK shareManager] setWeight:[_ForWeight_TF.text floatValue]]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[iFreshSDK shareManager] setTotalCalorie:[_ForKcal_TF.text floatValue]]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[iFreshSDK shareManager] setCalorie:[_ForTotalKcal_TF.text floatValue]]; }); }); }); } @end