I am using the C# Windows.Devices.Bluetooth APIs to communicate with a device via BLE.
The BluetoothLEDevice class does not expose connect or disconnect methods but at least the connect can be forced by settings MaintainConnection to true, or by retrieving any GATT service (see here).
For disconnecting, however, there is no direct option. The only option seems to be to
[dispose] of all references to a BluetoothLEDevice object for a device (and if no other app on the system has a reference to the device) [Windows] will trigger an automatic disconnect after a small timeout period. (see here slightly further down)
Unfortunately, this behavior is not sufficient in my case. My connected BLE device requires receiving an explicit disconnect signal from my program when I close the connection.
My question is: does anyone know a reliable workaround, or perhaps a P/Invoke method, to explicitly trigger a disconnect?
From what I’ve read, the Bluetooth stack is managed directly by Windows, so this may not be possible. On the other hand, since Windows can detect when no object is holding a reference to a device and then initiate a disconnect, it seems like there should be a way to request this explicitly.