Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows BLE UWP disconnect

How one forces Windows to disconnect from BLE device being used in UWP app? I receive notifications from some characteristics but at some point I want to stop receiving them and make sure I disconnect from the BLE device to save BLE device's battery?

like image 260
Martin Dusek Avatar asked Sep 20 '16 16:09

Martin Dusek


1 Answers

Assuming your application is running as a gatt client and you have the following instances your are working with in your code:

GattCharacteristic myGattchar; // The gatt characteristic you are reading or writing on your BLE peripheral 
GattDeviceService myGattServ; // The BLE peripheral' gatt service on which you are connecting from your application  
BluetoothLEDevice myBleDev;  // The BLE peripheral device your are connecting to from your application

When you are already connected to your BLE peripheral, if you call the Dispose() methods like this :

myBleDev.Dispose(); and/or myGattServ.Dispose(); and/or myGattchar.Service.Dispose()

you surely will free resources in your app but will not cleanly close the BLE connection: The application looses access to control resources for the connection. Nevertheless, connection remains established on the lower levels of the stack (On my peripheral device the Bluetooth connection active LED remains ON after calling any of Dispose() methods).

Forcing disconnection is done by first disabling notifications and indications on the concerned characteristic (i.e. myGattchar in my example above) by writing a 0 (zero) to the Client Characteristic Configuration descriptor for that characteristic through call to method WriteClientCharacteristicConfigurationDescriptorAsync with parameter GattClientCharacteristicConfigurationDescriptorValue.None :

GattCommunicationStatus status = 
await myGattchar.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.None);
like image 139
filotn Avatar answered Oct 23 '22 04:10

filotn