how to disconnect bluetooth device, GATT service & GATT characteristic

Maurizio Bonelli 21 Reputation points
2022-10-12T13:55:35.83+00:00

Hello,

I have developed an application in .NET Framework 4.8 using some code examples with the Windows.Device.BluetoothLE, which successfully connect, discover services, open one of them for Characteristics then register for Write and Notification....

The problems arrive when I have to disconnect to re-open the same or another similar device (I use the BT connection to calibrate and save parameters of a family of devices during manufacturing....):

  • I have declared IDisposable the Class BleCore
  • I have implemented the Dispose method described at IDisposable on .NET documentation

the Dispose method is the following

        // Public implementation of Dispose pattern callable by consumers.  
        public void Dispose()  
        {  
            Dispose(true);  
            GC.SuppressFinalize(this);  
        }  
  
        // Protected implementation of Dispose pattern.  
        protected virtual void Dispose(bool disposing)  
        {  
            if (!_disposedValue)  
            {  
                if (disposing)  
                {  
                    // TODO: dispose managed state (managed objects)  
                    CurrentDeviceMAC = null;  
                    CurrentWriteCharacteristic?.Service.Dispose();  
                    //CurrentNotifyCharacteristic?.Service.Dispose();  
                    CurrentService?.Dispose();  
                    CurrentDevice?.Dispose();  
                    CurrentNotifyCharacteristic = null;  
                    CurrentWriteCharacteristic = null;  
                    CurrentService = null;  
                    CurrentDevice = null;  
                    MessAgeChanged(MsgType.NotifyTxt, "Actively disconnect");  
                }  
  
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer  
                // TODO: set large fields to null  
                GC.WaitForFullGCComplete();  
                _disposedValue = true;  
            }  
        }  
  

Now, after the 1st connection, the disconnection attempt work, but from the 2nd and subsequent, disconnection doesn't happen or need long time, even minutes.

Another anomalie is the at the 2 disconnection, the event CurrentDevice_ConnectionStatusChanged is triggered twice, at the 3d it is triggered 6 times .... and the same for connection.

What am I doing wrong?

Thanks

Maurizio

Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2022-10-14T09:36:27.047+00:00

    Hi,

    Thank you for posting your query.

    Kindly follow the steps provided below to resolve your issue.

    This article demonstrates usage of the Bluetooth Generic Attribute (GATT) Client APIs for Universal Windows Platform (UWP) apps, along with sample code for common GATT client tasks:

    Query for nearby devices
    Connect to device
    Enumerate the supported services and characteristics of the device
    Read and write to a characteristic
    Subscribe for notifications when characteristic value changes
    Important

    You must declare the "bluetooth" capability in Package.appxmanifest.

    <Capabilities> <DeviceCapability Name="bluetooth" /> </Capabilities>

    Important APIs

    Windows.Devices.Bluetooth
    Windows.Devices.Bluetooth.GenericAttributeProfile

    Go to this link for your reference https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client

    ------------------------------------------------------------------------------------------------------------------------------------------

    If the answer is helpful kindly click "Accept as Answer" and upvote it. Thanks.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.