iOS AILinkBleSDK - 蓝牙SDK
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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <AILinkBleSDK/ELAILinkBleManager.h>
  10. #import "ELDeviceScanCell.h"
  11. #import "ELDemoDeviceModel.h"
  12. @interface ELDemoScanVC () <UITableViewDelegate, UITableViewDataSource, ELAILinkBleManagerDelegate>
  13. @property (weak, nonatomic) IBOutlet UILabel *bleStatusLabel;
  14. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  15. @property (nonatomic, strong) ELAILinkBleManager *bleManager;
  16. @property (nonatomic, assign) NELBleManagerConnectState bleConnectState;
  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. self.bleManager = [[ELAILinkBleManager alloc] init];
  63. self.bleManager.ailinkDelegate = self;
  64. }
  65. - (void)scanBle {
  66. self.peripheralArray = [NSMutableArray array];
  67. [self.bleManager scanFilterWithCidArray:self.cids];
  68. }
  69. - (void)deinitBle {
  70. [self.bleManager stopScan];
  71. self.bleManager.ailinkDelegate = nil;
  72. [self.bleManager disconnectPeripheral];
  73. }
  74. - (void)updateBleStatusView:(NELBleManagerConnectState)state {
  75. if (self.bleManager.central.state == CBManagerStatePoweredOff) {
  76. self.bleStatusLabel.text = @"Bluetooth: closed";
  77. return;
  78. }
  79. switch (state) {
  80. case NELBleManagerConnectStateDisconnected:
  81. case NELBleManagerConnectStateFailed:
  82. case NELBleManagerConnectStateFailedValidation:
  83. self.bleStatusLabel.text = @"Connect: failed";
  84. break;
  85. case NELBleManagerConnectStateCentralScanning:
  86. self.bleStatusLabel.text = @"Scanning...";
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. // 设备状态变更
  93. - (void)managerDidUpdateState:(CBCentralManager *)central {
  94. NSLog(@"%s state:%@", __func__, @(central.state));
  95. if (central.state == CBManagerStatePoweredOn) {
  96. [self scanBle];
  97. } else if (central.state == CBManagerStatePoweredOff) {
  98. self.bleConnectState = NELBleManagerConnectStateCentralPowerOff;
  99. }
  100. }
  101. - (void)managerScanState:(BOOL)scanning {
  102. NSLog(@"%s scanning:%@", __func__, @(scanning));
  103. if (scanning) {
  104. [self updateBleStatusView:NELBleManagerConnectStateCentralScanning];
  105. }
  106. }
  107. // 扫描到设备
  108. - (void)managerDidDiscoverPeripheral:(ELAILinkPeripheral *)peripheral {
  109. NSLog(@"managerDidDiscoverPeripheral cid:%02x vid:%02x pid:%02x mac:%@", peripheral.cid, peripheral.vid, peripheral.pid, peripheral.macAddressString);
  110. for (int i=0; i<self.peripheralArray.count; i++) {
  111. ELAILinkPeripheral *per = self.peripheralArray[i];
  112. if ([per.macData isEqualToData:peripheral.macData]) {
  113. self.peripheralArray[i] = per;
  114. [self.tableView reloadData];
  115. return;
  116. }
  117. }
  118. [self.peripheralArray addObject:peripheral];
  119. [self.peripheralArray sortUsingComparator:^NSComparisonResult(ELAILinkPeripheral * _Nonnull obj1, ELAILinkPeripheral * _Nonnull obj2) {
  120. if (obj1.RSSI.integerValue < obj2.RSSI.integerValue) return NSOrderedDescending;
  121. else if (obj1.RSSI.integerValue > obj2.RSSI.integerValue) return NSOrderedAscending;
  122. return NSOrderedSame;
  123. }];
  124. [self.tableView reloadData];
  125. }
  126. - (void)managerDidUpdateConnect:(NELBleManagerConnectState)state {
  127. NSLog(@"%s NELBleManagerConnectState:%@", __func__, @(state));
  128. }
  129. //A7数据
  130. - (void)aiLinkBleReceiveA7Data:(NSData *)payload {
  131. NSLog(@"%s #### payload:%@", __func__, payload);
  132. // Byte *bytes = (Byte *)payload.bytes;
  133. // Byte cmd = bytes[0];
  134. }
  135. //A6数据
  136. - (void)aiLinkBleReceiveA6Data:(NSData *)packet {
  137. NSLog(@"%s ##### packet:%@", __func__, packet);
  138. Byte *bytes = (Byte *)packet.bytes;
  139. Byte cmd = bytes[2];
  140. if (cmd == ELInetGetCmdTypeGetBatteryState) {
  141. int power = self.bleManager.battery.power;
  142. ELBatteryChargingState state = self.bleManager.battery.state;
  143. NSLog(@"##### state: %lu power: %d", (unsigned long)state, power);
  144. }
  145. if (cmd == ELInetGetCmdTypeGetBMVersion) {
  146. NSString *bmVersion = self.bleManager.bmVersion;
  147. NSLog(@"##### bmVersion: %@", bmVersion);
  148. }
  149. if (cmd == ELInetGetCmdTypeGetBMVersionPro) {
  150. NSString *bmVersionPro = self.bleManager.bmVersionPro;
  151. NSLog(@"##### bmVersionPro: %@", bmVersionPro);
  152. }
  153. }
  154. @end