I need to develop an application which communicates with a device via bluetooth low energy. Once the application is connected to the device via bluetooth it receives and sends data by using a gatt service.
The application needs to run on a Windows 10 environment. So far I was able to develop and try the application by using the following UWP classes:
DeviceWatcher
BluetoothLEDevice
Once the device has been discovered and paired the communication starts. The main problem I have is that I am not able to control the connection/disconnection with the device, that is the connection starts automatically once the device has been properly and previously paired, but I haven't found any connect()/disconnect() method so far.
Is there a way to control the connection with a specific bluetooth LE device? Are there other APIs which allow to use the bluetooth without using the UWP framework and which offer more control over the bluetooth?
Bluetooth Low Energy is a power-conserving variant of Bluetooth personal area network (PAN) technology, designed for use by Internet-connected machines and appliances. Also marketed as Bluetooth Smart, Bluetooth LE was introduced in the Bluetooth 4.0 specification as an alternative to Bluetooth Classic.
That LE stands for “low energy,” and the goal of the update is to allow a standard Bluetooth signal to better manage and share wireless audio streams between devices without overworking the batteries in your phone, your smartwatch, or your headphones.
Once the device is paired, whenever it turns on close to the Windows 10 machine, it will try to connect. This is defined behavior in Bluetooth, as the peripheral will always send out a connection request when it is turned on.
There is a DeviceWatcher background task that you can register for to trigger your app when your Bluetooth device connects. You can find some sample code here.
Is there a way to control the connection with a specific bluetooth LE device?
Yes. To initiate a connection: when you create a BluetoothLEDevice
via FromBluetoothAddressAsync
or FromIdAsync
the system will try to initiate a connection to that peripheral, if it does not already hold a connection.
// Connects to a Bluetooth device, given some string deviceId
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(deviceId);
To dispose of a connection, call the close
method on BluetoothLEDevice
. If your app is the only entity with a handle to the peripheral, this will cause the system to disconnect. However, if another app or system service has a handle to the peripheral, the connection will not be closed.
// Will disconnect from the BTLE device, if you hold the only handle
bleDevice.close()
These are taken from the BluetoothLEDevice documentation here.
Are there other APIs?
There are not any other APIs built in to Windows 10 that offer more control over Bluetooth. The UWP APIs offer the most control that Windows 10 currently provides. You could use an alternate Bluetooth stack, but these would have to be installed separately and likely break other Bluetooth behavior on Windows 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With