Sometimes you may need to disconnect a Bluetooth device that is already connected by the system or by another application to be able to use it in your application. Of course you can ask an application user to do it through the Windows Bluetooth Devices dialog. However, it does not sound too user-friendly; why simply not add such functionality to your application? Probably you did not do it because there is no standard way to disconnect any connected Bluetooth device. However, it is now possible with the Bluetooth Framework. And this is one of the more advantageous reasons to use the Bluetooth Framework instead of the standard Windows Bluetooth API. Before disconnecting a device, you need to find it by enumerating the connected device. If you know your device's MAC address, you can skip this step.
Enumerating Connected Bluetooth Devices
Enumerating connected Bluetooth devices is very easy when you use the Bluetooth Framework. All you need to do is just simply call the EnumConnectedDevices method of the wclBluetoothRadio class. This method returns a list of MAC addresses of all connected Bluetooth devices: Classic and BLE. Note: The list includes all connected devices, including the devices that were connected by the system or by other applications and the devices that were connected by your applications.
GATT-enabled devices note: The Microsoft GATT API does not really connect to the GATT-enabled device until an application reads a characteristic value with the goForceReadFromDevice flag or subscribes to the characteristic value changes notification. Because of that, such devices may not be visible in the connected devices list because actually the devices are not connected yet.
Once you get the connected devices list, you can show the list to an end user or check the required device in the list somehow else. Once a device was selected, you can get its MAC address and disconnect.
Disconnect a Bluetooth Device
You are ready to disconnect the connected Bluetooth device. It is a very simple process; all you need to do is just call the RemoteDisconnect method of the wclBluetoothRadio class. This method forces the device disconnection. This method automatically detects the device's type and initiates the correct disconnection procedure for class Bluetooth and Bluetooth LE devices. This method works with any supported Bluetooth drivers and with any Bluetooth-enabled devices.
The method described above has only one problem that appears when you try to disconnect a Bluetooth audio device. Usually such devices, when paired, install few system devices (one per the device's service). For example, it can be a headset and a speaker, or a microphone and a headset, or something else. When you use the method from above, you completely disconnect all installed devices. But there can be a situation when you need to disconnect only one of them. Let's say you still want to use a Bluetooth microphone but do not need a Bluetooth speaker. To disconnect such a device by its service, use the disconnection method described in this short article.