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 add such feature in your application.
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?
Actually it is very easy thing to do with Bluetooth Framework. 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 that the list includes all connected device: the devices that were connected by system or/and 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 for Bluetooth Classic and Bluetooth Low Energy devices. This method works with any Bluetooth drivers (BlueSoleil, BLED112, Microsoft, Toshiba) and with both Microsoft GATT APIs: legacy and WinRT.
Should you have any questions please do not hesitate to contact us.