bluetoothaudio-streaming32feet

Stream Audio to Bluetooth Device in C#


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.

    globalClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(BluetoothConnectedAsyncHandler), device);

Async Callback method:

    private void BluetoothConnectedAsyncHandler(IAsyncResult result)
    {
        BluetoothDeviceInfo connectedDevice = (BluetoothDeviceInfo)result.AsyncState;

        globalClient.EndConnect(result);

        if (result.IsCompleted)
        {
            NetworkStream btStream = globalClient.GetStream();
        }
    }

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:

Socket Exception

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.

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.

Please help! Has anyone done this?

Would it even work if I sent an a2dp compliant stream to the device over the BluetoothService.SerialPort connection?

A2DP spec: https://www.bluetooth.org/docman/handlers/DownloadDoc.ashx?doc_id=8236

Thanks for any help!


Solution

  • Update:

    This isn't possible within the 32feet.net library, you can only set the device up to talk the the Microsoft audio service using the setService method call in 32feet.net

    www.nudoq.org/#!/Packages/32feet.NET/InTheHand.Net.Personal/MicrosoftSdpService/M/SetService

    This MS service manages the A2DP output to the device. There is no way to directly output audio from code into the Bluetooth Device using this library in C#.