123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- //
- // ViewController.m
- // iFreshDemo
- //
- // Created by zhang on 16/10/8.
- // Copyright © 2016年 taolei. All rights reserved.
- //
-
- #import "ViewController.h"
- #import <iFreshSDK/iFreshSDK.h>
-
-
- @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,BleReturnValueDelegate>
- {
- 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<UITouch *> *)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
|