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.

SearchDeviceVC.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // AIFit-Demo
  3. //
  4. // Created by iot_wz on 2018/9/1.
  5. // Copyright © 2018年 iot_wz. All rights reserved.
  6. //
  7. #import "SearchDeviceVC.h"
  8. #import <iFreshSDK/iFreshSDK.h>
  9. @interface SearchDeviceVC () <UITableViewDelegate,UITableViewDataSource,BleReturnValueDelegate>
  10. {
  11. CGFloat topDis;
  12. }
  13. @property (nonatomic, strong)UITableView *BleTableView;
  14. @property (nonatomic, strong) NSMutableArray *peripheralArray;
  15. @property (nonatomic, assign) BOOL isAddPeripheraling;
  16. @end
  17. @implementation SearchDeviceVC
  18. #pragma mark - ================= 视图 ==========================
  19. - (void)viewDidLoad
  20. {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:250/255.0 blue:250/255.0 alpha:1.0];
  23. [self setupBackButton];
  24. [self setupStartScanBtn];
  25. [self setupStopScanBtn];
  26. [self setupTableView];
  27. [[iFreshSDK shareManager] bleDisconnect];
  28. [[iFreshSDK shareManager] setbleReturnValueDelegate:self];
  29. [[iFreshSDK shareManager] bleDoScan];
  30. }
  31. - (void)bleStatusupdate:(GN_BleStatus)bleStatus {
  32. if ( bleStatus == bleOpen || bleStatus == bleBreak) {
  33. NSLog(@"蓝牙未连接");
  34. }else if (bleStatus == bleOff){
  35. NSLog(@"蓝牙未打开");
  36. }else if (bleStatus == bleConnect){
  37. NSLog(@"蓝牙已连接");
  38. }
  39. }
  40. -(void)setupBackButton
  41. {
  42. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  43. topDis = (UIScreen.mainScreen.bounds.size.height == 812) ? (34+10) : (20+10);
  44. btn.frame = CGRectMake(25, topDis, 250, 40);
  45. [btn setTitle:@"<< BackAndCloseBLE" forState:UIControlStateNormal];
  46. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  47. [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
  48. [self.view addSubview:btn];
  49. [btn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
  50. }
  51. -(void)goBack
  52. {
  53. [[iFreshSDK shareManager] bleDisconnect];
  54. if (self.gobackBlock) {
  55. self.gobackBlock();
  56. }
  57. [self dismissViewControllerAnimated:YES completion:nil];
  58. }
  59. - (void)setupStartScanBtn
  60. {
  61. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  62. btn.frame = CGRectMake(25, topDis+40+10, 100, 40);
  63. [btn setTitle:@"start scan" forState:UIControlStateNormal];
  64. [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  65. btn.backgroundColor = [UIColor whiteColor];
  66. [self.view addSubview:btn];
  67. [btn addTarget:self action:@selector(scanPeripheral) forControlEvents:UIControlEventTouchUpInside];
  68. }
  69. - (void)setupStopScanBtn
  70. {
  71. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  72. btn.frame = CGRectMake(self.view.bounds.size.width-25-100, topDis+40+10, 100, 40);
  73. [btn setTitle:@"stop scan" forState:UIControlStateNormal];
  74. [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  75. btn.backgroundColor = [UIColor whiteColor];
  76. [self.view addSubview:btn];
  77. [btn addTarget:self action:@selector(stopScan) forControlEvents:UIControlEventTouchUpInside];
  78. }
  79. - (void)setupTableView {
  80. _BleTableView = [[UITableView alloc]init];
  81. _BleTableView.frame = CGRectMake(25, topDis+50+50, self.view.bounds.size.width-50, self.view.bounds.size.height-(topDis+50+50)-20);
  82. _BleTableView.dataSource = self;
  83. _BleTableView.delegate = self;
  84. [self.view addSubview:_BleTableView];
  85. }
  86. - (void)scanPeripheral
  87. {
  88. [[iFreshSDK shareManager] bleDoScan];
  89. }
  90. - (void)stopScan
  91. {
  92. [[iFreshSDK shareManager] bleStopScan];
  93. }
  94. #pragma mark - BluetoothManagerDelegate
  95. /**
  96. *Callback Bluetooth scanned devices(回调扫描到的设备)
  97. */
  98. - (void)bleReturnScannedDevice:(iFreshDevice *)device
  99. {
  100. if (self.isAddPeripheraling == YES) return;
  101. self.isAddPeripheraling = YES;
  102. BOOL willAdd = YES;
  103. for (iFreshDevice *model in self.peripheralArray) //avoid add the same device
  104. {
  105. if ([model.mac isEqualToString:device.mac])
  106. {
  107. willAdd = NO;
  108. }
  109. }
  110. if (willAdd) {
  111. [self.peripheralArray addObject:device];
  112. [self.BleTableView reloadData];
  113. }
  114. self.isAddPeripheraling = NO;
  115. }
  116. #pragma mark - UITableViewDataSource
  117. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  118. {
  119. return self.peripheralArray.count;
  120. }
  121. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AIFitCell"];
  124. if (!cell) {
  125. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AIFitCell"];
  126. }
  127. iFreshDevice *device = self.peripheralArray[indexPath.row];
  128. cell.textLabel.text = device.peripheral.name;
  129. cell.detailTextLabel.text = device.mac;
  130. return cell;
  131. }
  132. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  133. {
  134. [[iFreshSDK shareManager] bleStopScan];
  135. iFreshDevice *device = self.peripheralArray[indexPath.row];
  136. if (_didSelectDeviceBlock) {
  137. _didSelectDeviceBlock(device);
  138. [self dismissViewControllerAnimated:YES completion:nil];
  139. }
  140. }
  141. #pragma mark - Setter and Getter
  142. - (NSMutableArray *)peripheralArray
  143. {
  144. if (_peripheralArray == nil) {
  145. _peripheralArray = [[NSMutableArray alloc] init];
  146. }
  147. return _peripheralArray;
  148. }
  149. - (void)dealloc
  150. {
  151. NSLog(@"---class:%@ instance:%p already dealloc!",self.class,self);
  152. }
  153. @end