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.

ELDemoScanVC.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // ELDemoScanVC.m
  3. // AILinkBleSDK_Example
  4. //
  5. // Created by LarryZhang on 2022/12/12.
  6. // Copyright © 2022 zhengzida. All rights reserved.
  7. //
  8. #import "ELDemoScanVC.h"
  9. #import "ELDeviceScanCell.h"
  10. #import "ELDemoDeviceModel.h"
  11. #import <AICareComponentRingBleSDK/ELSmartRingManager.h>
  12. #import <AICareComponentRingBleSDK/NELBleManagerHeader.h>
  13. @interface ELDemoScanVC () <UITableViewDelegate, UITableViewDataSource,ELSmartRingManagerDelegate>
  14. @property (weak, nonatomic) IBOutlet UILabel *bleStatusLabel;
  15. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  16. @property (nonatomic, strong) ELSmartRingManager *smartRingManager;
  17. @property (nonatomic, strong) NSMutableArray<ELAILinkPeripheral *> *peripheralArray;
  18. @end
  19. @implementation ELDemoScanVC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view from its nib.
  23. self.bleStatusLabel.text = @"";
  24. [self initBle];
  25. }
  26. - (void)dealloc {
  27. [self deinitBle];
  28. }
  29. - (IBAction)closeAction:(id)sender {
  30. [self dismissViewControllerAnimated:YES completion:nil];
  31. }
  32. #pragma mark - UITableViewDelegate, UITableViewDataSource
  33. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  34. return 1;
  35. }
  36. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  37. return self.peripheralArray.count;
  38. }
  39. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  40. return 88;
  41. }
  42. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  43. ELDeviceScanCell *cell = [ELDeviceScanCell subsribeCell];
  44. ELAILinkPeripheral *per = self.peripheralArray[indexPath.row];
  45. cell.iconImageView.image = [UIImage imageNamed:self.demoDeviceModel.imageName];
  46. cell.cidValueLabel.text = [NSString stringWithFormat:@"0x%02X", per.cid];
  47. cell.vidValueLabel.text = [NSString stringWithFormat:@"0x%02X", per.vid];
  48. cell.pidValueLabel.text = [NSString stringWithFormat:@"0x%02X", per.pid];
  49. cell.macValueLabel.text = per.macAddressString;
  50. cell.rssiValueLabel.text = [NSString stringWithFormat:@"%@", per.RSSI];
  51. return cell;
  52. }
  53. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  54. ELAILinkPeripheral *per = self.peripheralArray[indexPath.row];
  55. if (self.selectedBlock) {
  56. self.selectedBlock(per);
  57. }
  58. [self dismissViewControllerAnimated:YES completion:nil];
  59. }
  60. #pragma mark - ELAILinkBleManagerDelegate
  61. - (void)initBle {
  62. [ELSmartRingManager sharedManager].delegate = self;
  63. [self scanBle];
  64. // self.bleManager = [[ELAILinkBleManager alloc] init];
  65. // self.bleManager.ailinkDelegate = self;
  66. }
  67. - (void)scanBle {
  68. self.peripheralArray = [NSMutableArray array];
  69. [[ELSmartRingManager sharedManager] scanFilterWithCidArray:self.cids];
  70. }
  71. - (void)deinitBle {
  72. }
  73. -(void)smartRingManager:(ELSmartRingManager *)smartRingManager managerDidUpdateState:(CBCentralManager *)central
  74. {
  75. NSLog(@"%s state:%@", __func__, @(central.state));
  76. if (central.state == CBManagerStatePoweredOn) {
  77. [self scanBle];
  78. } else if (central.state == CBManagerStatePoweredOff) {
  79. }
  80. }
  81. -(void)smartRingManager:(ELSmartRingManager *)smartRingManager managerDidDiscoverPeripheral:(ELAILinkPeripheral *)peripheral
  82. {
  83. NSLog(@"managerDidDiscoverPeripheral cid:%02x vid:%02x pid:%02x mac:%@", peripheral.cid, peripheral.vid, peripheral.pid, peripheral.macAddressString);
  84. for (int i=0; i<self.peripheralArray.count; i++) {
  85. ELAILinkPeripheral *per = self.peripheralArray[i];
  86. if ([per.macData isEqualToData:peripheral.macData]) {
  87. self.peripheralArray[i] = per;
  88. [self.tableView reloadData];
  89. return;
  90. }
  91. }
  92. [self.peripheralArray addObject:peripheral];
  93. [self.peripheralArray sortUsingComparator:^NSComparisonResult(ELAILinkPeripheral * _Nonnull obj1, ELAILinkPeripheral * _Nonnull obj2) {
  94. if (obj1.RSSI.integerValue < obj2.RSSI.integerValue) return NSOrderedDescending;
  95. else if (obj1.RSSI.integerValue > obj2.RSSI.integerValue) return NSOrderedAscending;
  96. return NSOrderedSame;
  97. }];
  98. [self.tableView reloadData];
  99. }
  100. -(void)smartRingManager:(ELSmartRingManager *)smartRingManager ConnectState:(NELBleManagerConnectState)bleConnectState
  101. {
  102. }
  103. @end