AIFit-SDK for ble body fat scale
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchDeviceVC.m 5.6KB

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