Created
April 3, 2016 21:31
-
-
Save kived/0e7d910994ba69e807ff5bb9cf662d68 to your computer and use it in GitHub Desktop.
CBCentralManager code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <CoreBluetooth/CoreBluetooth.h> | |
@interface cbcentral : NSObject<CBCentralManagerDelegate> { | |
CBCentralManager* central; | |
NSMutableArray* events; | |
NSLock* eventlock; | |
NSMutableArray* _peripherals; | |
CBPeripheral* _connecting; | |
} | |
@property (strong, atomic) NSMutableArray* peripherals; | |
@property (strong, atomic) CBPeripheral* connecting; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "cbcentral.h" | |
@implementation cbcentral | |
- (id) init { | |
if (self = [super init]) { | |
_peripherals = [NSMutableArray new]; | |
_connecting = nil; | |
eventlock = [[NSLock alloc] init]; | |
events = [[NSMutableArray alloc] init]; | |
central = [[CBCentralManager alloc] initWithDelegate:self | |
queue:dispatch_get_main_queue()]; | |
} | |
return self; | |
} | |
- (void) addPeripheral:(CBPeripheral *)peripheral { | |
if (![self.peripherals containsObject:peripheral]) { | |
NSLog(@"saving peripheral"); | |
[self.peripherals addObject:peripheral]; | |
[peripheral retain]; | |
} | |
} | |
- (void) clearPeripherals { | |
[self.peripherals removeAllObjects]; | |
} | |
- (void) queueEvent:(NSArray *)event { | |
[eventlock lock]; | |
[events addObject:event]; | |
[eventlock unlock]; | |
NSLog(@"event queued"); | |
} | |
- (BOOL) hasEvents { | |
return events.count; | |
} | |
- (NSArray *) getEvent { | |
[eventlock lock]; | |
if (events.count == 0) { | |
[eventlock unlock]; | |
return nil; | |
} | |
NSArray* event = [events objectAtIndex:0]; | |
[events removeObjectAtIndex:0]; | |
[eventlock unlock]; | |
return event; | |
} | |
- (void) startScanning:(BOOL)allowDuplicates { | |
[central scanForPeripheralsWithServices:nil | |
options:nil]; | |
} | |
- (void) stopScan { | |
[central stopScan]; | |
} | |
- (void) connectPeripheral:(CBPeripheral *)peripheral { | |
if (![self.peripherals containsObject:peripheral]) { | |
NSLog(@"error: connect to invalid peripheral"); | |
return; | |
} | |
__weak cbcentral *this = self; | |
NSLog(@"dispatch connect peripheral"); | |
self.connecting = peripheral; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
NSLog(@"connect peripheral and save 6"); | |
//this.connecting = peripheral; | |
[central connectPeripheral:this.connecting | |
options:nil]; | |
}); | |
} | |
- (void) cancelConnectPeripheral:(CBPeripheral *)peripheral { | |
NSLog(@"cancel connect peripheral"); | |
[central cancelPeripheralConnection:peripheral]; | |
} | |
#pragma mark CBCentralManagerDelegate | |
- (void) centralManagerDidUpdateState:(CBCentralManager *)central { | |
NSLog(@"central update state"); | |
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManagerDidUpdateState_", | |
central, | |
nil]; | |
[self queueEvent:event]; | |
} | |
- (void) centralManager:(CBCentralManager *)central | |
didDiscoverPeripheral:(CBPeripheral *)peripheral | |
advertisementData:(NSDictionary *)advertisementData | |
RSSI:(NSNumber *)RSSI { | |
NSLog(@"did discover peripheral"); | |
[self addPeripheral:peripheral]; | |
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didDiscoverPeripheral_advertisementData_RSSI_", | |
central, | |
peripheral, | |
advertisementData, | |
RSSI, | |
nil]; | |
[self queueEvent:event]; | |
} | |
- (void) centralManager:(CBCentralManager *)central | |
didConnectPeripheral:(CBPeripheral *)peripheral { | |
NSLog(@"did connect peripheral"); | |
[self addPeripheral:peripheral]; | |
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didConnectPeripheral_", | |
central, | |
peripheral, | |
nil]; | |
[self queueEvent:event]; | |
} | |
- (void) centralManager:(CBCentralManager *)central | |
didFailToConnectPeripheral:(CBPeripheral *)peripheral | |
error:(NSError *)error { | |
NSLog(@"did fail to connect peripheral"); | |
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didFailToConnectPeripheral_error_", | |
central, | |
peripheral, | |
error, | |
nil]; | |
[self queueEvent:event]; | |
} | |
- (void) centralManager:(CBCentralManager *)central | |
didDisconnectPeripheral:(CBPeripheral *)peripheral | |
error:(NSError *)error { | |
NSLog(@"did disconnect peripheral"); | |
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didDisconnectPeripheral_error_", | |
central, | |
peripheral, | |
error, | |
nil]; | |
[self queueEvent:event]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment