Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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; //记录当前选择的位置
  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. _weightLabel.userInteractionEnabled = NO;
  30. //
  31. UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
  32. button.frame = CGRectMake(20, self.view.frame.size.height - 50, self.view.frame.size.width-40, 40);
  33. [button setTitle:@"单位设置" forState:(UIControlStateNormal)];
  34. [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  35. button.backgroundColor = [UIColor colorWithRed:43/255.0 green:142/255.0 blue:192/255.0 alpha:1.0];
  36. button.titleLabel.font = [UIFont systemFontOfSize:15];
  37. [self.view addSubview:button];
  38. [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  39. }
  40. -(void)buttonAction:(UIButton *)sender{
  41. UIAlertController * contrl = [UIAlertController alertControllerWithTitle:@"Please choose" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
  42. UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"g" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  43. [[iFreshSDK shareManager] insertTheUnit:(UNIT_g)];
  44. }];
  45. [contrl addAction:action1];
  46. UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"ml" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  47. [[iFreshSDK shareManager] insertTheUnit:(UNIT_ml)];
  48. }];
  49. [contrl addAction:action2];
  50. UIAlertAction * action3 = [UIAlertAction actionWithTitle:@"lb:oz" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  51. [[iFreshSDK shareManager] insertTheUnit:(UNIT_lb)];
  52. }];
  53. [contrl addAction:action3];
  54. UIAlertAction * action4 = [UIAlertAction actionWithTitle:@"oz" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  55. [[iFreshSDK shareManager] insertTheUnit:(UNIT_oz)];
  56. }];
  57. [contrl addAction:action4];
  58. UIAlertAction * action5 = [UIAlertAction actionWithTitle:@"kg" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  59. [[iFreshSDK shareManager] insertTheUnit:(UNIT_kg)];
  60. }];
  61. [contrl addAction:action5];
  62. UIAlertAction * action6 = [UIAlertAction actionWithTitle:@"斤" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  63. [[iFreshSDK shareManager] insertTheUnit:(UNIT_jin)];
  64. }];
  65. [contrl addAction:action6];
  66. UIAlertAction * actionLB = [UIAlertAction actionWithTitle:@"lb" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  67. [[iFreshSDK shareManager] insertTheUnit:(UNIT_LB)];
  68. }];
  69. [contrl addAction:actionLB];
  70. UIAlertAction * action7 = [UIAlertAction actionWithTitle:@"Cancel" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
  71. }];
  72. [contrl addAction:action7];
  73. [self presentViewController:contrl animated:YES completion:nil];
  74. }
  75. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  76. [self.view endEditing:YES];
  77. }
  78. -(void)bluetoothManagerReturePenetrateData:(NSData *)data{
  79. NSLog(@"bluetoothManagerReturePenetrateData = %@",data.description);
  80. }
  81. - (void)bleStatusupdate:(GN_BleStatus)bleStatus {
  82. if ( bleStatus == bleOpen || bleStatus == bleBreak) {
  83. _bleStatus.text = @"蓝牙未连接";
  84. }else if (bleStatus == bleOff){
  85. _bleStatus.text = @"蓝牙未打开";
  86. }else if (bleStatus == bleConnect){
  87. _bleStatus.text = @"蓝牙已连接";
  88. }
  89. }
  90. - (void)bleReturnValueModel:(iFreshModel *)model{
  91. //这段代码是之前客户要求写入的——app接收到一组数据就发送总卡路里和卡路里的一组随机数据,在执行写入数据时要把这段代码关闭,
  92. self.weightLabel.text = model.value;
  93. NSLog(@"model.gValue = %ld",(long)model.gValue);//gValue为以“g”为单位的数据
  94. NSLog(@"value == %@",model.value);
  95. if([_weightLabel.text integerValue] != 0) {
  96. _weightCount = [_weightLabel.text integerValue];
  97. //[[iFreshSDK shareManager] zeroWriteBle]; //删除这句,否则秤一直归0
  98. }
  99. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  100. [[iFreshSDK shareManager] setWeight:_weightCount];
  101. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  102. [[iFreshSDK shareManager] setTotalCalorie:600];
  103. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  104. [[iFreshSDK shareManager] setCalorie:500];
  105. });
  106. });
  107. });
  108. }
  109. - (void)changeUnitWithBle:(GN_UnitEnum)unitChange{
  110. NSLog(@"unitChange == %ld",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. //在此获得当前称的单位
  134. NSLog(@"+++++++++++++%@",unit);
  135. [[NSUserDefaults standardUserDefaults] setObject:unit forKey:@"unitUnit"];
  136. [_unitTabView reloadData];
  137. }
  138. - (void)bleReturnDeviceType:(AiFrushDeviceType)deviceType{
  139. NSLog(@"接收到的设备类型:%lu",(unsigned long)deviceType);
  140. }
  141. #pragma mark - UITableViewDelegate
  142. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  143. return 0.001f;
  144. }
  145. #pragma mark - UITableViewDataSource
  146. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  147. {
  148. return 1;
  149. }
  150. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  151. {
  152. return self.unitArray.count;
  153. }
  154. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. UITableViewCell *cell =[[UITableViewCell alloc] init];
  157. NSString *identifier = @"Identifier";
  158. if (cell == nil) {
  159. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
  160. }
  161. cell.textLabel.text = self.unitArray[indexPath.row];
  162. NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  163. NSString *str =[def objectForKey:@"unitUnit"];
  164. if ([str isEqualToString:cell.textLabel.text]) {
  165. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  166. selectIndexPath = indexPath;
  167. }else{
  168. cell.accessoryType = UITableViewCellAccessoryNone;
  169. }
  170. return cell;
  171. }
  172. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. NSUserDefaults *ud_Unit = [NSUserDefaults standardUserDefaults];
  175. if (indexPath.row == 0) {
  176. _unitLabel.text = _unitArray[0] ;
  177. unit = _unitArray[0];
  178. [[iFreshSDK shareManager] insertTheUnit:UNIT_g];
  179. }else if (indexPath.row == 1) {
  180. _unitLabel.text = _unitArray[1] ;
  181. unit = _unitArray[1];
  182. [[iFreshSDK shareManager] insertTheUnit:UNIT_ml];
  183. }else if (indexPath.row == 2) {
  184. _unitLabel.text = _unitArray[2] ;
  185. unit = _unitArray[2];
  186. [[iFreshSDK shareManager] insertTheUnit:UNIT_lb];
  187. }else if (indexPath.row == 3) {
  188. _unitLabel.text = _unitArray[3] ;
  189. unit = _unitArray[3];
  190. [[iFreshSDK shareManager] insertTheUnit:UNIT_oz];
  191. }else if (indexPath.row == 4) {
  192. _unitLabel.text = _unitArray[4] ;
  193. unit = _unitArray[4];
  194. [[iFreshSDK shareManager] insertTheUnit:UNIT_kg];
  195. }else if (indexPath.row == 5) {
  196. _unitLabel.text = _unitArray[5] ;
  197. unit = _unitArray[5];
  198. [[iFreshSDK shareManager] insertTheUnit:UNIT_jin];
  199. }else if (indexPath.row == 6) {
  200. _unitLabel.text = _unitArray[6] ;
  201. unit = _unitArray[6];
  202. [[iFreshSDK shareManager] insertTheUnit:UNIT_LB];
  203. }
  204. [ud_Unit setObject:unit forKey:@"unitUnit"];
  205. [ud_Unit synchronize];
  206. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  207. if (selectIndexPath != indexPath) {
  208. cell.accessoryType = UITableViewCellAccessoryNone;
  209. }else{
  210. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  211. }
  212. [self.unitTabView reloadData];
  213. }
  214. //2018-4-27 重量设置
  215. - (IBAction)WeightInput_one:(UITextField *)sender {
  216. _WeightTF_one.text = sender.text;
  217. }
  218. - (IBAction)setWeight_one:(UIButton *)sender {
  219. [[iFreshSDK shareManager] setWeight:[_WeightTF_one.text integerValue]];
  220. }
  221. - (IBAction)WeightInput_two:(UITextField *)sender {
  222. _WeightTF_two.text = sender.text;
  223. }
  224. - (IBAction)setWeight_two:(UIButton *)sender {
  225. [[iFreshSDK shareManager] setWeight:[_WeightTF_two.text integerValue]];
  226. }
  227. - (IBAction)WeightInput_three:(UITextField *)sender {
  228. _WeightTF_three.text = sender.text;
  229. }
  230. - (IBAction)SetWeight_three:(id)sender {
  231. [[iFreshSDK shareManager] setWeight:[_WeightTF_three.text integerValue]];
  232. }
  233. - (IBAction)WeightInput_four:(UITextField *)sender {
  234. _WeightTF_four.text = sender.text;
  235. }
  236. - (IBAction)setWeight_four:(id)sender {
  237. [[iFreshSDK shareManager] setWeight:[_WeightTF_four.text integerValue]];
  238. }
  239. - (IBAction)zeroClick:(UIButton *)sender {
  240. [[iFreshSDK shareManager] zeroWriteBle];
  241. }
  242. - (IBAction)breakConnect:(UIButton *)sender {
  243. if ([iFreshSDK shareManager].isBle_Link == YES) {
  244. [[iFreshSDK shareManager] bleDisconnect];
  245. }else {
  246. return;
  247. }
  248. }
  249. - (IBAction)linkBle:(UIButton *)sender {
  250. if ([iFreshSDK shareManager].isBle_Link == NO) {
  251. [[iFreshSDK shareManager] bleDoScan];
  252. }else {
  253. return;
  254. }
  255. }
  256. - (IBAction)setweight:(UIButton *)sender {
  257. //模拟输入2000g,称端返回处理为200g,可根据需要自己设置重量值。不能超过65535
  258. [[iFreshSDK shareManager] setWeight:[_dataTextFiled.text integerValue]];
  259. }
  260. - (IBAction)setKcal:(UIButton *)sender {
  261. NSLog(@"设置卡路里,,,");
  262. [[iFreshSDK shareManager] setCalorie:[_dataTextFiled.text integerValue]];
  263. }
  264. - (IBAction)setTotalKcal:(id)sender {
  265. [[iFreshSDK shareManager] setTotalCalorie:[_dataTextFiled.text integerValue]];
  266. }
  267. - (IBAction)setTotalFat:(id)sender {
  268. [[iFreshSDK shareManager] setTotalFat:[_dataTextFiled.text integerValue]];
  269. }
  270. - (IBAction)setTotalProtein:(id)sender {
  271. [[iFreshSDK shareManager] setTotalProtein:[_dataTextFiled.text integerValue]];
  272. }
  273. - (IBAction)setTotalCarbohydrates:(id)sender {
  274. [[iFreshSDK shareManager] setTotalCarbohydrates:[_dataTextFiled.text integerValue]];
  275. }
  276. - (IBAction)setTotalFiber:(id)sender {
  277. [[iFreshSDK shareManager] setTotalFiber:[_dataTextFiled.text integerValue]];
  278. }
  279. - (IBAction)setTotalCholesterol:(id)sender {
  280. [[iFreshSDK shareManager] setTotalCholesterd:[_dataTextFiled.text integerValue]];
  281. }
  282. - (IBAction)setTotalSodium:(id)sender {
  283. [[iFreshSDK shareManager] setTotalSodium:[_dataTextFiled.text integerValue]];
  284. }
  285. - (IBAction)setTotalSuger:(id)sender {
  286. [[iFreshSDK shareManager] setTotalSugar:[_dataTextFiled.text integerValue]];
  287. }
  288. - (IBAction)setFat:(id)sender {
  289. [[iFreshSDK shareManager] setFat:[_dataTextFiled.text integerValue]];
  290. }
  291. - (IBAction)setProtein:(id)sender {
  292. [[iFreshSDK shareManager] setProtein:[_dataTextFiled.text integerValue]];
  293. }
  294. - (IBAction)setCarbohydrates:(id)sender {
  295. [[iFreshSDK shareManager] setCarbohydrates:[_dataTextFiled.text integerValue]];
  296. }
  297. - (IBAction)setCholesterol:(id)sender {
  298. [[iFreshSDK shareManager] setCholesterd:[_dataTextFiled.text integerValue]];
  299. }
  300. - (IBAction)setFiber:(id)sender {
  301. [[iFreshSDK shareManager] setFiber:[_dataTextFiled.text integerValue]];
  302. }
  303. - (IBAction)setSodium:(id)sender {
  304. [[iFreshSDK shareManager] setSodium:[_dataTextFiled.text integerValue]];
  305. }
  306. - (IBAction)setSuger:(id)sender {
  307. [[iFreshSDK shareManager] setSugar:[_dataTextFiled.text integerValue]];
  308. }
  309. - (IBAction)inputData:(UITextField *)sender {
  310. _dataTextFiled.text = sender.text;
  311. }
  312. - (IBAction)ForWeight:(UITextField *)sender {
  313. _ForWeight_TF.text = sender.text;
  314. }
  315. - (IBAction)ForKcal:(UITextField *)sender {
  316. _ForKcal_TF.text = sender.text;
  317. }
  318. - (IBAction)ForTotalKcal:(UITextField *)sender {
  319. _ForTotalKcal_TF.text = sender.text;
  320. }
  321. - (IBAction)StartSet:(id)sender {
  322. [[iFreshSDK shareManager] zeroWriteBle];
  323. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  324. [[iFreshSDK shareManager] setWeight:[_ForWeight_TF.text floatValue]];
  325. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  326. [[iFreshSDK shareManager] setTotalCalorie:[_ForKcal_TF.text floatValue]];
  327. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  328. [[iFreshSDK shareManager] setCalorie:[_ForTotalKcal_TF.text floatValue]];
  329. });
  330. });
  331. });
  332. }
  333. @end