AIFit-SDK for ble body fat scale
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SearchDeviceVC.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <InetBleSDK/InetBleSDK.h>
  9. #import "MainViewController.h"
  10. @interface SearchDeviceVC () <UITableViewDelegate,UITableViewDataSource,INBluetoothManagerDelegate>
  11. {
  12. CGFloat topDis;
  13. }
  14. @property (nonatomic, strong)UITableView *BleTableView;
  15. @property (nonatomic, strong) NSMutableArray *peripheralArray;
  16. @property (nonatomic, assign) BOOL isAddPeripheraling;
  17. @end
  18. @implementation SearchDeviceVC
  19. #pragma mark - ================= 视图 ==========================
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23. self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:250/255.0 blue:250/255.0 alpha:1.0];
  24. [self setupBackButton];
  25. [self setupStartScanBtn];
  26. [self setupStopScanBtn];
  27. [self setupTableView];
  28. if ([INBluetoothManager shareManager].bleState == CBCentralManagerStatePoweredOn) {
  29. [[INBluetoothManager shareManager] closeBleAndDisconnect]; //maybe your scale has been connected
  30. [INBluetoothManager shareManager].delegate = self;
  31. [[INBluetoothManager shareManager] startBleScan];
  32. } else {
  33. NSLog(@"---Error: BLE not avalible, pls check.");
  34. }
  35. }
  36. -(void)setupBackButton
  37. {
  38. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  39. topDis = (UIScreen.mainScreen.bounds.size.height == 812) ? (34+10) : (20+10);
  40. btn.frame = CGRectMake(25, topDis, 250, 40);
  41. [btn setTitle:@"<< BackAndCloseBLE" forState:UIControlStateNormal];
  42. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  43. [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
  44. [self.view addSubview:btn];
  45. [btn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
  46. }
  47. -(void)goBack
  48. {
  49. [[INBluetoothManager shareManager] closeBleAndDisconnect];
  50. __weak typeof(self) weakSelf = self;
  51. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  52. weakSelf.gobackBlock();
  53. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  54. });
  55. }
  56. - (void)setupStartScanBtn
  57. {
  58. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  59. btn.frame = CGRectMake(25, topDis+40+10, 100, 40);
  60. [btn setTitle:@"start scan" forState:UIControlStateNormal];
  61. [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  62. btn.backgroundColor = [UIColor whiteColor];
  63. [self.view addSubview:btn];
  64. [btn addTarget:self action:@selector(scanPeripheral) forControlEvents:UIControlEventTouchUpInside];
  65. }
  66. - (void)setupStopScanBtn
  67. {
  68. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  69. btn.frame = CGRectMake(self.view.bounds.size.width-25-100, topDis+40+10, 100, 40);
  70. [btn setTitle:@"stop scan" forState:UIControlStateNormal];
  71. [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  72. btn.backgroundColor = [UIColor whiteColor];
  73. [self.view addSubview:btn];
  74. [btn addTarget:self action:@selector(stopScan) forControlEvents:UIControlEventTouchUpInside];
  75. }
  76. - (void)setupTableView {
  77. _BleTableView = [[UITableView alloc]init];
  78. _BleTableView.frame = CGRectMake(25, topDis+50+50, self.view.bounds.size.width-50, self.view.bounds.size.height-(topDis+50+50)-20);
  79. _BleTableView.dataSource = self;
  80. _BleTableView.delegate = self;
  81. [self.view addSubview:_BleTableView];
  82. }
  83. - (void)scanPeripheral
  84. {
  85. [[INBluetoothManager shareManager] startBleScan];
  86. }
  87. - (void)stopScan
  88. {
  89. [[INBluetoothManager shareManager] stopBleScan];
  90. }
  91. #pragma mark - BluetoothManagerDelegate
  92. - (void)BluetoothManager:(INBluetoothManager *)manager didDiscoverDevice:(DeviceModel *)deviceModel
  93. {
  94. if (self.isAddPeripheraling == YES) return;
  95. self.isAddPeripheraling = YES;
  96. BOOL willAdd = YES;
  97. for (DeviceModel *model in self.peripheralArray) //avoid add the same device
  98. {
  99. // if ([model.deviceAddress isEqualToString:deviceModel.deviceAddress] && [model.deviceName isEqualToString:deviceModel.deviceName])
  100. if ([model.deviceAddress isEqualToString:deviceModel.deviceAddress])
  101. {
  102. willAdd = NO;
  103. }
  104. }
  105. if (willAdd) {
  106. [self.peripheralArray addObject:deviceModel];
  107. [self.BleTableView reloadData];
  108. }
  109. self.isAddPeripheraling = NO;
  110. }
  111. #pragma mark - UITableViewDataSource
  112. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  113. {
  114. return self.peripheralArray.count;
  115. }
  116. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AIFitCell"];
  119. if (!cell) {
  120. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AIFitCell"];
  121. }
  122. DeviceModel *peripheralModel = self.peripheralArray[indexPath.row];
  123. cell.textLabel.text = peripheralModel.deviceName;
  124. cell.detailTextLabel.text = peripheralModel.deviceAddress;
  125. return cell;
  126. }
  127. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. DeviceModel *peripheralModel = self.peripheralArray[indexPath.row];
  130. if (_didSelectDeviceBlock) {
  131. _didSelectDeviceBlock(peripheralModel);
  132. //note: just go back, keep ble scan all the time
  133. [self dismissViewControllerAnimated:YES completion:nil];
  134. }
  135. }
  136. #pragma mark - Setter and Getter
  137. - (NSMutableArray *)peripheralArray
  138. {
  139. if (_peripheralArray == nil) {
  140. _peripheralArray = [[NSMutableArray alloc] init];
  141. }
  142. return _peripheralArray;
  143. }
  144. - (void)dealloc
  145. {
  146. NSLog(@"---class:%@ instance:%p already dealloc!",self.class,self);
  147. }
  148. @end