Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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",@"oz",@"kg",@"斤",@"lb"];
  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:oz" 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 * actionLB = [UIAlertAction actionWithTitle:@"lb" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  68. [[iFreshSDK shareManager] insertTheUnit:(UNIT_LB)];
  69. }];
  70. [contrl addAction:actionLB];
  71. UIAlertAction * action7 = [UIAlertAction actionWithTitle:@"Cancel" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
  72. }];
  73. [contrl addAction:action7];
  74. [self presentViewController:contrl animated:YES completion:nil];
  75. }
  76. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  77. [self.view endEditing:YES];
  78. }
  79. #pragma mark ============ 蓝牙回调代理 ==============
  80. -(void)bluetoothManagerReturePenetrateData:(NSData *)data{
  81. NSLog(@"bluetoothManagerReturePenetrateData = %@",data.description);
  82. }
  83. - (void)bleStatusupdate:(GN_BleStatus)bleStatus {
  84. if ( bleStatus == bleOpen || bleStatus == bleBreak) {
  85. _bleStatus.text = @"Not connected";
  86. }else if (bleStatus == bleOff){
  87. _bleStatus.text = @"Not turned on";
  88. }else if (bleStatus == bleConnect){
  89. _bleStatus.text = @"Connected";
  90. }
  91. }
  92. - (void)bleReturnValueModel:(iFreshModel *)model{
  93. //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.
  94. self.weightLabel.text = model.value;
  95. NSLog(@"model.gValue=%ld",(long)model.gValue);
  96. if([_weightLabel.text integerValue] != 0) {
  97. _weightCount = [_weightLabel.text integerValue];
  98. }
  99. _unitLabel.text = model.Unit;
  100. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  101. [[iFreshSDK shareManager] setWeight:_weightCount];
  102. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  103. [[iFreshSDK shareManager] setTotalCalorie:600];
  104. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  105. [[iFreshSDK shareManager] setCalorie:500];
  106. });
  107. });
  108. });
  109. }
  110. - (void)changeUnitWithBle:(GN_UnitEnum)unitChange{
  111. if (unitChange == UNIT_g) {
  112. unit = _unitArray[0];;
  113. _unitLabel.text = _unitArray[0];
  114. }else if (unitChange == UNIT_ml) {
  115. unit = _unitArray[1];
  116. _unitLabel.text = _unitArray[1];
  117. }else if (unitChange == UNIT_lb) {
  118. unit = _unitArray[2];
  119. _unitLabel.text = _unitArray[2];
  120. }else if (unitChange == UNIT_oz) {
  121. unit = _unitArray[3];
  122. _unitLabel.text = _unitArray[3];
  123. }else if (unitChange == UNIT_kg){
  124. unit = _unitArray[4];
  125. _unitLabel.text = _unitArray[4];
  126. }else if (unitChange == UNIT_jin){
  127. unit = _unitArray[5];
  128. _unitLabel.text = _unitArray[5];
  129. }else if (unitChange == UNIT_LB){
  130. unit = _unitArray[6];
  131. _unitLabel.text = _unitArray[6];
  132. }
  133. //Get the current unit here
  134. NSLog(@"+++++++++++++%@",unit);
  135. [[NSUserDefaults standardUserDefaults] setObject:unit forKey:@"unitUnit"];
  136. [_unitTabView reloadData];
  137. }
  138. #pragma mark - UITableViewDelegate
  139. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  140. return 0.001f;
  141. }
  142. #pragma mark - UITableViewDataSource
  143. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  144. {
  145. return 1;
  146. }
  147. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  148. {
  149. return self.unitArray.count;
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. UITableViewCell *cell =[[UITableViewCell alloc] init];
  154. NSString *identifier = @"Identifier";
  155. if (cell == nil) {
  156. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
  157. }
  158. cell.textLabel.text = self.unitArray[indexPath.row];
  159. NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  160. NSString *str =[def objectForKey:@"unitUnit"];
  161. if ([str isEqualToString:cell.textLabel.text]) {
  162. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  163. selectIndexPath = indexPath;
  164. }else{
  165. cell.accessoryType = UITableViewCellAccessoryNone;
  166. }
  167. return cell;
  168. }
  169. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  170. {
  171. NSUserDefaults *ud_Unit = [NSUserDefaults standardUserDefaults];
  172. if (indexPath.row == 0) {
  173. _unitLabel.text = _unitArray[0] ;
  174. unit = _unitArray[0];
  175. [[iFreshSDK shareManager] insertTheUnit:UNIT_g];
  176. }else if (indexPath.row == 1) {
  177. _unitLabel.text = _unitArray[1] ;
  178. unit = _unitArray[1];
  179. [[iFreshSDK shareManager] insertTheUnit:UNIT_ml];
  180. }else if (indexPath.row == 2) {
  181. _unitLabel.text = _unitArray[2] ;
  182. unit = _unitArray[2];
  183. [[iFreshSDK shareManager] insertTheUnit:UNIT_lb];
  184. }else if (indexPath.row == 3) {
  185. _unitLabel.text = _unitArray[3] ;
  186. unit = _unitArray[3];
  187. [[iFreshSDK shareManager] insertTheUnit:UNIT_oz];
  188. }else if (indexPath.row == 4) {
  189. _unitLabel.text = _unitArray[4] ;
  190. unit = _unitArray[4];
  191. [[iFreshSDK shareManager] insertTheUnit:UNIT_kg];
  192. }else if (indexPath.row == 5) {
  193. _unitLabel.text = _unitArray[5] ;
  194. unit = _unitArray[5];
  195. [[iFreshSDK shareManager] insertTheUnit:UNIT_jin];
  196. }else if (indexPath.row == 6) {
  197. _unitLabel.text = _unitArray[6] ;
  198. unit = _unitArray[6];
  199. [[iFreshSDK shareManager] insertTheUnit:UNIT_LB];
  200. }
  201. [ud_Unit setObject:unit forKey:@"unitUnit"];
  202. [ud_Unit synchronize];
  203. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  204. if (selectIndexPath != indexPath) {
  205. cell.accessoryType = UITableViewCellAccessoryNone;
  206. }else{
  207. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  208. }
  209. [self.unitTabView reloadData];
  210. }
  211. //2018-4-27 Weight setting
  212. - (IBAction)WeightInput_one:(UITextField *)sender {
  213. _WeightTF_one.text = sender.text;
  214. }
  215. - (IBAction)setWeight_one:(UIButton *)sender {
  216. [[iFreshSDK shareManager] setWeight:[_WeightTF_one.text integerValue]];
  217. }
  218. - (IBAction)WeightInput_two:(UITextField *)sender {
  219. _WeightTF_two.text = sender.text;
  220. }
  221. - (IBAction)setWeight_two:(UIButton *)sender {
  222. [[iFreshSDK shareManager] setWeight:[_WeightTF_two.text integerValue]];
  223. }
  224. - (IBAction)WeightInput_three:(UITextField *)sender {
  225. _WeightTF_three.text = sender.text;
  226. }
  227. - (IBAction)SetWeight_three:(id)sender {
  228. [[iFreshSDK shareManager] setWeight:[_WeightTF_three.text integerValue]];
  229. }
  230. - (IBAction)WeightInput_four:(UITextField *)sender {
  231. _WeightTF_four.text = sender.text;
  232. }
  233. - (IBAction)setWeight_four:(id)sender {
  234. [[iFreshSDK shareManager] setWeight:[_WeightTF_four.text integerValue]];
  235. }
  236. - (IBAction)zeroClick:(UIButton *)sender {
  237. [[iFreshSDK shareManager] zeroWriteBle];
  238. }
  239. - (IBAction)breakConnect:(UIButton *)sender {
  240. if ([iFreshSDK shareManager].isBle_Link == YES) {
  241. [[iFreshSDK shareManager] closeBleAndDisconnect];
  242. }else {
  243. return;
  244. }
  245. }
  246. - (IBAction)linkBle:(UIButton *)sender {
  247. if ([iFreshSDK shareManager].isBle_Link == NO) {
  248. [[iFreshSDK shareManager] bleDoScan];
  249. }else {
  250. return;
  251. }
  252. }
  253. - (IBAction)setweight:(UIButton *)sender {
  254. //The analog input is 2000g, and the end return processing is 200g. You can set the weight value as needed. Can't exceed 65535
  255. [[iFreshSDK shareManager] setWeight:[_dataTextFiled.text integerValue]];
  256. }
  257. - (IBAction)setKcal:(UIButton *)sender {
  258. NSLog(@"Setting calories,,,");
  259. [[iFreshSDK shareManager] setCalorie:[_dataTextFiled.text integerValue]];
  260. }
  261. - (IBAction)setTotalKcal:(id)sender {
  262. [[iFreshSDK shareManager] setTotalCalorie:[_dataTextFiled.text integerValue]];
  263. }
  264. - (IBAction)setTotalFat:(id)sender {
  265. [[iFreshSDK shareManager] setTotalFat:[_dataTextFiled.text integerValue]];
  266. }
  267. - (IBAction)setTotalProtein:(id)sender {
  268. [[iFreshSDK shareManager] setTotalProtein:[_dataTextFiled.text integerValue]];
  269. }
  270. - (IBAction)setTotalCarbohydrates:(id)sender {
  271. [[iFreshSDK shareManager] setTotalCarbohydrates:[_dataTextFiled.text integerValue]];
  272. }
  273. - (IBAction)setTotalFiber:(id)sender {
  274. [[iFreshSDK shareManager] setTotalFiber:[_dataTextFiled.text integerValue]];
  275. }
  276. - (IBAction)setTotalCholesterol:(id)sender {
  277. [[iFreshSDK shareManager] setTotalCholesterd:[_dataTextFiled.text integerValue]];
  278. }
  279. - (IBAction)setTotalSodium:(id)sender {
  280. [[iFreshSDK shareManager] setTotalSodium:[_dataTextFiled.text integerValue]];
  281. }
  282. - (IBAction)setTotalSuger:(id)sender {
  283. [[iFreshSDK shareManager] setTotalSugar:[_dataTextFiled.text integerValue]];
  284. }
  285. - (IBAction)setFat:(id)sender {
  286. [[iFreshSDK shareManager] setFat:[_dataTextFiled.text integerValue]];
  287. }
  288. - (IBAction)setProtein:(id)sender {
  289. [[iFreshSDK shareManager] setProtein:[_dataTextFiled.text integerValue]];
  290. }
  291. - (IBAction)setCarbohydrates:(id)sender {
  292. [[iFreshSDK shareManager] setCarbohydrates:[_dataTextFiled.text integerValue]];
  293. }
  294. - (IBAction)setCholesterol:(id)sender {
  295. [[iFreshSDK shareManager] setCholesterd:[_dataTextFiled.text integerValue]];
  296. }
  297. - (IBAction)setFiber:(id)sender {
  298. [[iFreshSDK shareManager] setFiber:[_dataTextFiled.text integerValue]];
  299. }
  300. - (IBAction)setSodium:(id)sender {
  301. [[iFreshSDK shareManager] setSodium:[_dataTextFiled.text integerValue]];
  302. }
  303. - (IBAction)setSuger:(id)sender {
  304. [[iFreshSDK shareManager] setSugar:[_dataTextFiled.text integerValue]];
  305. }
  306. - (IBAction)inputData:(UITextField *)sender {
  307. _dataTextFiled.text = sender.text;
  308. }
  309. - (IBAction)ForWeight:(UITextField *)sender {
  310. _ForWeight_TF.text = sender.text;
  311. }
  312. - (IBAction)ForKcal:(UITextField *)sender {
  313. _ForKcal_TF.text = sender.text;
  314. }
  315. - (IBAction)ForTotalKcal:(UITextField *)sender {
  316. _ForTotalKcal_TF.text = sender.text;
  317. }
  318. - (IBAction)StartSet:(id)sender {
  319. [[iFreshSDK shareManager] zeroWriteBle];
  320. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  321. [[iFreshSDK shareManager] setWeight:[_ForWeight_TF.text floatValue]];
  322. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  323. [[iFreshSDK shareManager] setTotalCalorie:[_ForKcal_TF.text floatValue]];
  324. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  325. [[iFreshSDK shareManager] setCalorie:[_ForTotalKcal_TF.text floatValue]];
  326. });
  327. });
  328. });
  329. }
  330. @end