Sometimes you need to disconnect Bluetooth device (Classic or Low Energy) that is already connected 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 a MAC address of a device that was not connected by my application? Or even: How can I know if device is connected?

You can easy do that with Bluetooth Framework. All you need to do is just simple call the EnumConnectedDevices method of the wclBluetoothRadio class. This method returns list 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 have 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.