When developing an application that communicates with Bluetooth-enabled devices — Classic, LE, or audio — a common need arises: check whether a device is already connected by another application or by the system, and disconnect it so the application can use it.
The typical workaround is to ask the user to open the Windows Devices dialog, find the device — not always easy when it has a strange name — and disconnect it manually. The reason for this workaround is that Windows does not provide any API to disconnect a connected Bluetooth-enabled device. If the device is paired, it can be disconnected by unpairing, but that removes the device from the system and requires a new pairing the next time it is needed. That is not a good solution.
With the Bluetooth Framework, the same operation is done programmatically. The
BluetoothManager sample application demonstrates both steps.
Enumerating connected devices
In the BluetoothManager sample application, clicking Enum connected lists all currently connected devices. Even though
connected devices also appear in the Enum paired list, not all connected devices are paired. Additionally, if a UWP application that
uses a GATT-enabled device is running, that device may not appear because of UWP GATT API limitations. The
Bluetooth Framework does not have such limitations, and any BLE device — unless it is specifically
restricted — will be properly reported.
To enumerate connected Bluetooth-enabled devices in an application, use the EnumConnectedDevices method of the
wclBluetoothRadio class. It returns the list of currently connected devices.
Disconnecting a device
Once the connected devices list is available, select the required device and disconnect it. In the sample application, this is done by selecting the device in the list and clicking Disconnect. The device is disconnected, and, if it was paired, it remains paired.
In an application, use the RemoteDisconnect method of the wclBluetoothRadio class. It takes the target
device's MAC address and disconnects the device. The Bluetooth Framework can disconnect any
Bluetooth-enabled device type: Classic, LE, and audio. Using this method with a Bluetooth audio device may cause some issues, though.
More details on working with Bluetooth audio devices are available in the Bluetooth audio article.
Frequently asked questions
- How to disconnect a connected Bluetooth device on Windows?
- Windows does not provide a native API for disconnecting connected Bluetooth devices. The
Bluetooth Framework adds this through the
RemoteDisconnectmethod of thewclBluetoothRadioclass. - Can I disconnect a Bluetooth device without unpairing it?
- Yes. The Bluetooth Framework allows disconnecting any Bluetooth device while keeping it paired, so it can be reconnected later without going through the pairing process again.
- How to find out which Bluetooth devices are currently connected?
- Use the
EnumConnectedDevicesmethod ofwclBluetoothRadioto get a list of all connected Bluetooth devices.