| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- //
- // HeartBLEDriver.m
- // HeartTestDemo
- //
- // Created by 郭志奇 on 2019/8/7.
- // Copyright © 2019 郭志奇. All rights reserved.
- //
- #import "HeartBLEDriver.h"
- #import "HeartBLEDevice.h"
- #import "HeartBLEManager.h"
- @interface HeartBLEDriver()<CBCentralManagerDelegate,CBPeripheralDelegate>
- {
- HeartBLEDevice *HeartDevice;
- }
- @property (nonatomic,strong)NSTimer *scanTimer;
- @end
- @implementation HeartBLEDriver
- + (HeartBLEDriver *)sharedInstance
- {
- static HeartBLEDriver* singleInstance;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- singleInstance = [[HeartBLEDriver alloc]init];
- });
- return singleInstance;
- }
- - (instancetype)init
- {
- self = [super init];
- if (self)
- {
- dispatch_queue_t queue = dispatch_get_main_queue();
- centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:nil];
- //[centralManager setDelegate:self];
-
- discoverDevices = [NSMutableArray arrayWithCapacity:1];
- discoverPers = [NSMutableArray arrayWithCapacity:1];
- connectedPeripherals = [NSMutableArray arrayWithCapacity:1];
- connectedDevices = [NSMutableArray arrayWithCapacity:1];
- theFliter = [NSMutableArray arrayWithCapacity:1];
- }
- return self;
- }
- - (void)setDelegate:(id)delegate{
- theDelegate = delegate;
- }
- - (BOOL)isBLEPoweredOn{
- return (centralManager.state == CBManagerStatePoweredOn);
- }
- #pragma mark - Scan
- - (void)StartScanForDevice
- {
- [discoverDevices removeAllObjects];
- [discoverPers removeAllObjects];
-
- if ([theDelegate respondsToSelector:@selector(onDeviceFound:)]) {
- [theDelegate onDeviceFound:discoverDevices.copy];
- }
-
- if (centralManager.state == CBManagerStateUnsupported) {
-
- }else {
- if (centralManager.state == CBManagerStatePoweredOn) {
-
- [centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:NO]}];
- }
- }
- }
- - (void)stopScan {
-
- [centralManager stopScan];
- }
- #pragma mark - connect
- - (void)connectDevice:(CBPeripheral*)device
- {
- if (centralManager.state == CBManagerStateUnsupported)
- {
-
- }
- else
- {
- if (centralManager.state == CBManagerStatePoweredOn && device != nil) {
- [centralManager connectPeripheral:device options:nil];//@{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES,CBConnectPeripheralOptionNotifyOnNotificationKey:@YES,CBConnectPeripheralOptionNotifyOnDisconnectionKey:@YES}
- }
- }
- }
- - (void)disConnectDevice:(CBPeripheral *)peripheral{
-
- if (!peripheral) {
- return;
- }
-
- [centralManager cancelPeripheralConnection:peripheral];
- }
- - (void)closelAllDevice{
- for (CBPeripheral *per in connectedPeripherals) {
- [centralManager cancelPeripheralConnection:per];
- }
- }
- #pragma mark - CBCentralManager Delegate
- - (void)centralManagerDidUpdateState:(CBCentralManager *)central
- {
- switch (central.state) {
- case CBManagerStatePoweredOff:
- NSLog(@"CBCentralManagerStatePoweredOff");
- break;
- case CBManagerStatePoweredOn:
- NSLog(@"CBCentralManagerStatePoweredOn");
- break;
- case CBManagerStateResetting:
- NSLog(@"CBCentralManagerStateResetting");
- break;
- case CBManagerStateUnauthorized:
- NSLog(@"CBCentralManagerStateUnauthorized");
- break;
- case CBManagerStateUnknown:
- NSLog(@"CBCentralManagerStateUnknown");
- break;
- case CBManagerStateUnsupported:
- NSLog(@"CBCentralManagerStateUnsupported");
- break;
-
- default:
- break;
- }
- }
- - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI
- {
- NSArray *arrName = @[@"CL831",@"CL837"];
- // NSArray *arrName = @[@"CL820W", @"CL831",@"CL837",@"HW702C",@"sloops",@"SE2", @"CL880"];
-
- for (NSString *strA in arrName)
- {
-
- NSString *localName = [advertisementData objectForKey:@"kCBAdvDataLocalName"];
- if (localName != nil && [localName rangeOfString:strA].location !=NSNotFound) // [localName rangeOfString:strA].location !=NSNotFound &&
- {
- NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
- formatter.numberStyle = NSNumberFormatterDecimalStyle;
- NSString *Numstr = [formatter stringFromNumber:RSSI];
-
- HeartBLEDevice *Device = [[HeartBLEDevice alloc]initWithPeriphral:peripheral];
-
- Device.DeviceName = localName;
- Device.RSSI = Numstr;
- Device.UUIDStr = peripheral.identifier.UUIDString;
-
- [self addDiscoverDevices:Device];
- [self addDiscoverPers:peripheral];
-
- //代理方法传值
- if ([theDelegate respondsToSelector:@selector(onDeviceFound:)]) {
- [theDelegate onDeviceFound:discoverDevices.copy];
- }
- }
- }
- }
- - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
- {
- //停止扫描
- [centralManager stopScan];
-
- [self addPeripheral:peripheral];
-
- HeartBLEDevice *device = [self getDeviceWithPeripheral:peripheral];
-
- [self addConnectedDevice:device];
-
- //遵循代理方法 代理回调
- [peripheral setDelegate:device];
-
- [discoverPers removeObject:peripheral];
- [discoverDevices removeObject:device];
-
- if ([theDelegate respondsToSelector:@selector(onDeviceFound:)]) {
- [theDelegate onDeviceFound:discoverDevices.copy];
- }
- // 根据UUID来寻找服务
- [peripheral discoverServices:nil];
-
- //连接设备的参数回调
- if ([theDelegate respondsToSelector:@selector(isConnected:withDevice:)]) {
- [theDelegate isConnected:YES withDevice:device];
- }
- }
- - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
- {
-
- HeartBLEDevice *device = [self getDeviceWithPeripheral:peripheral];
-
- //连接失败的回调
- if ([theDelegate respondsToSelector:@selector(isConnected:withDevice:)]) {
- [theDelegate isConnected:NO withDevice:device];
- }
- }
- - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
- {
- HeartBLEDevice *Device = [self getDeviceWithPeripheral:peripheral];
-
- [connectedPeripherals removeObject:peripheral];
- [connectedDevices removeObject:Device];
-
- if ([theDelegate respondsToSelector:@selector(disconnected:)]) {
- [theDelegate disconnected:Device];
- }
- }
- - (CBPeripheral *)getPeripheralWithDevice:(HeartBLEDevice *)device{
-
- for (CBPeripheral *per in discoverPers) {
-
- if ([per.identifier.UUIDString isEqualToString:device.UUIDStr]) {
- return per;
- }
- }
- for (CBPeripheral *per in connectedPeripherals) {
- if ([per.identifier.UUIDString isEqualToString:device.UUIDStr]) {
- return per;
- }
- }
- return nil;
- }
- - (HeartBLEDevice *)getDeviceWithPeripheral:(CBPeripheral *)peripheral{
- for (HeartBLEDevice *device in discoverDevices) {
-
- if ([device.UUIDStr isEqualToString:peripheral.identifier.UUIDString]) {
- return device;
- }
- }
- for (HeartBLEDevice *device in connectedDevices) {
- if ([device.UUIDStr isEqualToString:peripheral.identifier.UUIDString]) {
- return device;
- }
- }
- return nil;
- }
- - (void)addDiscoverDevices:(HeartBLEDevice *)device
- {
- BOOL isExist = NO;
-
- if (discoverDevices.count == 0) {
- [discoverDevices addObject:device];
- }else {
- for (int i = 0;i < discoverDevices.count;i++) {
- HeartBLEDevice *info = [discoverDevices objectAtIndex:i];
- if ([info.UUIDStr isEqualToString:device.UUIDStr]) {
- isExist = YES;
- [discoverDevices replaceObjectAtIndex:i withObject:info];
- }
- }
- if (!isExist) {
- [discoverDevices addObject:device];
- }
- }
- }
- - (void)addConnectedDevice:(HeartBLEDevice *)device{
- if (![connectedDevices containsObject:device]) {
- [connectedDevices addObject:device];
- }
- }
- - (void)addDiscoverPers:(CBPeripheral *)peripheral{
- if (![discoverPers containsObject:peripheral]) {
- [discoverPers addObject:peripheral];
- }
- }
- - (void)addPeripheral:(CBPeripheral *)peripheral {
- if (![connectedPeripherals containsObject:peripheral]) {
- [connectedPeripherals addObject:peripheral];
- }
- }
- @end
|