HeartBLEDriver.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // HeartBLEDriver.m
  3. // HeartTestDemo
  4. //
  5. // Created by 郭志奇 on 2019/8/7.
  6. // Copyright © 2019 郭志奇. All rights reserved.
  7. //
  8. #import "HeartBLEDriver.h"
  9. #import "HeartBLEDevice.h"
  10. #import "HeartBLEManager.h"
  11. @interface HeartBLEDriver()<CBCentralManagerDelegate,CBPeripheralDelegate>
  12. {
  13. HeartBLEDevice *HeartDevice;
  14. }
  15. @property (nonatomic,strong)NSTimer *scanTimer;
  16. @end
  17. @implementation HeartBLEDriver
  18. + (HeartBLEDriver *)sharedInstance
  19. {
  20. static HeartBLEDriver* singleInstance;
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. singleInstance = [[HeartBLEDriver alloc]init];
  24. });
  25. return singleInstance;
  26. }
  27. - (instancetype)init
  28. {
  29. self = [super init];
  30. if (self)
  31. {
  32. dispatch_queue_t queue = dispatch_get_main_queue();
  33. centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:nil];
  34. //[centralManager setDelegate:self];
  35. discoverDevices = [NSMutableArray arrayWithCapacity:1];
  36. discoverPers = [NSMutableArray arrayWithCapacity:1];
  37. connectedPeripherals = [NSMutableArray arrayWithCapacity:1];
  38. connectedDevices = [NSMutableArray arrayWithCapacity:1];
  39. theFliter = [NSMutableArray arrayWithCapacity:1];
  40. }
  41. return self;
  42. }
  43. - (void)setDelegate:(id)delegate{
  44. theDelegate = delegate;
  45. }
  46. - (BOOL)isBLEPoweredOn{
  47. return (centralManager.state == CBManagerStatePoweredOn);
  48. }
  49. #pragma mark - Scan
  50. - (void)StartScanForDevice
  51. {
  52. [discoverDevices removeAllObjects];
  53. [discoverPers removeAllObjects];
  54. if ([theDelegate respondsToSelector:@selector(onDeviceFound:)]) {
  55. [theDelegate onDeviceFound:discoverDevices.copy];
  56. }
  57. if (centralManager.state == CBManagerStateUnsupported) {
  58. }else {
  59. if (centralManager.state == CBManagerStatePoweredOn) {
  60. [centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:NO]}];
  61. }
  62. }
  63. }
  64. - (void)stopScan {
  65. [centralManager stopScan];
  66. }
  67. #pragma mark - connect
  68. - (void)connectDevice:(CBPeripheral*)device
  69. {
  70. if (centralManager.state == CBManagerStateUnsupported)
  71. {
  72. }
  73. else
  74. {
  75. if (centralManager.state == CBManagerStatePoweredOn && device != nil) {
  76. [centralManager connectPeripheral:device options:nil];//@{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES,CBConnectPeripheralOptionNotifyOnNotificationKey:@YES,CBConnectPeripheralOptionNotifyOnDisconnectionKey:@YES}
  77. }
  78. }
  79. }
  80. - (void)disConnectDevice:(CBPeripheral *)peripheral{
  81. if (!peripheral) {
  82. return;
  83. }
  84. [centralManager cancelPeripheralConnection:peripheral];
  85. }
  86. - (void)closelAllDevice{
  87. for (CBPeripheral *per in connectedPeripherals) {
  88. [centralManager cancelPeripheralConnection:per];
  89. }
  90. }
  91. #pragma mark - CBCentralManager Delegate
  92. - (void)centralManagerDidUpdateState:(CBCentralManager *)central
  93. {
  94. switch (central.state) {
  95. case CBManagerStatePoweredOff:
  96. NSLog(@"CBCentralManagerStatePoweredOff");
  97. break;
  98. case CBManagerStatePoweredOn:
  99. NSLog(@"CBCentralManagerStatePoweredOn");
  100. break;
  101. case CBManagerStateResetting:
  102. NSLog(@"CBCentralManagerStateResetting");
  103. break;
  104. case CBManagerStateUnauthorized:
  105. NSLog(@"CBCentralManagerStateUnauthorized");
  106. break;
  107. case CBManagerStateUnknown:
  108. NSLog(@"CBCentralManagerStateUnknown");
  109. break;
  110. case CBManagerStateUnsupported:
  111. NSLog(@"CBCentralManagerStateUnsupported");
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI
  118. {
  119. NSArray *arrName = @[@"CL831",@"CL837"];
  120. // NSArray *arrName = @[@"CL820W", @"CL831",@"CL837",@"HW702C",@"sloops",@"SE2", @"CL880"];
  121. for (NSString *strA in arrName)
  122. {
  123. NSString *localName = [advertisementData objectForKey:@"kCBAdvDataLocalName"];
  124. if (localName != nil && [localName rangeOfString:strA].location !=NSNotFound) // [localName rangeOfString:strA].location !=NSNotFound &&
  125. {
  126. NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  127. formatter.numberStyle = NSNumberFormatterDecimalStyle;
  128. NSString *Numstr = [formatter stringFromNumber:RSSI];
  129. HeartBLEDevice *Device = [[HeartBLEDevice alloc]initWithPeriphral:peripheral];
  130. Device.DeviceName = localName;
  131. Device.RSSI = Numstr;
  132. Device.UUIDStr = peripheral.identifier.UUIDString;
  133. [self addDiscoverDevices:Device];
  134. [self addDiscoverPers:peripheral];
  135. //代理方法传值
  136. if ([theDelegate respondsToSelector:@selector(onDeviceFound:)]) {
  137. [theDelegate onDeviceFound:discoverDevices.copy];
  138. }
  139. }
  140. }
  141. }
  142. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
  143. {
  144. //停止扫描
  145. [centralManager stopScan];
  146. [self addPeripheral:peripheral];
  147. HeartBLEDevice *device = [self getDeviceWithPeripheral:peripheral];
  148. [self addConnectedDevice:device];
  149. //遵循代理方法 代理回调
  150. [peripheral setDelegate:device];
  151. [discoverPers removeObject:peripheral];
  152. [discoverDevices removeObject:device];
  153. if ([theDelegate respondsToSelector:@selector(onDeviceFound:)]) {
  154. [theDelegate onDeviceFound:discoverDevices.copy];
  155. }
  156. // 根据UUID来寻找服务
  157. [peripheral discoverServices:nil];
  158. //连接设备的参数回调
  159. if ([theDelegate respondsToSelector:@selector(isConnected:withDevice:)]) {
  160. [theDelegate isConnected:YES withDevice:device];
  161. }
  162. }
  163. - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
  164. {
  165. HeartBLEDevice *device = [self getDeviceWithPeripheral:peripheral];
  166. //连接失败的回调
  167. if ([theDelegate respondsToSelector:@selector(isConnected:withDevice:)]) {
  168. [theDelegate isConnected:NO withDevice:device];
  169. }
  170. }
  171. - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
  172. {
  173. HeartBLEDevice *Device = [self getDeviceWithPeripheral:peripheral];
  174. [connectedPeripherals removeObject:peripheral];
  175. [connectedDevices removeObject:Device];
  176. if ([theDelegate respondsToSelector:@selector(disconnected:)]) {
  177. [theDelegate disconnected:Device];
  178. }
  179. }
  180. - (CBPeripheral *)getPeripheralWithDevice:(HeartBLEDevice *)device{
  181. for (CBPeripheral *per in discoverPers) {
  182. if ([per.identifier.UUIDString isEqualToString:device.UUIDStr]) {
  183. return per;
  184. }
  185. }
  186. for (CBPeripheral *per in connectedPeripherals) {
  187. if ([per.identifier.UUIDString isEqualToString:device.UUIDStr]) {
  188. return per;
  189. }
  190. }
  191. return nil;
  192. }
  193. - (HeartBLEDevice *)getDeviceWithPeripheral:(CBPeripheral *)peripheral{
  194. for (HeartBLEDevice *device in discoverDevices) {
  195. if ([device.UUIDStr isEqualToString:peripheral.identifier.UUIDString]) {
  196. return device;
  197. }
  198. }
  199. for (HeartBLEDevice *device in connectedDevices) {
  200. if ([device.UUIDStr isEqualToString:peripheral.identifier.UUIDString]) {
  201. return device;
  202. }
  203. }
  204. return nil;
  205. }
  206. - (void)addDiscoverDevices:(HeartBLEDevice *)device
  207. {
  208. BOOL isExist = NO;
  209. if (discoverDevices.count == 0) {
  210. [discoverDevices addObject:device];
  211. }else {
  212. for (int i = 0;i < discoverDevices.count;i++) {
  213. HeartBLEDevice *info = [discoverDevices objectAtIndex:i];
  214. if ([info.UUIDStr isEqualToString:device.UUIDStr]) {
  215. isExist = YES;
  216. [discoverDevices replaceObjectAtIndex:i withObject:info];
  217. }
  218. }
  219. if (!isExist) {
  220. [discoverDevices addObject:device];
  221. }
  222. }
  223. }
  224. - (void)addConnectedDevice:(HeartBLEDevice *)device{
  225. if (![connectedDevices containsObject:device]) {
  226. [connectedDevices addObject:device];
  227. }
  228. }
  229. - (void)addDiscoverPers:(CBPeripheral *)peripheral{
  230. if (![discoverPers containsObject:peripheral]) {
  231. [discoverPers addObject:peripheral];
  232. }
  233. }
  234. - (void)addPeripheral:(CBPeripheral *)peripheral {
  235. if (![connectedPeripherals containsObject:peripheral]) {
  236. [connectedPeripherals addObject:peripheral];
  237. }
  238. }
  239. @end