Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //
  2. // ViewController.m
  3. // iFreshDemo
  4. //
  5. // Created by zhang on 16/10/8.
  6. // Copyright © 2016年 taolei. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import <iFreshSDK/iFreshSDK.h>
  10. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,BleReturnValueDelegate>
  11. {
  12. NSIndexPath *selectIndexPath; //Record the currently selected location
  13. NSString *unit;
  14. }
  15. @property (weak, nonatomic) IBOutlet UILabel *unitLabel;
  16. @property (weak, nonatomic) IBOutlet UILabel *bleStatus;
  17. @property (weak, nonatomic) IBOutlet UILabel *weightLabel;
  18. @property (weak, nonatomic) IBOutlet UITableView *unitTabView;
  19. @property (weak, nonatomic) IBOutlet UIButton *zerobutton;
  20. @property (nonatomic,strong) NSArray *unitArray;
  21. @property (nonatomic,assign) NSInteger weightCount;
  22. @end
  23. @implementation ViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.unitArray = @[@"g",@"ml",@"lb",@"oz",@"kg",@"斤"];
  27. [[iFreshSDK shareManager] setbleReturnValueDelegate:self];
  28. [[iFreshSDK shareManager] bleDoScan];
  29. _bleStatus.adjustsFontSizeToFitWidth = YES;
  30. _weightLabel.userInteractionEnabled = NO;
  31. //
  32. UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
  33. button.frame = CGRectMake(20, self.view.frame.size.height - 50, self.view.frame.size.width-40, 40);
  34. [button setTitle:@"Unit setting" forState:(UIControlStateNormal)];
  35. [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  36. button.backgroundColor = [UIColor blueColor];
  37. button.titleLabel.font = [UIFont systemFontOfSize:15];
  38. [self.view addSubview:button];
  39. [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  40. }
  41. -(void)buttonAction:(UIButton *)sender{
  42. UIAlertController * contrl = [UIAlertController alertControllerWithTitle:@"Please choose" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
  43. UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"g" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  44. [[iFreshSDK shareManager] insertTheUnit:(UNIT_g)];
  45. }];
  46. [contrl addAction:action1];
  47. UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"ml" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  48. [[iFreshSDK shareManager] insertTheUnit:(UNIT_ml)];
  49. }];
  50. [contrl addAction:action2];
  51. UIAlertAction * action3 = [UIAlertAction actionWithTitle:@"lb" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  52. [[iFreshSDK shareManager] insertTheUnit:(UNIT_lb)];
  53. }];
  54. [contrl addAction:action3];
  55. UIAlertAction * action4 = [UIAlertAction actionWithTitle:@"oz" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  56. [[iFreshSDK shareManager] insertTheUnit:(UNIT_oz)];
  57. }];
  58. [contrl addAction:action4];
  59. UIAlertAction * action5 = [UIAlertAction actionWithTitle:@"kg" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  60. [[iFreshSDK shareManager] insertTheUnit:(UNIT_kg)];
  61. }];
  62. [contrl addAction:action5];
  63. UIAlertAction * action6 = [UIAlertAction actionWithTitle:@"斤" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  64. [[iFreshSDK shareManager] insertTheUnit:(UNIT_jin)];
  65. }];
  66. [contrl addAction:action6];
  67. UIAlertAction * action7 = [UIAlertAction actionWithTitle:@"Cancel" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
  68. }];
  69. [contrl addAction:action7];
  70. [self presentViewController:contrl animated:YES completion:nil];
  71. }
  72. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  73. [self.view endEditing:YES];
  74. }
  75. #pragma mark ============ 蓝牙回调代理 ==============
  76. -(void)bluetoothManagerReturePenetrateData:(NSData *)data{
  77. NSLog(@"bluetoothManagerReturePenetrateData = %@",data.description);
  78. }
  79. - (void)bleStatusupdate:(GN_BleStatus)bleStatus {
  80. if ( bleStatus == bleOpen || bleStatus == bleBreak) {
  81. _bleStatus.text = @"Not connected";
  82. }else if (bleStatus == bleOff){
  83. _bleStatus.text = @"Not turned on";
  84. }else if (bleStatus == bleConnect){
  85. _bleStatus.text = @"Connected";
  86. }
  87. }
  88. - (void)bleReturnValueModel:(iFreshModel *)model{
  89. //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.
  90. self.weightLabel.text = model.value;
  91. NSLog(@"model.gValue=%ld",(long)model.gValue);
  92. if([_weightLabel.text integerValue] != 0) {
  93. _weightCount = [_weightLabel.text integerValue];
  94. }
  95. _unitLabel.text = model.Unit;
  96. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  97. [[iFreshSDK shareManager] setWeight:_weightCount];
  98. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  99. [[iFreshSDK shareManager] setTotalCalorie:600];
  100. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  101. [[iFreshSDK shareManager] setCalorie:500];
  102. });
  103. });
  104. });
  105. }
  106. - (void)changeUnitWithBle:(GN_UnitEnum)unitChange{
  107. if (unitChange == UNIT_g) {
  108. unit = _unitArray[0];;
  109. _unitLabel.text = _unitArray[0];
  110. }else if (unitChange == UNIT_ml) {
  111. unit = _unitArray[1];
  112. _unitLabel.text = _unitArray[1];
  113. }else if (unitChange == UNIT_lb) {
  114. unit = _unitArray[2];
  115. _unitLabel.text = _unitArray[2];
  116. }else if (unitChange == UNIT_oz) {
  117. unit = _unitArray[3];
  118. _unitLabel.text = _unitArray[3];
  119. }else if (unitChange == UNIT_kg){
  120. unit = _unitArray[4];
  121. _unitLabel.text = _unitArray[4];
  122. }else if (unitChange == UNIT_jin){
  123. unit = _unitArray[5];
  124. _unitLabel.text = _unitArray[5];
  125. }
  126. //Get the current unit here
  127. NSLog(@"+++++++++++++%@",unit);
  128. [[NSUserDefaults standardUserDefaults] setObject:unit forKey:@"unitUnit"];
  129. [_unitTabView reloadData];
  130. }
  131. #pragma mark - UITableViewDelegate
  132. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  133. return 0.001f;
  134. }
  135. #pragma mark - UITableViewDataSource
  136. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  137. {
  138. return 1;
  139. }
  140. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  141. {
  142. return self.unitArray.count;
  143. }
  144. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  145. {
  146. UITableViewCell *cell =[[UITableViewCell alloc] init];
  147. NSString *identifier = @"Identifier";
  148. if (cell == nil) {
  149. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
  150. }
  151. cell.textLabel.text = self.unitArray[indexPath.row];
  152. NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  153. NSString *str =[def objectForKey:@"unitUnit"];
  154. if ([str isEqualToString:cell.textLabel.text]) {
  155. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  156. selectIndexPath = indexPath;
  157. }else{
  158. cell.accessoryType = UITableViewCellAccessoryNone;
  159. }
  160. return cell;
  161. }
  162. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. NSUserDefaults *ud_Unit = [NSUserDefaults standardUserDefaults];
  165. if (indexPath.row == 0) {
  166. _unitLabel.text = _unitArray[0] ;
  167. unit = _unitArray[0];
  168. [[iFreshSDK shareManager] insertTheUnit:UNIT_g];
  169. }else if (indexPath.row == 1) {
  170. _unitLabel.text = _unitArray[1] ;
  171. unit = _unitArray[1];
  172. [[iFreshSDK shareManager] insertTheUnit:UNIT_ml];
  173. }else if (indexPath.row == 2) {
  174. _unitLabel.text = _unitArray[2] ;
  175. unit = _unitArray[2];
  176. [[iFreshSDK shareManager] insertTheUnit:UNIT_lb];
  177. }else if (indexPath.row == 3) {
  178. _unitLabel.text = _unitArray[3] ;
  179. unit = _unitArray[3];
  180. [[iFreshSDK shareManager] insertTheUnit:UNIT_oz];
  181. }else if (indexPath.row == 4) {
  182. _unitLabel.text = _unitArray[4] ;
  183. unit = _unitArray[4];
  184. [[iFreshSDK shareManager] insertTheUnit:UNIT_kg];
  185. }else if (indexPath.row == 5) {
  186. _unitLabel.text = _unitArray[5] ;
  187. unit = _unitArray[5];
  188. [[iFreshSDK shareManager] insertTheUnit:UNIT_jin];
  189. }
  190. [ud_Unit setObject:unit forKey:@"unitUnit"];
  191. [ud_Unit synchronize];
  192. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  193. if (selectIndexPath != indexPath) {
  194. cell.accessoryType = UITableViewCellAccessoryNone;
  195. }else{
  196. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  197. }
  198. [self.unitTabView reloadData];
  199. }
  200. //2018-4-27 Weight setting
  201. - (IBAction)WeightInput_one:(UITextField *)sender {
  202. _WeightTF_one.text = sender.text;
  203. }
  204. - (IBAction)setWeight_one:(UIButton *)sender {
  205. [[iFreshSDK shareManager] setWeight:[_WeightTF_one.text integerValue]];
  206. }
  207. - (IBAction)WeightInput_two:(UITextField *)sender {
  208. _WeightTF_two.text = sender.text;
  209. }
  210. - (IBAction)setWeight_two:(UIButton *)sender {
  211. [[iFreshSDK shareManager] setWeight:[_WeightTF_two.text integerValue]];
  212. }
  213. - (IBAction)WeightInput_three:(UITextField *)sender {
  214. _WeightTF_three.text = sender.text;
  215. }
  216. - (IBAction)SetWeight_three:(id)sender {
  217. [[iFreshSDK shareManager] setWeight:[_WeightTF_three.text integerValue]];
  218. }
  219. - (IBAction)WeightInput_four:(UITextField *)sender {
  220. _WeightTF_four.text = sender.text;
  221. }
  222. - (IBAction)setWeight_four:(id)sender {
  223. [[iFreshSDK shareManager] setWeight:[_WeightTF_four.text integerValue]];
  224. }
  225. - (IBAction)zeroClick:(UIButton *)sender {
  226. [[iFreshSDK shareManager] zeroWriteBle];
  227. }
  228. - (IBAction)breakConnect:(UIButton *)sender {
  229. if ([iFreshSDK shareManager].isBle_Link == YES) {
  230. [[iFreshSDK shareManager] closeBleAndDisconnect];
  231. }else {
  232. return;
  233. }
  234. }
  235. - (IBAction)linkBle:(UIButton *)sender {
  236. if ([iFreshSDK shareManager].isBle_Link == NO) {
  237. [[iFreshSDK shareManager] bleDoScan];
  238. }else {
  239. return;
  240. }
  241. }
  242. - (IBAction)setweight:(UIButton *)sender {
  243. //The analog input is 2000g, and the end return processing is 200g. You can set the weight value as needed. Can't exceed 65535
  244. [[iFreshSDK shareManager] setWeight:[_dataTextFiled.text integerValue]];
  245. }
  246. - (IBAction)setKcal:(UIButton *)sender {
  247. NSLog(@"Setting calories,,,");
  248. [[iFreshSDK shareManager] setCalorie:[_dataTextFiled.text integerValue]];
  249. }
  250. - (IBAction)setTotalKcal:(id)sender {
  251. [[iFreshSDK shareManager] setTotalCalorie:[_dataTextFiled.text integerValue]];
  252. }
  253. - (IBAction)setTotalFat:(id)sender {
  254. [[iFreshSDK shareManager] setTotalFat:[_dataTextFiled.text integerValue]];
  255. }
  256. - (IBAction)setTotalProtein:(id)sender {
  257. [[iFreshSDK shareManager] setTotalProtein:[_dataTextFiled.text integerValue]];
  258. }
  259. - (IBAction)setTotalCarbohydrates:(id)sender {
  260. [[iFreshSDK shareManager] setTotalCarbohydrates:[_dataTextFiled.text integerValue]];
  261. }
  262. - (IBAction)setTotalFiber:(id)sender {
  263. [[iFreshSDK shareManager] setTotalFiber:[_dataTextFiled.text integerValue]];
  264. }
  265. - (IBAction)setTotalCholesterol:(id)sender {
  266. [[iFreshSDK shareManager] setTotalCholesterd:[_dataTextFiled.text integerValue]];
  267. }
  268. - (IBAction)setTotalSodium:(id)sender {
  269. [[iFreshSDK shareManager] setTotalSodium:[_dataTextFiled.text integerValue]];
  270. }
  271. - (IBAction)setTotalSuger:(id)sender {
  272. [[iFreshSDK shareManager] setTotalSugar:[_dataTextFiled.text integerValue]];
  273. }
  274. - (IBAction)setFat:(id)sender {
  275. [[iFreshSDK shareManager] setFat:[_dataTextFiled.text integerValue]];
  276. }
  277. - (IBAction)setProtein:(id)sender {
  278. [[iFreshSDK shareManager] setProtein:[_dataTextFiled.text integerValue]];
  279. }
  280. - (IBAction)setCarbohydrates:(id)sender {
  281. [[iFreshSDK shareManager] setCarbohydrates:[_dataTextFiled.text integerValue]];
  282. }
  283. - (IBAction)setCholesterol:(id)sender {
  284. [[iFreshSDK shareManager] setCholesterd:[_dataTextFiled.text integerValue]];
  285. }
  286. - (IBAction)setFiber:(id)sender {
  287. [[iFreshSDK shareManager] setFiber:[_dataTextFiled.text integerValue]];
  288. }
  289. - (IBAction)setSodium:(id)sender {
  290. [[iFreshSDK shareManager] setSodium:[_dataTextFiled.text integerValue]];
  291. }
  292. - (IBAction)setSuger:(id)sender {
  293. [[iFreshSDK shareManager] setSugar:[_dataTextFiled.text integerValue]];
  294. }
  295. - (IBAction)inputData:(UITextField *)sender {
  296. _dataTextFiled.text = sender.text;
  297. }
  298. - (IBAction)ForWeight:(UITextField *)sender {
  299. _ForWeight_TF.text = sender.text;
  300. }
  301. - (IBAction)ForKcal:(UITextField *)sender {
  302. _ForKcal_TF.text = sender.text;
  303. }
  304. - (IBAction)ForTotalKcal:(UITextField *)sender {
  305. _ForTotalKcal_TF.text = sender.text;
  306. }
  307. - (IBAction)StartSet:(id)sender {
  308. [[iFreshSDK shareManager] zeroWriteBle];
  309. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  310. [[iFreshSDK shareManager] setWeight:[_ForWeight_TF.text floatValue]];
  311. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  312. [[iFreshSDK shareManager] setTotalCalorie:[_ForKcal_TF.text floatValue]];
  313. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  314. [[iFreshSDK shareManager] setCalorie:[_ForTotalKcal_TF.text floatValue]];
  315. });
  316. });
  317. });
  318. }
  319. @end