Why is the BLE device not disconnecting?

Viewed 140

I am able to pair a Bluetooth skincare device to my PC using a C# app that I wrote.

Definition:

        //Device Data
        BluetoothLEDevice bluetoothLeDevice;
        DeviceData deviceData=new DeviceData();
        GattDeviceServicesResult result;
        static DeviceInformation device = null;

The handlers that reads devices, implemented in MainForm():

// Query for extra properties you want returned
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };

            DeviceWatcher deviceWatcher =
                        DeviceInformation.CreateWatcher(
                                BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                                requestedProperties,
                                DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            // Added, Updated and Removed are required to get all nearby devices
            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;

            // EnumerationCompleted and Stopped are optional to implement.
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;

            // Start the watcher.
            deviceWatcher.Start();

The code that pairs the device with a click of a button:

private async void btnPair_Click(object sender, EventArgs e)
        {
            feedbackTB.AppendText("Waiting for pairing..." + Environment.NewLine);
            if (device == null)
            {
                statusBox.Text = "Please, make sure the device is on, and try again. Uncover the device and it will automatically turn on.";
                feedbackTB.AppendText("Timed out." + Environment.NewLine);
                Thread.Sleep(200);
            }
            else
            {
                try
                {
                    hydrationFqList = new List<int>();
                    psList = new List<int>();
                    bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
                    statusBox.Text = "Attempting to pair with device";
                    result = await bluetoothLeDevice.GetGattServicesAsync();

                    if (result.Status == GattCommunicationStatus.Success)
                    {
                        statusBox.Text = "Pairing succeeded";
                        feedbackTB.AppendText("Device paired." + Environment.NewLine);
                        var services = result.Services;
                        foreach (var service in services)
                        {
                            if (service.Uuid.ToString() == deviceData.devInfoSvc)
                            {
                                GattCharacteristicsResult charactiristicResult = await service.GetCharacteristicsAsync();
                                feedbackTB.AppendText("Skimming device info service... " + Environment.NewLine);
                                if (charactiristicResult.Status == GattCommunicationStatus.Success)
                                {
                                    feedbackTB.AppendText("Reading device info... " + Environment.NewLine);
                                    var characteristics = charactiristicResult.Characteristics;
                                    foreach (var characteristic in characteristics)
                                    {
                                        
                                        GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
                                        if (properties.HasFlag(GattCharacteristicProperties.Read))
                                        {
                                            
                                            GattReadResult rResult = await characteristic.ReadValueAsync();
                                            var reader = DataReader.FromBuffer(rResult.Value);
                                            byte[] input = new byte[reader.UnconsumedBufferLength];
                                            reader.ReadBytes(input);
                                            string data = Encoding.ASCII.GetString(input);

                                            if (characteristic.Uuid.ToString() == deviceData.mfgNameCt)
                                            {
                                                mfgNameTB.Text = data;
                                            }
                                            if (characteristic.Uuid.ToString() == deviceData.modelNumCt)
                                            {
                                                modelNumTB.Text = data;
                                            }
                                            if (characteristic.Uuid.ToString() == deviceData.serialNumCt)
                                            {
                                                serialNumber = data;
                                                snTB.Text = serialNumber;
                                            }
                                            if (characteristic.Uuid.ToString() == deviceData.macAddrCt)
                                            {
                                                
                                                macAddress = data;
                                                macAddrTB.Text = macAddress;
                                            }
                                            if (characteristic.Uuid.ToString() == deviceData.hwRevCt)
                                            {
                                                hwRevisionTB.Text = data;
                                            }
                                            if (characteristic.Uuid.ToString() == deviceData.fwRevCt)
                                            {
                                                fwRevisionTB.Text = data;
                                            }
                                        }
                                        statusBox.Text = "Press and hold device against your skin to start scan.";
                                    }
                                }
                            }
                        }
                        
                    }
                    else
                    {
                        statusBox.Text = "Please, make sure the device is connected, and try again.";
                        feedbackTB.AppendText("Timed out." + Environment.NewLine);
                    }
                    feedbackTB.AppendText("Number of run(s):" + increment + Environment.NewLine);
                     
                }
                catch (Exception err)
                {
                    ErrorWindow eWindow = new ErrorWindow();
                    eWindow.label4.Text = err.GetType().ToString();
                    eWindow.errorDetails.Text = err.Message;
                    eWindow.ShowDialog();
                }
            }
        }

And this is my newly implemented disconnect button:

        private void btnDisconnect _Click(object sender, EventArgs e)
        {
            bluetoothLeDevice.Dispose();
        }

Now we get to the issue.

When the device is on, a blue light is suppose to flash. When the device is paired to a PC or app, the blue light will stay on.

I expect the device to disconnect when the button is clicked. Upon the click, the light will start flashing again when it's just turned on.

But upon clicking on the button, nothing happened. The device is still stuck connected.

How can I fix this?

0 Answers
Related