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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. @interface ELDemoScanVC () <UITableViewDelegate, UITableViewDataSource, ELAILinkBleManagerDelegate>
  12. @property (weak, nonatomic) IBOutlet UILabel *bleStatusLabel;
  13. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  14. @property (nonatomic, strong) ELAILinkBleManager *bleManager;
  15. @property (nonatomic, assign) NELBleManagerConnectState bleConnectState;
  16. @property (nonatomic, strong) NSMutableArray<ELAILinkPeripheral *> *peripheralArray;
  17. @end
  18. @implementation ELDemoScanVC
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. // Do any additional setup after loading the view from its nib.
  22. self.bleStatusLabel.text = @"";
  23. [self initBle];
  24. }
  25. - (void)dealloc {
  26. [self deinitBle];
  27. }
  28. #pragma mark - UITableViewDelegate, UITableViewDataSource
  29. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  30. return 1;
  31. }
  32. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  33. return self.peripheralArray.count;
  34. }
  35. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  36. return 88;
  37. }
  38. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  39. ELDeviceScanCell *cell = [ELDeviceScanCell subsribeCell];
  40. ELAILinkPeripheral *per = self.peripheralArray[indexPath.row];
  41. cell.iconImageView.image = [UIImage imageNamed:@"air_detection_type_ic"];
  42. cell.cidValueLabel.text = [NSString stringWithFormat:@"0x%02X", per.cid];
  43. cell.vidValueLabel.text = [NSString stringWithFormat:@"0x%02X", per.vid];
  44. cell.pidValueLabel.text = [NSString stringWithFormat:@"0x%02X", per.pid];
  45. cell.macValueLabel.text = per.macAddressString;
  46. cell.rssiValueLabel.text = [NSString stringWithFormat:@"%@", per.RSSI];
  47. return cell;
  48. }
  49. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  50. ELAILinkPeripheral *per = self.peripheralArray[indexPath.row];
  51. if (self.selectedBlock) {
  52. self.selectedBlock(per);
  53. }
  54. [self dismissViewControllerAnimated:YES completion:^{
  55. }];
  56. }
  57. #pragma mark - ELAILinkBleManagerDelegate
  58. - (void)initBle {
  59. self.bleManager = [[ELAILinkBleManager alloc] init];
  60. self.bleManager.ailinkDelegate = self;
  61. }
  62. - (void)scanBle {
  63. self.peripheralArray = [NSMutableArray array];
  64. [self.bleManager scanFilterWithCidArray:self.cids];
  65. }
  66. - (void)deinitBle {
  67. [self.bleManager stopScan];
  68. self.bleManager.ailinkDelegate = nil;
  69. [self.bleManager disconnectPeripheral];
  70. }
  71. - (void)updateBleStatusView:(NELBleManagerConnectState)state {
  72. if (self.bleManager.central.state == CBManagerStatePoweredOff) {
  73. self.bleStatusLabel.text = @"蓝牙关闭";
  74. return;
  75. }
  76. switch (state) {
  77. case NELBleManagerConnectStateDisconnected:
  78. case NELBleManagerConnectStateFailed:
  79. case NELBleManagerConnectStateFailedValidation:
  80. self.bleStatusLabel.text = @"连接失败";
  81. break;
  82. case NELBleManagerConnectStateCentralScanning:
  83. self.bleStatusLabel.text = @"正在扫描...";
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. // 设备状态变更
  90. - (void)managerDidUpdateState:(CBCentralManager *)central {
  91. NSLog(@"%s state:%@", __func__, @(central.state));
  92. if (central.state == CBManagerStatePoweredOn) {
  93. [self scanBle];
  94. } else if (central.state == CBManagerStatePoweredOff) {
  95. self.bleConnectState = NELBleManagerConnectStateCentralPowerOff;
  96. }
  97. }
  98. - (void)managerScanState:(BOOL)scanning {
  99. NSLog(@"%s scanning:%@", __func__, @(scanning));
  100. if (scanning) {
  101. [self updateBleStatusView:NELBleManagerConnectStateCentralScanning];
  102. }
  103. }
  104. // 扫描到设备
  105. - (void)managerDidDiscoverPeripheral:(ELAILinkPeripheral *)peripheral {
  106. NSLog(@"managerDidDiscoverPeripheral cid:%02x vid:%02x pid:%02x mac:%@", peripheral.cid, peripheral.vid, peripheral.pid, peripheral.macAddressString);
  107. for (int i=0; i<self.peripheralArray.count; i++) {
  108. ELAILinkPeripheral *per = self.peripheralArray[i];
  109. if ([per.macData isEqualToData:peripheral.macData]) {
  110. self.peripheralArray[i] = per;
  111. [self.tableView reloadData];
  112. return;
  113. }
  114. }
  115. [self.peripheralArray addObject:peripheral];
  116. [self.peripheralArray sortUsingComparator:^NSComparisonResult(ELAILinkPeripheral * _Nonnull obj1, ELAILinkPeripheral * _Nonnull obj2) {
  117. if (obj1.RSSI.integerValue < obj2.RSSI.integerValue) return NSOrderedDescending;
  118. else if (obj1.RSSI.integerValue > obj2.RSSI.integerValue) return NSOrderedAscending;
  119. return NSOrderedSame;
  120. }];
  121. [self.tableView reloadData];
  122. }
  123. - (void)managerDidUpdateConnect:(NELBleManagerConnectState)state {
  124. NSLog(@"%s NELBleManagerConnectState:%@", __func__, @(state));
  125. }
  126. //A7数据
  127. - (void)aiLinkBleReceiveA7Data:(NSData *)payload {
  128. NSLog(@"%s #### payload:%@", __func__, payload);
  129. // Byte *bytes = (Byte *)payload.bytes;
  130. // Byte cmd = bytes[0];
  131. }
  132. //A6数据
  133. - (void)aiLinkBleReceiveA6Data:(NSData *)packet {
  134. NSLog(@"%s ##### packet:%@", __func__, packet);
  135. Byte *bytes = (Byte *)packet.bytes;
  136. Byte cmd = bytes[2];
  137. if (cmd == ELInetGetCmdTypeGetBatteryState) {
  138. int power = self.bleManager.battery.power;
  139. ELBatteryChargingState state = self.bleManager.battery.state;
  140. NSLog(@"##### state: %lu power: %d", (unsigned long)state, power);
  141. }
  142. if (cmd == ELInetGetCmdTypeGetBMVersion) {
  143. NSString *bmVersion = self.bleManager.bmVersion;
  144. NSLog(@"##### bmVersion: %@", bmVersion);
  145. }
  146. if (cmd == ELInetGetCmdTypeGetBMVersionPro) {
  147. NSString *bmVersionPro = self.bleManager.bmVersionPro;
  148. NSLog(@"##### bmVersionPro: %@", bmVersionPro);
  149. }
  150. }
  151. @end