AIFit-SDK for ble body fat scale
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SearchDeviceVC.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. {
  101. willAdd = NO;
  102. }
  103. }
  104. if (willAdd) {
  105. [self.peripheralArray addObject:deviceModel];
  106. [self.BleTableView reloadData];
  107. }
  108. self.isAddPeripheraling = NO;
  109. }
  110. #pragma mark - UITableViewDataSource
  111. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  112. {
  113. return self.peripheralArray.count;
  114. }
  115. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AIFitCell"];
  118. if (!cell) {
  119. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AIFitCell"];
  120. }
  121. DeviceModel *peripheralModel = self.peripheralArray[indexPath.row];
  122. cell.textLabel.text = peripheralModel.deviceName;
  123. cell.detailTextLabel.text = peripheralModel.deviceAddress;
  124. return cell;
  125. }
  126. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. DeviceModel *peripheralModel = self.peripheralArray[indexPath.row];
  129. if (_didSelectDeviceBlock) {
  130. _didSelectDeviceBlock(peripheralModel);
  131. //note: just go back, keep ble scan all the time
  132. [self dismissViewControllerAnimated:YES completion:nil];
  133. }
  134. }
  135. #pragma mark - Setter and Getter
  136. - (NSMutableArray *)peripheralArray
  137. {
  138. if (_peripheralArray == nil) {
  139. _peripheralArray = [[NSMutableArray alloc] init];
  140. }
  141. return _peripheralArray;
  142. }
  143. - (void)dealloc
  144. {
  145. NSLog(@"---class:%@ instance:%p already dealloc!",self.class,self);
  146. }
  147. @end