title: OSX bluetooth openRFCOMMChannelAsync claims to have connected but connection is not established and delegate is never called
body: <p>I have written a C++ wrapper for the objc bluetooth framework for OSX. The wrapper works so far that it can a) obtain a list of all paired devices, b) get one device, c) print its name, d) open the channel. This all works without any reported errors. However, my bluetooth device (an RFComm box) never reports a connection. It stays offline. The delegate I have registered with openRFCOMMChannelAsync is never called.</p>
  
  <p>Since it's a C++ wrapper the header is a standard C++ class:</p>
  
  <pre><code>    class IosBT {
          public:
              //IosBT();                                                                                                                                             
              void clearText();
              void sendMessage(char* dataToSend, int len);
              void log(const char *text);
              void discover();
              void closeConnection();
  
          private:
              void *mRFCOMMChannel;
  
     };
  </code></pre>
  
  <p>The implementation of the class is written as an .mm file. I explain it in two chunks. First is the delegate which keeps a pointer to the C++ class:</p>
  
  <pre><code>@interface AsyncCommDelegate : NSObject &lt;IOBluetoothRFCOMMChannelDelegate&gt; {
      @public
      IosBT* delegateCPP;
  }
  @end
  
  @implementation AsyncCommDelegate {
  }
  
  -(void)rfcommChannelOpenComplete:(IOBluetoothRFCOMMChannel *)rfcommChannel status:(IOReturn)error
  {
  
      if ( error != kIOReturnSuccess ) {
          delegateCPP-&gt;log("Error - failed to open the RFCOMM channel with error %08lx.\n");
          return;
      }
      else{
          delegateCPP-&gt;log("Connected. Yeah!\n");
      }
  
  }
  
  -(void)rfcommChannelData:(IOBluetoothRFCOMMChannel *)rfcommChannel data:(void *)dataPointer length:(size_t)dataLength
  {
      NSString  *message = [[NSString alloc] initWithBytes:dataPointer length:dataLength encoding:NSUTF8StringEncoding];
      delegateCPP-&gt;log([message UTF8String]);
  }
  
  
  @end
  </code></pre>
  
  <p>The second chunk of the .mm file is the "discover" method which establishes the connection.
  Here, I suspect something is not right:</p>
  
  <pre><code>void IosBT::discover()
  {
      IOBluetoothSDPUUID                                      *sppServiceUUID;
      NSArray                                                         *deviceArray;
      IOBluetoothRFCOMMChannel *chan;
  
      log("Attempting to connect\n");
  
      sppServiceUUID = [IOBluetoothSDPUUID uuid16:kBluetoothSDPUUID16ServiceClassSerialPort];
  
      deviceArray = [IOBluetoothDevice pairedDevices];
      if ( ( deviceArray == nil ) || ( [deviceArray count] == 0 ) ) {
          log("Error - no selected device.  ***This should never happen.***\n");
      return;
      }
      fprintf(stderr,"%lu devices\n",(unsigned long)deviceArray.count);
      IOBluetoothDevice *device = [deviceArray objectAtIndex:0];
      fprintf(stderr,"dev=%s\n",[device.name UTF8String]);
      IOBluetoothSDPServiceRecord     *sppServiceRecord = [device getServiceRecordForUUID:sppServiceUUID];
      if ( sppServiceRecord == nil ) {
          log("Error - no spp service in selected device.  ***This should never happen since the selector forces the user to select only devices with spp.**\
  *\n");
      return;
      }
     // To connect we need a device to connect and an RFCOMM channel ID to open on the device:
      UInt8 rfcommChannelID;
      if ( [sppServiceRecord getRFCOMMChannelID:&amp;rfcommChannelID] != kIOReturnSuccess ) {
          log("Error - no SPP device.\n");
          return;
      }
  
      AsyncCommDelegate* asyncCommDelegate = [[AsyncCommDelegate alloc] init];
      asyncCommDelegate-&gt;delegateCPP = this;
  
      if ( [device openRFCOMMChannelAsync:&amp;chan withChannelID:rfcommChannelID delegate:asyncCommDelegate] != kIOReturnSuccess ) {
          log("Error - open sequence failed.\n");
          return;
      }
  
      if ( chan == NULL ) {
          log("Error - chan == NULL");
          return;
      }
  
      [chan setDelegate:asyncCommDelegate];
  
      log("Successfully connected");
  
      mRFCOMMChannel = (__bridge void*)chan;
  }
  </code></pre>
  
  <p>I only log the received data from the bluetooth device with the "log" method on the screen with a fprintf.</p>
  
  <p>The main program is simply:</p>
  
  <pre><code>IosBT ad;
  ad.discover();
  getchar();
  ad.closeConnection();
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Attempting to connect
  
  1 devices
  dev=GN-ATTYS1-FB86
  Successfully connected
  </code></pre>
  
  <p>However the method "Channeldata" is never called but should print data received from the bluetooth device. The bluetooth box reports that it's not connected. However, the right hand bluetooth icon on the top bar shows that the Mac is connected to GN-ATTYS1-FB86. However, that's not true.</p>
  
  <p>The demo here using a window to print on the screen works fine with my bluetooth box: <a href="https://github.com/berndporr/coco-bluetooth-rfcomm" rel="nofollow noreferrer">https://github.com/berndporr/coco-bluetooth-rfcomm</a>. For me the only apparent difference is that I use the commandline.</p>
  
  <p>Question is: why is OSX reporting a working connection but not actually establishing one? How can I possibly debug this without any error reported?</p>