C# Windows IoT核心上的蓝牙连接不稳定-即使使用外部加密狗

C# Windows IoT核心上的蓝牙连接不稳定-即使使用外部加密狗,c#,uwp,bluetooth-lowenergy,windows-10-iot-core,C#,Uwp,Bluetooth Lowenergy,Windows 10 Iot Core,我使用运行最新稳定Windows IoT核心版本(v.10.0.17763.737)的RaspberryPis(v3)检索数据并控制蓝牙恒温器。我为蓝牙连接的稳定性挣扎了很长时间。几个月前,我将所有节点都切换到外部加密狗(microsoft兼容性列表中的一个:ORICO BTA-403),这改善了情况,但距离稳定还有很长的路要走 一段时间后(从几分钟到几天不等),蓝牙连接停止工作。我已经实现了一个powershell脚本来检查这种情况,并发出“devcon restart USB”命令。这有帮助

我使用运行最新稳定Windows IoT核心版本(v.10.0.17763.737)的RaspberryPis(v3)检索数据并控制蓝牙恒温器。我为蓝牙连接的稳定性挣扎了很长时间。几个月前,我将所有节点都切换到外部加密狗(microsoft兼容性列表中的一个:ORICO BTA-403),这改善了情况,但距离稳定还有很长的路要走

一段时间后(从几分钟到几天不等),蓝牙连接停止工作。我已经实现了一个powershell脚本来检查这种情况,并发出“devcon restart USB”命令。这有帮助,但仅在所有故障中的50%左右。如果它不能解决这种情况,我必须重新启动节点。然而,这并不令人满意,因为在启动时节点会不时冻结,我的经验是,重新启动过多会缩短micro sd卡的使用寿命

经过几个小时的分析,我发现蓝牙支持服务可能是问题所在。在连接崩溃之前,我可以使用powershell轻松地重新启动蓝牙支持服务。问题发生后,这是不可能的。服务重新启动挂起,服务状态表示它正在等待重新启动或类似的操作

你们有过同样的经历吗?还是我的蓝牙连接实施得太糟糕了?我附加了功能供您查看:

private async Task Connect()
    {
        try
        {
            int count = 0;
            do
            {
                count++;
                if (count > 10)
                {
                    m_IsInitialized = false;
                    //throw new Exception("More than 10 tries have not been successfull for connecting with thermostat module. Stopping.");
                }
                m_IsInitialized = false;

                m_Device = await BluetoothLEDevice.FromIdAsync(m_DeviceID);
                if (m_Device == null)
                {
                    throw new Exception("Device was not found.");
                }

                m_Characteristics = new Dictionary<ushort, GattCharacteristic>();
                GattDeviceServicesResult gattServices = await m_Device.GetGattServicesAsync();
                foreach (GattDeviceService service in gattServices.Services)
                {
                    try
                    {
                        GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsAsync();
                        IReadOnlyList<GattCharacteristic> characteristics = characteristicsResult.Characteristics;
                        foreach (GattCharacteristic characteristic in characteristics)
                        {
                            try
                            {
                                m_Characteristics.Add(characteristic.AttributeHandle, characteristic);
                                GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
                                if (properties.HasFlag(GattCharacteristicProperties.Notify))
                                {
                                    try
                                    {
                                        GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                                        if (status == GattCommunicationStatus.Success)
                                        {
                                            characteristic.ValueChanged += Characteristic_ValueChanged;
                                            m_IsInitialized = true;
                                            //Logger.ServiceLog(string.Format("Thermostat has been initialized successfully ({0} tries).", count));
                                            return;
                                        }
                                    }
                                    catch (Exception ex4)
                                    {
                                        throw new Exception("4: GattCommunicationStatus: " + ex4.Message + "\nStackTrace: " + ex4.StackTrace);
                                    }
                                }
                            }
                            catch (Exception ex3)
                            {
                                throw new Exception("3: GattCharacteristic: " + ex3.Message + "\nStackTrace: " + ex3.StackTrace);
                            }
                        }
                    }
                    catch (Exception ex2)
                    {
                        throw new Exception("2: GattDeviceService: " + ex2.Message + "\nStackTrace: " + ex2.StackTrace);
                    }
                }
                //Logger.ServiceLog(string.Format("Thermostat:Connect: Unsuccessful try: {0}", count));
                await Task.Delay(1 * 60 * 1000);
            }
            while (!m_IsInitialized);
        }
        catch (Exception ex)
        {
            Logger.ServiceLog("1: Thermostat.cs Connect", ex);
        }
    }
专用异步任务连接()
{
尝试
{
整数计数=0;
做
{
计数++;
如果(计数>10)
{
m_初始=错误;
//抛出新异常(“连接节温器模块的尝试未成功超过10次。正在停止”);
}
m_初始=错误;
m_设备=等待来自IDASync的蓝牙设备(m_设备ID);
如果(m_设备==null)
{
抛出新异常(“未找到设备”);
}
m_Characteristics=新字典();
GattDeviceServicesResult gattServices=等待m_设备。GetGattServicesAsync();
foreach(gattServices.Services中的GattDeviceService服务)
{
尝试
{
GattCharacteristicsResult characteristicsResult=等待服务。GetCharacteristicsAsync();
IReadOnlyList特性=特性结果特性;
foreach(特征中的GattCharacteristic特征)
{
尝试
{
m_Characteristics.Add(characteristic.AttributeHandle,characteristic);
GattCharacteristicProperties=CharacteristicProperties.CharacteristicProperties;
if(properties.HasFlag(GattCharacteristicProperties.Notify))
{
尝试
{
GattCommunicationStatus=wait characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
if(status==GattCommunicationStatus.Success)
{
characteristic.ValueChanged+=characteristic\u ValueChanged;
m_i初始=真;
//ServiceLog(string.Format(“恒温器已成功初始化({0})。”,count));
返回;
}
}
捕获(异常ex4)
{
抛出新异常(“4:GattCommunicationStatus:+ex4.Message+”\nStackTrace:+ex4.StackTrace);
}
}
}
捕获(异常ex3)
{
抛出新异常(“3:GattCharacteristic:+ex3.Message+”\nStackTrace:+ex3.StackTrace);
}
}
}
捕获(异常ex2)
{
抛出新异常(“2:GattDeviceService:+ex2.Message+”\nStackTrace:+ex2.StackTrace”);
}
}
//ServiceLog(string.Format(“恒温器:连接:不成功的尝试:{0}”,计数));
等待任务延迟(1*60*1000);
}
而(!m_已初始化);
}
捕获(例外情况除外)
{
Logger.ServiceLog(“1:automator.cs Connect”,ex);
}
}
如您所见,代码中充满了调试帮助,但当连接崩溃时,根本不会引发异常。我真的很期待任何帮助,因为经过几个月的努力,我真的更愿意得到一个稳定的解决方案,没有任何黑客或变通办法


感谢您的帮助:-)uwp 2)稳定性,选择一个!我理解你的意思,迈赫扎德,但我认为这一定是可能的。我想我发现了蓝牙控制服务(bthserv)的问题,但我不知道如何让它恢复运行或在不重新启动整个节点的情况下成功重启它…问题不在服务中。蓝牙框架在RPi上运行,无需任何p