Sometimes you need to disconnect Bluetooth device (Classic or Low Energy) that is connected and is used by the system or by other application to be able to use it in your application. Of course it can be done through Windows Bluetooth Devices dialog. But why don't add such functional in your application? In this article you will find the way to do that.

Enumerating Connected Bluetooth Devices

Before disconnect Bluetooth device you need to know its MAC address. So the question is: How do I get the MAC address of a device that was not connected by my application? Or even: How do I get know if device is connected?

With Bluetooth Framework you can easy do it. All you have to do is just simple call the EnumConnectedDevices method of the wclBluetoothRadio class. This method returns array (or list in C++ Edition) of MAC addreses of all connected Bluetooth devices: Classic and BLE. Note: the list includes all connected devices including the devices that were connected by system and/or by other applications and the devices that were connected by your applications.

Legacy Microsoft GATT API note: The legacy Microsoft GATT API (that is also used to communicate with DFRobot Bluno boards) does not really connect to device until your read characteristic value with goForceReadFromDevice flag or until you subscribed to its characteristic value changes notification. So such devices may not be visible in the connected list.

Disconnect the Bluetooth Device

Now once you get the device's MAC you can simple disconnect the device. To do that simple 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: Bluetooth Classic or Bluetooth Low Energy. This method works with any Bluetooth drivers (BlueSoleil, BLED112, Microsoft, Toshiba) and with both Microsoft GATT APIs: legacy and WinRT. If your device is a Bluetooth Audio device you can additionally control which service you want to disconnect. For more details about Bluetooth Audio devices disconnect and re-connection refer to this short article.