title: Stream Audio to Bluetooth Device in C#
body: <p>I am trying to stream Audio to a bluetooth device in-code in C#. I've picked up the 32feet.net library to help with this. I am able to get a bluetooth speaker paired just fine, and then I use the code below to connect to the device.</p>
  
  <pre><code>    globalClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(BluetoothConnectedAsyncHandler), device);
  </code></pre>
  
  <p>Async Callback method:</p>
  
  <pre><code>    private void BluetoothConnectedAsyncHandler(IAsyncResult result)
      {
          BluetoothDeviceInfo connectedDevice = (BluetoothDeviceInfo)result.AsyncState;
  
          globalClient.EndConnect(result);
  
          if (result.IsCompleted)
          {
              NetworkStream btStream = globalClient.GetStream();
          }
      }
  </code></pre>
  
  <p>This all works well, but when I try to set the service from BluetoothService.SerialPort to BluetoothService.AudioSource, then I receive a SocketException on the "globalClient.EndConnect(result);" line saying "A socket operation failed because the destination host was down". See screenshot: </p>
  
  <p><a href="https://i.stack.imgur.com/5KQqC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5KQqC.png" alt="Socket Exception"></a></p>
  
  <p>I've also tried to throw data at the speaker through the NetworkStream when it is setup with BluetoothService.SerialPort, but it doesn't play anything - no noise or static.</p>
  
  <p>My running hypothesis is that this can't be done easily with 32feet.net, and I would have to code up the a2dp spec in code. I think the 32feet.net library is used so that I can tell the Operating System to use the speaker as an output device, rather than control audio output in-code as a supported feature. </p>
  
  <p>Please help! Has anyone done this?</p>
  
  <p>Would it even work if I sent an a2dp compliant stream to the device over the BluetoothService.SerialPort connection?</p>
  
  <p>A2DP spec: <a href="https://www.bluetooth.org/docman/handlers/DownloadDoc.ashx?doc_id=8236" rel="nofollow noreferrer">https://www.bluetooth.org/docman/handlers/DownloadDoc.ashx?doc_id=8236</a> </p>
  
  <p>Thanks for any help!</p>