title: How to get NSOutputStream to send or flush packets immediately
body: <p>I am having an issue with latency when connecting to a bluetooth accessory using the External Accessory Framework.  When sending data I get the following custom output in the console:</p>
  
  <pre><code>if( [stream hasSpaceAvailable] )
  {
     NSLog( @"Space avail" );
  }
  else {
      NSLog(@"No space");
  }
  while( [stream hasSpaceAvailable] &amp;&amp; ( [_outputBuffer length] &gt; 0 ) )
  {
      /* write as many bytes as possible */
      NSInteger written = [stream write:[_outputBuffer bytes] maxLength:[_outputBuffer length]];
      NSLog( @"wrote %i out of %i bytes to the stream", written, [_outputBuffer length] );
  if( written == -1 )
      {
          /* error, bad */
          Log( @"Error writing bytes" );
          break;
      }
      else if( written &gt; 0 )
      {
          /* remove the bytes from the buffer that were written */
          Log( @"erasing %i bytes", written );
          [_outputBuffer replaceBytesInRange:NSMakeRange( 0, written ) withBytes:nil length:0 ];
      }
  }
  </code></pre>
  
  <p>This results with the following output where immediate pack buffer is the payload.</p>
  
  <pre><code>immediate pack buffer-&gt; 040040008
  Space avail
  wrote 10 out of 10 bytes to the stream
  immediate pack buffer-&gt; 040010005
  No space
  immediate pack buffer-&gt; 030040007
  No space
  wrote 20 out of 20 bytes to the stream
  immediate pack buffer-&gt; 030010004
  No space
  immediate pack buffer-&gt; 040000004
  Space avail
  wrote 20 out of 20 bytes to the stream
  immediate pack buffer-&gt; 030000003
  Space avail
  wrote 10 out of 10 bytes to the stream
  immediate pack buffer-&gt; 040040008
  Space avail
  wrote 10 out of 10 bytes to the stream
  </code></pre>
  
  <p>Notice how it continually has "No Space" written which means that the method <code>hasSpaceAvailable</code> is returning false and forcing the data to be buffered until it returns true.</p>
  
  <p>1) What I need to know is why is the happening?  Is it waiting for an Ack from the BT hardware? If so how do you removing this blocking?</p>
  
  <p>2) How do you do this so it sends immediately and we basically stream the data in real time without buffering?</p>
  
  <p>3) Is there a hidden API method that will disable this blocking?</p>
  
  <p>This is a real problem because there cannot be any delay/latency in sending the data to the device, it must be sent immediately in order for the hardware to be in sync with the iPhone commands.  Please help.</p>