Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (BOOL)
- application: (UIApplication*) inApplication
- didFinishLaunchingWithOptions: (NSDictionary*) inLaunchOptions
- {
- #if 1
- CFUUIDRef cfUUID = CFUUIDCreate(kCFAllocatorDefault);
- NSLog(@"uuid: cfUUID: %@", cfUUID);
- cfUUID = CFUUIDCreate(kCFAllocatorDefault);
- NSLog(@"uuid: cfUUID: %@", cfUUID);
- #endif
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
- {
- self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
- }
- else
- {
- self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
- }
- self.window.rootViewController = self.viewController;
- [self.window makeKeyAndVisible];
- [self setupBTLE];
- return YES;
- }
- - (void)
- setupBTLE
- {
- #if TARGET_IPHONE_SIMULATOR
- // Look for peripherals…
- self.cm = [[CBCentralManager alloc] initWithDelegate: self queue: nil];
- #else
- // Create the peripheral…
- self.pm = [[CBPeripheralManager alloc] initWithDelegate: self queue: nil];
- #endif
- }
- - (void)
- peripheralManagerDidUpdateState: (CBPeripheralManager*) inPM
- {
- NSLog(@"peripheralManagerDidUpdateState: %d", inPM.state);
- if (inPM.state == CBPeripheralManagerStatePoweredOn)
- {
- [self setupService];
- }
- }
- - (void)
- setupService
- {
- // Add a service…
- CBUUID* serviceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
- CBMutableService* service = [[CBMutableService alloc] initWithType: serviceUUID primary: true];
- // Add a characteristic…
- CBUUID* charUUID = [CBUUID UUIDWithString: kTestCharacteristicUUID];
- CBMutableCharacteristic* characteristic =
- [[CBMutableCharacteristic alloc]
- initWithType: charUUID
- properties: CBCharacteristicPropertyNotify
- value: nil
- permissions: 0];
- service.characteristics = @[ characteristic ];
- [self.pm addService: service];
- }
- - (void)
- peripheralManager: (CBPeripheralManager*) inPeripheral
- didAddService: (CBService*) inService
- error: (NSError*) inError
- {
- NSLog(@"pm:didAddService:Error: %@", inError);
- if (inError != nil)
- {
- return;
- }
- // Advertise…
- CBUUID* serviceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
- NSDictionary* dict = @{ CBAdvertisementDataServiceUUIDsKey : @[ serviceUUID ],
- CBAdvertisementDataLocalNameKey : @"Test Peripheral" };
- [self.pm startAdvertising: dict];
- }
- - (void)
- peripheralManagerDidStartAdvertising: (CBPeripheralManager*) inPM
- error: (NSError*) inError
- {
- if (inError != nil)
- {
- NSLog(@"peripheralManagerDidStartAdvertising error: %@", inError);
- return;
- }
- NSLog(@"peripheralManagerDidStartAdvertising.");
- }
- - (void)
- peripheralManager: (CBPeripheralManager*) inPM
- central: (CBCentral*) inCentral
- didSubscribeToCharacteristic: (CBCharacteristic*) inCharacteristic
- {
- NSLog(@"Center did subscribe!");
- }
- - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- {
- NSLog(@"didReceiveReadRequest");
- }
- - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
- {
- NSLog(@"didReceiveWriteRequests");
- }
- - (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral
- {
- NSLog(@"peripheralManagerIsReadyToUpdateSubscribers");
- }
- #pragma mark -
- #pragma mark • CB Central
- - (void)
- centralManagerDidUpdateState: (CBCentralManager*) inCM
- {
- NSLog(@"centralManagerDidUpdateState: %d", inCM.state);
- if (inCM.state == CBCentralManagerStatePoweredOn)
- {
- CBUUID* serviceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
- NSArray* svcs = @[ serviceUUID ];
- [inCM scanForPeripheralsWithServices: svcs options: @{ CBCentralManagerScanOptionAllowDuplicatesKey : @false }];
- NSLog(@"Central is Scanning");
- }
- }
- - (void)
- centralManager: (CBCentralManager*) inCM
- didDiscoverPeripheral: (CBPeripheral*) inPeripheral
- advertisementData: (NSDictionary*) inAdvertisementData
- RSSI: (NSNumber*) inRSSI
- {
- //NSLog(@"discovered peripheral %@: %@", inPeripheral.name, inRSSI);
- NSLog(@"%@", inAdvertisementData);
- NSArray* uuids = [inAdvertisementData valueForKey: CBAdvertisementDataServiceUUIDsKey];
- CBUUID* kServiceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
- for (CBUUID* uuid in uuids)
- {
- NSLog(@"service: %@", uuid);
- if ([uuid isEqual: kServiceUUID])
- {
- NSLog(@"Found service: %@", [inAdvertisementData valueForKey: CBAdvertisementDataLocalNameKey]);
- //[inCM stopScan];
- if (false && self.connectedPeripheral == nil)
- {
- NSLog(@"Connecting");
- self.connectedPeripheral = inPeripheral;
- inPeripheral.delegate = self;
- [inCM connectPeripheral: inPeripheral options: @{ CBConnectPeripheralOptionNotifyOnDisconnectionKey: @true }];
- }
- }
- }
- #if 0
- for (CBService* service in inPeripheral.services)
- {
- NSLog(@" Service: %@", service);
- for (CBCharacteristic* characteristic in service.characteristics)
- {
- NSLog(@" Characteristic: %@", characteristic);
- }
- }
- #endif
- }
- - (void)
- centralManager: (CBCentralManager*) inCM
- didFailToConnectPeripheral: (CBPeripheral*) inPeripheral
- error: (NSError*) inError
- {
- NSLog(@"Error connecting to peripheral: %@", inError);
- }
- - (void)
- centralManager: (CBCentralManager*) inCM
- didConnectPeripheral: (CBPeripheral*) inPeripheral
- {
- NSLog(@"Did connect to peripheral");
- }
- - (void)
- centralManager: (CBCentralManager*) inCM
- didDisconnectPeripheral: (CBPeripheral*) inPeripheral
- error: (NSError*) inError
- {
- NSLog(@"cm:didDisconnect: %@ error: %@", inPeripheral, inError);
- if (inPeripheral == self.connectedPeripheral)
- {
- self.connectedPeripheral.delegate = nil;
- self.connectedPeripheral = nil;
- }
- }
- #pragma mark -
- #pragma mark • CBPeripheral
- - (void)
- peripheralDidUpdateName: (CBPeripheral*) inPeripheral
- {
- NSLog(@"Peripheral name: %@", inPeripheral.name);
- }
Advertisement
Add Comment
Please, Sign In to add comment