JetForMe

Untitled

Nov 4th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. - (BOOL)
  3. application: (UIApplication*) inApplication
  4.     didFinishLaunchingWithOptions: (NSDictionary*) inLaunchOptions
  5. {
  6. #if 1
  7.     CFUUIDRef cfUUID = CFUUIDCreate(kCFAllocatorDefault);
  8.     NSLog(@"uuid: cfUUID: %@", cfUUID);
  9.     cfUUID = CFUUIDCreate(kCFAllocatorDefault);
  10.     NSLog(@"uuid: cfUUID: %@", cfUUID);
  11. #endif
  12.  
  13.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  14.     // Override point for customization after application launch.
  15.     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  16.     {
  17.         self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
  18.     }
  19.     else
  20.     {
  21.         self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
  22.     }
  23.     self.window.rootViewController = self.viewController;
  24.     [self.window makeKeyAndVisible];
  25.    
  26.     [self setupBTLE];
  27.    
  28.     return YES;
  29. }
  30.  
  31. - (void)
  32. setupBTLE
  33. {
  34. #if TARGET_IPHONE_SIMULATOR
  35.     //  Look for peripherals…
  36.    
  37.     self.cm = [[CBCentralManager alloc] initWithDelegate: self queue: nil];
  38. #else
  39.  
  40.     //  Create the peripheral…
  41.    
  42.     self.pm = [[CBPeripheralManager alloc] initWithDelegate: self queue: nil];
  43. #endif
  44. }
  45.  
  46. - (void)
  47. peripheralManagerDidUpdateState: (CBPeripheralManager*) inPM
  48. {
  49.     NSLog(@"peripheralManagerDidUpdateState: %d", inPM.state);
  50.    
  51.     if (inPM.state == CBPeripheralManagerStatePoweredOn)
  52.     {
  53.         [self setupService];
  54.     }
  55. }
  56.  
  57. - (void)
  58. setupService
  59. {
  60.     //  Add a service…
  61.    
  62.     CBUUID* serviceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
  63.     CBMutableService* service = [[CBMutableService alloc] initWithType: serviceUUID primary: true];
  64.    
  65.     //  Add a characteristic…
  66.    
  67.     CBUUID* charUUID = [CBUUID UUIDWithString: kTestCharacteristicUUID];
  68.     CBMutableCharacteristic* characteristic =
  69.         [[CBMutableCharacteristic alloc]
  70.                 initWithType: charUUID
  71.                 properties: CBCharacteristicPropertyNotify
  72.                 value: nil
  73.                 permissions: 0];
  74.    
  75.     service.characteristics = @[ characteristic ];
  76.    
  77.     [self.pm addService: service];
  78. }
  79.  
  80. - (void)
  81. peripheralManager: (CBPeripheralManager*) inPeripheral
  82.     didAddService: (CBService*) inService
  83.     error: (NSError*) inError
  84. {
  85.     NSLog(@"pm:didAddService:Error: %@", inError);
  86.    
  87.     if (inError != nil)
  88.     {
  89.         return;
  90.     }
  91.    
  92.     //  Advertise…
  93.    
  94.     CBUUID* serviceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
  95.     NSDictionary* dict = @{ CBAdvertisementDataServiceUUIDsKey : @[ serviceUUID ],
  96.                             CBAdvertisementDataLocalNameKey : @"Test Peripheral" };
  97.     [self.pm startAdvertising: dict];
  98. }
  99.  
  100. - (void)
  101. peripheralManagerDidStartAdvertising: (CBPeripheralManager*) inPM
  102.     error: (NSError*) inError
  103. {
  104.     if (inError != nil)
  105.     {
  106.         NSLog(@"peripheralManagerDidStartAdvertising error: %@", inError);
  107.         return;
  108.     }
  109.    
  110.     NSLog(@"peripheralManagerDidStartAdvertising.");
  111. }
  112.  
  113. - (void)
  114. peripheralManager: (CBPeripheralManager*) inPM
  115.     central: (CBCentral*) inCentral
  116.     didSubscribeToCharacteristic: (CBCharacteristic*) inCharacteristic
  117. {
  118.     NSLog(@"Center did subscribe!");
  119. }
  120.  
  121. - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
  122. {
  123.     NSLog(@"didReceiveReadRequest");
  124. }
  125.  
  126. - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
  127. {
  128.     NSLog(@"didReceiveWriteRequests");
  129. }
  130.  
  131. - (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral
  132. {
  133.     NSLog(@"peripheralManagerIsReadyToUpdateSubscribers");
  134. }
  135.  
  136.  
  137. #pragma mark -
  138. #pragma mark • CB Central
  139.  
  140. - (void)
  141. centralManagerDidUpdateState: (CBCentralManager*) inCM
  142. {
  143.     NSLog(@"centralManagerDidUpdateState: %d", inCM.state);
  144.    
  145.     if (inCM.state == CBCentralManagerStatePoweredOn)
  146.     {
  147.         CBUUID* serviceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
  148.         NSArray* svcs = @[ serviceUUID ];
  149.         [inCM scanForPeripheralsWithServices: svcs options: @{ CBCentralManagerScanOptionAllowDuplicatesKey : @false }];
  150.         NSLog(@"Central is Scanning");
  151.     }
  152. }
  153.  
  154. - (void)
  155. centralManager: (CBCentralManager*) inCM
  156.     didDiscoverPeripheral: (CBPeripheral*) inPeripheral
  157.     advertisementData: (NSDictionary*) inAdvertisementData
  158.     RSSI: (NSNumber*) inRSSI
  159. {
  160.     //NSLog(@"discovered peripheral %@: %@", inPeripheral.name, inRSSI);
  161.     NSLog(@"%@", inAdvertisementData);
  162.     NSArray* uuids = [inAdvertisementData valueForKey: CBAdvertisementDataServiceUUIDsKey];
  163.     CBUUID* kServiceUUID = [CBUUID UUIDWithString: kTestServiceUUID];
  164.     for (CBUUID* uuid in uuids)
  165.     {
  166.         NSLog(@"service: %@", uuid);
  167.         if ([uuid isEqual: kServiceUUID])
  168.         {
  169.             NSLog(@"Found service: %@", [inAdvertisementData valueForKey: CBAdvertisementDataLocalNameKey]);
  170.            
  171.             //[inCM stopScan];
  172.            
  173.             if (false && self.connectedPeripheral == nil)
  174.             {
  175.                 NSLog(@"Connecting");
  176.                 self.connectedPeripheral = inPeripheral;
  177.                 inPeripheral.delegate = self;
  178.                 [inCM connectPeripheral: inPeripheral options: @{ CBConnectPeripheralOptionNotifyOnDisconnectionKey: @true }];
  179.             }
  180.         }
  181.     }
  182.    
  183. #if 0
  184.     for (CBService* service in inPeripheral.services)
  185.     {
  186.         NSLog(@"  Service: %@", service);
  187.         for (CBCharacteristic* characteristic in service.characteristics)
  188.         {
  189.             NSLog(@"    Characteristic: %@", characteristic);
  190.         }
  191.     }
  192. #endif
  193. }
  194.  
  195. - (void)
  196. centralManager: (CBCentralManager*) inCM
  197.     didFailToConnectPeripheral: (CBPeripheral*) inPeripheral
  198.     error: (NSError*) inError
  199. {
  200.     NSLog(@"Error connecting to peripheral: %@", inError);
  201. }
  202.  
  203. - (void)
  204. centralManager: (CBCentralManager*) inCM
  205.     didConnectPeripheral: (CBPeripheral*) inPeripheral
  206. {
  207.     NSLog(@"Did connect to peripheral");
  208. }
  209.  
  210. - (void)
  211. centralManager: (CBCentralManager*) inCM
  212.     didDisconnectPeripheral: (CBPeripheral*) inPeripheral
  213.     error: (NSError*) inError
  214. {
  215.     NSLog(@"cm:didDisconnect: %@ error: %@", inPeripheral, inError);
  216.     if (inPeripheral == self.connectedPeripheral)
  217.     {
  218.         self.connectedPeripheral.delegate = nil;
  219.         self.connectedPeripheral = nil;
  220.     }
  221. }
  222.  
  223. #pragma mark -
  224. #pragma mark • CBPeripheral
  225.  
  226. - (void)
  227. peripheralDidUpdateName: (CBPeripheral*) inPeripheral
  228. {
  229.     NSLog(@"Peripheral name: %@", inPeripheral.name);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment