title: Run iOS 6 device as a BLE peripheral
body: <p>As we know, iOS 6 support running devices (iPhone 4s and above, and new iPad) as a BLE peripheral. There is a demo in WWDC 2012 Session 705 called "advanced core bluetooth". I asked for the source code from Apple. They sent me a modified version of source code (BTLE_Transfer_Draft). Then I:</p>
  
  <ul>
  <li>Run the app in iPhone 5 (iOS 6) in "Peripheral Mode" and start "Advertising"</li>
  <li>Run the app in new iPad (iOS 5.1.1) in "Central Mode"</li>
  </ul>
  
  <p>The problem is that the peripheral is never been discovered at all. So I use other testing applications including some downloaded from App Store. All failed to discover peripherals. I think the problem should be in BTLE_Transfer_Draft. Because I'm not sure whether I'm allowed to present the whole source code. So I just show the "peripheral mode" part here:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      // Start up the CBPeripheralManager
      _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
  }
  
  - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
      // Opt out from any other state
      if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
          return;
      }
  
      // We're in CBPeripheralManagerStatePoweredOn state...
      NSLog(@"self.peripheralManager powered on.");
  
      // ... so build our service.
  
      // Start with the CBMutableCharacteristic
      self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]
                                                                    properties:CBCharacteristicPropertyNotify
                                                                         value:nil
                                                                   permissions:CBAttributePermissionsReadable];
  
      // Then the service
      CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]
                                                                      primary:YES];
  
      // Add the characteristic to the service
      transferService.characteristics = @[self.transferCharacteristic];
  
      // And add it to the peripheral manager
      [self.peripheralManager addService:transferService];
  }
  
  /** Start advertising
   */
  - (IBAction)switchChanged:(id)sender
  {
      if (self.advertisingSwitch.on) {
          // All we advertise is our service's UUID
          [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }];
      }
      else {
          [self.peripheralManager stopAdvertising];
      }
  }
  </code></pre>
  
  <p>The BLE is in powered on status and the startAdvertising is called. But the BLE central can never discover it.</p>
  
  <p><strong>Post updated:</strong></p>
  
  <p>According to <a href="https://stackoverflow.com/users/1320303/mttrb">mttrb</a>'s suggestion I added "CBAdvertisementDataLocalNameKey" when I startAdvertising. But my service is still can't be discovered by most of the apps including some apps from app store. The only one app can discover my service is an app from app store called "BLE scanner".</p>
  
  <p>My question is: does this mean my application is working as a peripheral? But why my own code can't discover the service? How am I supposed to debug it ?</p>
  
  <p>My code in Central Mode is like this:</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      // Start up the CBCentralManager
      _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
  }
  
  - (void)centralManagerDidUpdateState:(CBCentralManager *)central
  {
      if (central.state != CBCentralManagerStatePoweredOn) {
          return;
      }
      [self.centralManager scanForPeripheralsWithServices:nil options:nil];
  }
  
  - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
  {
  ......
  }
  
  - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
  {
      if (error) {
          NSLog(@"Error discovering services: %@", [error localizedDescription]);
          return;
      }
  }
  
  - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
  {
      // Deal with errors (if any)
      if (error) {
          NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
          return;
      }
  }
  
  - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
  {
      NSLog(@"Peripheral Disconnected");
      self.discoveredPeripheral = nil;
  }
  </code></pre>
  
  <p>The didDiscoverPeripheral and didDiscoverServices are never called. What could be wrong? Any idea? Thanks </p>