Bluetooth Audio Devices
Changing the default audio device (the device currently used by the system) on Windows is a tricky task, especially if it is a Bluetooth audio device. Another problem you may need to solve is reconnecting to an already paired Bluetooth audio device. The problem is that those APIs are undocumented and vary across different Windows versions. Fortunately, the Bluetooth Framework has already solved this problem and provides an easy way to do it.
The Bluetooth Framework includes classes that allow you to control audio devices:
- wclAudioSwitcher
The class provides methods to enumerate available audio devices and select the default one. - wclAudioReceiver
The class allows your application to turn a PC into a Bluetooth audio receiver. - wclAudioMeter
The class includes methods for measuring the current audio level and events that notify your application when it changes. - wclAudioVolume
The class allows your application to adjust the audio volume for the selected audio device.
Below you can find more detailed information about those classes and how they help solve the problems described above. Should you have any questions, please do not hesitate to contact us.
AUDIO SWICTHER
The wclAudioSwitcher class allows your application to enumerate available audio devices and select a default audio device for multimedia, communication, and gaming. The default audio device is the one Windows uses to play or record audio. You can select input and output audio devices separately for each role.
To test the wclAudioSwitcher features, you can use the AudioSwitcher sample application from the Bluetooth Framework package. Once it is running, click the Open button. By default, the application displays all active audio devices. But if you need you can also enumerate disabled, unplugged, and not present devices.
An audio device has several properties that describe it. The sample application displays these properties in multiple columns:
- Id - a system ID assigned to a device.
- Friendly name - a device's friendly name.
- Description - a description of the device.
- Flow - column shows how the device is used in the system:
- Render is an output audio device.
- Capture is an input audio device.
- Roles - a set of roles associated with a device:
- 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.
- States - a current device state. A device can be in one of the following state: Active, Disabled, NotPresent, and Unplugged.
- MAC - if the audio device is Bluetooth, its MAC address appears in this column.
- Service - if the audio device is Bluetooth, the connected Bluetooth service UUID is displayed in this column.
Change the Default Audio Device
When you connect a new audio device to your PC, Windows makes it the default one. 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. That is all you need to do.
To see how to do this programmatically in your application, please refer to the AudioSwitcher sample application source code.
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 can perform this operation without using the "Add Device" dialog or unpairing and re-pairing the device to install services. This feature can be tested with the AudioSwitcher sample application. Select the Unplugged checkbox and click the Enum button. The application will then list all installed audio devices that are currently disconnected. Find your device by its MAC address or name in the devices list and select it. Then click the Connect button. The system will connect to the selected device (if it is available) and set it as the system default. You can then change the default audio device without disconnecting it, if needed.
To see how to do this programmatically in your application, please refer to the AudioSwitcher sample application source code.
Disconnect From Bluetooth Audio Device
When your application has finished its work with a Bluetooth audio device, you may need to disconnect it. The common way to do that is to turn your device off or use the Windows "Add Device" dialog, find the device and click the Disconnect button.
The other way to programmatically disconnect a Bluetooth audio device is to unpair it. When the device is unpaired, Windows disconnects from it, removes all associated drivers, and deletes the device from the system entirely.
Neither method is ideal: the first one disconnects the device completely but leaves it in the system, while the second removes the device entirely, requiring you to add it again. But what if your device is two-in-one (microphone and speaker, hands-free) and you only need to disconnect, let's say, the microphone?
The Bluetooth Framework provides a much better solution: methods to disconnect any connected Bluetooth device, or a specific part of it, without unpairing the device or using the Windows Add Device dialog. All of this can be done directly within your application.
If you need to completely disconnect any connected Bluetooth audio device, you can use the very simple method described in this article.
If you need to disconnect only part of the connected Bluetooth audio device, let's say the microphone, you can use a method shown in the AudioSwitcher sample application. To test this method, follow the following steps: select an audio device from the devices list based on the Bluetooth service you want to disconnect from and click the Disconnect button. The selected device's service will be disconnected, but the other one will stay connected and can be used.
To see how to do this programmatically in your application, please refer to the AudioSwitcher sample application source code.
AUDIO RECEIVER
The audio receiver is the Bluetooth Framework feature that allows your application to turn your PC into an audio playback device. This is very helpful if you need to record an audio stream from your cell phone or something like that.
To test this feature, you need to pair with your source device first. Please refer to the BluetoothManager sample application from the Bluetooth Framework package to find out how to pair with a Bluetooth-enabled device. Once the device is paired, enumerate its services, select the A2DP service, and then install it.
When the operation is complete, switch to the AudioReceiver sample application from the Bluetooth Framework package. First, find your just-paired Bluetooth-enabled audio stream source device. To do that, the application uses the wclBluetoothAudioWatcher class. Select that device and start the wclBluetoothAudioReceiver class. The class now waits for a new incoming audio connection. When the remote device starts the audio connection, the OnConnected event is called. When the remote device terminates the 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.
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 terminated the connection from the device's side, you will not be able to initiate a connection from the PC side and have to restart the wclBluetoothAudioReceiver.
To see how to do this programmatically in your application, please refer to the BluetoothManager (find, pair, and install service) and AudioSwitcher sample applications source code.
VOLUME CONTROL
When your application has such great functionality described above, the only thing you may want to add is the audio volume control. 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.
To see how to do this programmatically in your application, please refer to the AudioVolume sample application source code.