Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth LE device cannot disconnect in Win 10 IoT UWP application

I am trying to control the Bluetooth LE devices connect/disconnect in my Win 10 IoT UWP C# application, running on Raspberry Pi 3 SBC. The Win 10 IoT is Windows Insiders Preview build 10.0.17035.1000. When I start my application, it recognizes the advertising BLE device, successfully connects and communicates with it . Then I try to disconnect this device by applying the recommended procedure:

device.Dispose();

device = null;

GC.Collect();

and even stop and start again the BluetoothLEAdvertisementWatcher. But when this device starts advertising again, it is not recognized. Of my understanding the reason is that the device was not actually disconnected (despite that it shows disconnected status) or some connection information is still pending, which prevents it to be recognized on a new advertisement.

Even if I don't apply the above procedure, a new device advertisement (from the same or other BLE device) is not recognized and in both cases the only resort is to restart the Win 10 IoT and restart the application in order a new advertisement from the same device to be recognized. After the communication with the device completes, an advertisement from different device of the same type can be recognized. But when the second device completes to communicate, no new advertisement from it or the first device can be recognized again. No exceptions or other problems were reported in debug mode. This is really inacceptable in an application production version.

Please advise me how to solve this problem. Thanks.

like image 962
Paul Tomov Avatar asked Dec 07 '17 19:12

Paul Tomov


1 Answers

This issue may be caused by the active session of GattDeviceService not been closed.

When you connected a BLE device and access its services and characteristics, there is an active session for your operations. You dispose of the device but the session is still open.

To solve this problem you need do a little more work when disconnecting the device like this:

  1. If you opened a service(GattDeviceService) you need call service?.Dispose().
  2. If you opened a characteristic(GattCharacteristic) you need to call characteristic?.Service?.Dispose().

For checking the session status you can call service?.Session.SessionStatus or characteristic?.Service?.Session.SessionStatus

Update: There maybe serval characteristics belong to one service. So you may run into exception when do the dispose from the characteristic level because you may duplicate to close a closed service. To solve this problem you can do the dispose work at the service level. The following code piece based on BluetoothLE sample. You can edit ClearBluetoothLEDeviceAsync() like this:

        private async Task<bool> ClearBluetoothLEDeviceAsync()
        {
            ...

            foreach (var ser in ServiceCollection)
            {
                ser.service?.Dispose();
            }

            bluetoothLeDevice?.Dispose();
            bluetoothLeDevice = null;
            return true;
        }
like image 113
Rita Han Avatar answered Nov 18 '22 08:11

Rita Han