title: how to find Bluetooth audio devices in iOS
body: <p>Okay, I'm working on a fun project that has a hurdle where I need to enable Bluetooth audio support for my iOS app.</p>
  
  <p>The hurdle I'm at is that I simply can't even begin to get a list of connected Bluetooth audio devices. Even though my iPhone 5S recognizes my earpiece (a ~3 - 4 year old <a href="https://rads.stackoverflow.com/amzn/click/com/B004VHUX9Q" rel="nofollow noreferrer" rel="nofollow noreferrer">LG HBM-230</a>, to be precise) and plays audio through it for phone calls, <strong><em>BOTH</em></strong> External Accessory and CoreBluetooth are giving me nothing useful when I query both.</p>
  
  <p>I'm basing my own code off questions &amp; answers I found for both the <a href="https://stackoverflow.com/questions/10178293/how-to-get-list-of-available-bluetooth-devices">CoreBluetooth</a> and <a href="https://stackoverflow.com/questions/19998078/corebluetooth-cannot-find-devices-but-ios-can">External Accessory</a> frameworks.</p>
  
  <p>When my code simply tries to "<code>scanForPeripheralsWithServices:nil</code>" for <em>any</em> Bluetooth devices which Settings->Bluetooth say are visible and connected, the below code simply is NOT coming up with a single hit beyond the "<code>CBCentralManagerStatePoweredOn</code>" message in the console.  </p>
  
  <p>And this line in my code (with a valid EAAccessoryManager instance) </p>
  
  <pre><code>NSArray * connectedDevices = [self.eAAccessoryManager connectedAccessories];
  </code></pre>
  
  <p>also comes back with a nil array.  </p>
  
  <p>What could I be doing wrong?  </p>
  
  <p>B.T.W., <a href="https://github.com/dautermann/BluetoothTest" rel="nofollow noreferrer">I've made this code available as a GitHub project</a>.</p>
  
  <pre><code>@implementation BluetoothManager
  
  + (BluetoothManager *)sharedInstance
  {
      static dispatch_once_t pred = 0;
      __strong static id _bluetoothMGR = nil;
  
      dispatch_once(&amp;pred, ^{
          _bluetoothMGR = [[BluetoothManager alloc] init];
      });
  
      return _bluetoothMGR;
  }
  
  - (id)init
  {
      self = [super init];
      if(self)
      {
          dispatch_queue_t centralQueue = dispatch_queue_create("com.yo.mycentral", DISPATCH_QUEUE_SERIAL);
  
          // whether we try this on a queue of "nil" (the main queue) or this separate thread, still not getting results
          self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:nil];
      }
      return self;
  }
  
  // this would hit.... if I instantiated this in a storyboard of XIB file
  - (void)awakeFromNib
  {
      if(!self.cbManager)
          self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
  }
  
  - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
  
      NSLog(@"hey I found %@",[advertisementData description]);
  }
  
  - (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
  {
      NSLog( @"I retrieved CONNECTED peripherals");
  }
  
  -(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
      NSLog(@"This is it!");
  }
  
  - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
      NSString *messtoshow;
  
      switch (central.state) {
          case CBCentralManagerStateUnknown:
          {
              messtoshow=@"State unknown, update imminent.";
              break;
          }
          case CBCentralManagerStateResetting:
          {
              messtoshow=@"The connection with the system service was momentarily lost, update imminent.";
              break;
          }
          case CBCentralManagerStateUnsupported:
          {
              messtoshow=@"The platform doesn't support Bluetooth Low Energy";
              break;
          }
          case CBCentralManagerStateUnauthorized:
          {
              messtoshow=@"The app is not authorized to use Bluetooth Low Energy";
              break;
          }
          case CBCentralManagerStatePoweredOff:
          {
              messtoshow=@"Bluetooth is currently powered off.";
              break;
          }
          case CBCentralManagerStatePoweredOn:
          {
              messtoshow=@"Bluetooth is currently powered on and available to use.";
              NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
  
              [_cbManager scanForPeripheralsWithServices:nil options:options];
  
              break;
          }   
  
      }
      NSLog(@"%@", messtoshow);
  }
  
  @end
  </code></pre>