The Bluetooth Framework includes classes and components that allow you to control audio devices:
- AudioSwitcher - the class provides methods to switch the system's default audio device and to connect to already paired Bluetooth audio devices without reinstalling services and re-pairing with the device.
- AudioReceiver - the class (together with AudioWatcher) - allows you to switch your PC to a Bluetooth speaker.
- AudioMeter - the class allows you to monitor and to display the current level of the selected audio device.
- AudioVolume - to change the audio level of the selected audio device.
AUDIO SWICTHER
System Default Audio Devices
In Windows 10, there are a number of options that you can use to change sounds for different system events and
configure output and input devices. Modern computers can use classic speakers, Bluetooth headphones, and many
other audio devices, which you can connect to simultaneously. The default audio output device is a device
that Windows is using to play the audio. Other devices can be set to mute or to play the same audio stream.
The Bluetooth Framework provides methods that allow you to control audio devices in your system. The wclAudioSwitcher class allows you to enumerate available audio devices and to select a default audio device for multimedia, communication, and games. You can separately select input and output audio devices for each role.
To test this feature, you can use AudioSwitcher demo application. Once it is started, click the "Open" button. The application (by default) shows all active audio devices (see the picture above).
The "Flow" column shows how the device is used in the system:
- Render is an output audio device.
- Capture is an input audio device.
The "Roles" column shows a device's system role:
- Console: a device is used as default in games;
- Multimedia: a device is used as default in multimedia;
- Communications: a device is used as default in communications.
If a device is a Bluetooth audio device, the "MAC" column will contain a device's MAC address, and the "Service" column will contain a Bluetooth service used by a device. You can also enumerate disabled, unplugged, and not present devices.
To change a system default device, select a device in the devices list, select its default role in the "Role" dropdown box, and click the "Set default" button.
Connect to Already Paired Bluetooth Audio Device
It is almost a common case when your application needs to connect to a Bluetooth audio device. If the device is not paired yet, you can use the wclBluetoothManager class to pair with the device and to install required services. Windows will connect to the just-paired device automatically and make it a default audio device. However, if your device is already paired, then there is no easy way to reconnect to it except using the Windows Add Device dialog. The Bluetooth Framework solves this problem and provides the unique feature to reconnect to the already paired Bluetooth audio device.
With the Bluetooth Framework, your application users do not need to do anything outside your application to reconnect the paired Bluetooth audio device. All can be done by your application. And you do not need to unpair with the device, pair it again, reinstall its services, etc. All you need to do is just check the "Unplugged" checkbox and click the "Enum" button in the AudioSwitcher demo application (assume you already clicked the "Open" button). After that you will see the list of unplugged audio devices. Select the device you need to connect to (by its MAC or name) and click the "Connect" button. The system connects to the selected device (if one is available) and sets it as default. You can change the default audio device without disconnecting it.
Disconnect From Bluetooth Audio Device
Your application may also need to disconnect a connected Bluetooth audio device. There are many reasons why you may need it. The standard way to do that is to turn your device off or use the Windows Add Device dialog, find the device there, and click the Disconnect button. That does not look very useful, and your application users may not be very happy with such a solution.
The other possible way to disconnect a Bluetooth audio device programmatically is to completely unpair it. When the device is unpaired, Windows will disconnect from it, remove all device drivers, and completely remove the device from the system. However, next time you want to use the device, you will need to pair with it again, which can be not so easy: you need to put the device in a pairing mode, etc. It does not sound very user-friendly.
And again, the Bluetooth Framework has a much better solution. It provides methods to disconnect any connected Bluetooth device. If you want to completely disconnect any connected Bluetooth device, including audio devices, you can use this simple method. However, some Bluetooth audio devices may provide few audio services: HandsFree and A2DP, HandsFree and HeadSet, etc. In this case you may need to disconnect just one service you do not want to use. To do that, simply select an audio device from the devices list based on a Bluetooth service you want to disconnect from and click the "Disconnect" button. The required service will be disconnected, but the other one will keep connected and can be used.
AUDIO RECEIVER
The other great and unique feature of the Bluetooth Framework is the ability to switch your PC to a Bluetooth audio speaker. There is the wclBluetoothAudioReceiver class that can do it and that allows you to play any audio stream from, let's say, your cell phone on a PC speaker. This is very helpful if you need to record an audio stream from your cell phone or something like that.
To be able to use the wclBluetoothAudioReceiver, you need to pair your PC with your device (let's say, with a cell phone). You can do it using Windows Add Device or from your application using the Bluetooth Framework for the in-app pairing. In the case of in-app pairing, you will also need to install the A2DP service after the device successfully pairs.
Once the device is paired and the service is installed, you can start listening for incoming audio connections. To do that, you need to find your paired audio device (service). Use the wclBluetoothAudioWatcher class to find the required device ID. Once the device ID is found, you can start the wclBluetoothAudioReceiver. Once started, the audio receiver waits for a new audio connection. When the remote device starts the audio connection, the OnConnected event is called. When a remote device terminates connection, the OnDisconnected event is called.
You can also force an audio connection (initiate it from the PC side) by calling the Connect method of the wclBluetoothAudioReceiver class. It forces connection to a paired Bluetooth audio device.
Please refer to the AudioReceiver demo application from the Bluetooth Framework package to find sample code that shows how to use the wclBluetoothAudioWatcher and the wclBluetoothAudioReceiver classes to enable audio playback from a remote Bluetooth-enabled device through PC speakers.
Currently, there are few limitations: only the A2DP profile is supported; to disconnect a remote device from the PC side, you need to close the wclBluetoothAudioReceiver and then start it again; if the remote device is disconnected from the device's side, you will not be able to initiate a connection from the PC side and have to restart the wclBluetoothAudioReceiver.
VOLUME CONTROL
When your application has such great functionality described above, the only thing you may want to add is
the audio volume control. And, as always, the Bluetooth Framework
provides the easy way to do that. The wclAudioMeter class allows your application to monitor audio levels
on specified audio endpoint devices. You can show the volume to your application end-user. The
wclAudioVolume class allows you to control input and output stream volume on specified audio
endpoint devices. For more details, refer to the AudioVolume demo application.