蓝牙 LE 设备无法在 Win 10 IoT UWP 应用程序中断开连接 [英] Bluetooth LE device cannot disconnect in Win 10 IoT UWP application

查看:79
本文介绍了蓝牙 LE 设备无法在 Win 10 IoT UWP 应用程序中断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Win 10 IoT UWP C# 应用程序中控制蓝牙 LE 设备的连接/断开连接,该应用程序在 Raspberry Pi 3 SBC 上运行.Win 10 IoT 是 Windows Insiders Preview 版本 10.0.17035.1000.当我启动我的应用程序时,它识别广告 BLE 设备,成功连接并与之通信.然后我尝试通过应用推荐的程序断开此设备的连接:

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.Dispose();

设备 = 空;

GC.Collect();

GC.Collect();

甚至停止并重新启动BluetoothLEAdvertisementWatcher.但是当这个设备再次开始广告时,它就无法识别了.据我了解,原因是该设备实际上并未断开连接(尽管它显示断开连接状态)或某些连接信息仍处于待处理状态,从而无法在新广告中识别它.

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.

即使我不应用上述程序,也无法识别新设备广告(来自同一或其他 BLE 设备),在这两种情况下,唯一的方法是重新启动 Win 10 IoT 并按顺序重新启动应用程序来自同一设备的新广告被识别.与设备的通信完成后,可以识别来自不同设备的同一类型的广告.但是当第二个设备完成通信时,不能再次识别来自它或第一个设备的新广告.在调试模式下没有报告异常或其他问题.这在应用程序生产版本中确实是不可接受的.

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.

推荐答案

此问题可能是由 GattDeviceService 未关闭.

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

当您连接 BLE 设备并访问其服务和特性时,您的操作就会有一个活动会话.您处置了设备,但会话仍处于打开状态.

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. 如果你打开了一个服务(GattDeviceService),你需要调用service?.Dispose().
  2. 如果你打开了一个特性(GattCharacteristic),你需要调用characteristic?.Service?.Dispose().

要检查会话状态,您可以调用 service?.Session.SessionStatuscharacteristic?.Service?.Session.SessionStatus

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

更新:可能有一些特征属于一项服务.因此,当您从特征级别进行处置时,您可能会遇到异常,因为您可能会重复关闭已关闭的服务.要解决此问题,您可以在服务级别进行处置工作.以下代码基于 BluetoothLE 示例.您可以像这样编辑 ClearBluetoothLEDeviceAsync():

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;
        }

这篇关于蓝牙 LE 设备无法在 Win 10 IoT UWP 应用程序中断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆