C#Windows 10蓝牙LE can';无法连接到服务器

C#Windows 10蓝牙LE can';无法连接到服务器,c#,windows,windows-10,bluetooth-lowenergy,C#,Windows,Windows 10,Bluetooth Lowenergy,我正在开发一个带有表单的c#桌面api,我希望从BLE服务器接收ACC数据并将其显示在图表中。 所以我遇到了一个连接问题,我找不到任何解决方案。 我可以通过watcher找到我的LE服务器设备 DevicePairingResult dpr = await device.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.Encryption); 返回我“AlreadyPaired” 但当我这么做的时候 device

我正在开发一个带有表单的c#桌面api,我希望从BLE服务器接收ACC数据并将其显示在图表中。 所以我遇到了一个连接问题,我找不到任何解决方案。 我可以通过watcher找到我的LE服务器设备

DevicePairingResult dpr = await device.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.Encryption);
返回我“AlreadyPaired”

但当我这么做的时候

device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress: eventArgs.BluetoothAddress);
mGattService = device.GetGattService(MotionService_GUID);
mCharacteristic = mGattService.GetCharacteristics(ACC_Characteristic_GUID)[0];
然后

var con = device.ConnectionStatus;
我收到con中的“断开连接”

我与windows上的de设备绑定(我在windows中搜索并输入代码),但我未连接(根据windows信息中心中的状态)。 我在WindowsC#开发者页面的另一个帖子中读到,应该不再需要手动配对设备了

我对舒尔非常满意,因为我的其他代码都能正常工作,因为有时我可以获得连接(对我来说相当混乱),并在图表中看到正确的数据。 现在,我只想在更改代码的其他部分之前建立一个稳定的连接。 有人知道怎么解决这个问题吗? Thx医疗技术公司

编辑: 以下是代码的一部分:

扫描BLE

private void button1_Click(object sender, EventArgs e)
        {

            // Create Bluetooth Listener
            var watcher = new BluetoothLEAdvertisementWatcher();

            watcher.ScanningMode = BluetoothLEScanningMode.Active;

            // Register callback for when we see an advertisements
            watcher.Received += OnAdvertisementReceivedAsync;

            // Wait 5 seconds to make sure the device is really out of range
            watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(5000);
            watcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(2000);

            // Starting watching for advertisements
            watcher.Start();
        }
连接到服务器:

private async void OnAdvertisementReceivedAsync(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {

            // Filter for specific Device
            if (eventArgs.Advertisement.LocalName == "MYDEVICE")
            {
                watcher.Stop();

                var MotionService_GUID = new Guid("00002000-0000-1000-8000-00805F9B34FB");
                var ACC_Characteristic_GUID = new Guid("00002001-0000-1000-8000-00805F9B34FB");


                    device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress: eventArgs.BluetoothAddress);
                    DevicePairingResult dpr = await device.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.Encryption);
                    mGattService = device.GetGattService(MotionService_GUID);


                    mCharacteristic = mGattService.GetCharacteristics(ACC_Characteristic_GUID)[0];

                    GattDeviceServicesResult result = await device.GetGattServicesAsync();


                    GattCommunicationStatus status1 = await ReadFromCharacteristicAsync(mCharacteristic);

                    var con = device.ConnectionStatus;
                    while (status1 == GattCommunicationStatus.Success)
                    {
                        try
                        {
                            status1 = await ReadFromCharacteristicAsync(mCharacteristic);
                        }
                        catch
                        {
                            Console.WriteLine("ERROR");
                            status1 = GattCommunicationStatus.Unreachable;

                        }
                    }

            }
            }
从特征上看:

异步任务ReadFromCharacteristicAsync(GattCharacteristic McCharacteristic) {

        GattReadResult readResult = await mCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);

        if (readResult.Status == GattCommunicationStatus.Success)
        {
            byte[] data = new byte[readResult.Value.Length];
            DataReader.FromBuffer(readResult.Value).ReadBytes(data);
            if (chart1.IsHandleCreated)
            {
                this.Invoke((MethodInvoker)delegate { updateChart(data); });
            }

            return readResult.Status;
        }
        return readResult.Status;
    }
终止连接

 private async Task<bool> ClearBluetoothLEDeviceAsync()
        {

            mCharacteristic.Service.Dispose();
            mGattService.Dispose();
            await device.DeviceInformation.Pairing.UnpairAsync();
            device?.Dispose();
            device = null;
            GC.Collect();
            return true;
        }
专用异步任务ClearBluetoothLEDeviceAsync()
{
mCharacteristic.Service.Dispose();
mGattService.Dispose();
等待设备.DeviceInformation.Pairing.UnpairAsync();
设备?.Dispose();
设备=空;
GC.Collect();
返回true;
}
因此,现在当我第一次连接到服务器时,我只收到零,这表明可能存在身份验证错误。 之后,我总是收到此错误: mscorlib.dll中的“System.ArgumentException”,并通知由于所有线程都在执行异步操作,因此没有剩余的可执行代码。 当我尝试读取特征时,会抛出此错误。 我以前从未使用过c#编码,因此如果我的异步部分或通信部分出现错误,我就不是舒尔。
谢谢你

配对与连接不一样! 我真的建议使用BLE advertisementWatcher选择并连接到您的设备。 原因是许多BLE设备不保存其配对状态。 在windows device watcher中,配对后,即使设备已关闭或无法触及,也会保持配对状态。 也有很多次,连接状态保持不变,除非设备未配对、以代码处理或在windows设置中删除

我所知道的所有BLE设备在一段时间没有连接时就开始做广告。 此时间取决于设备,但大多数时间在几秒钟内


因此,如果设备正在播放广告,不要配对,只需连接即可。

您确定设备需要配对吗?许多可绑定设备不需要您配对。嗨,Manfred,我也遇到了这个问题,但没有配对。我使用给定的代码手动将设备与windows 10解除绑定并绑定,并且我的连接正常。但即使我“断开连接”作为Connectionstatus。这让我非常困惑,因为根据我的经验,没有设备之间的握手,也没有连接,我会收到正确数量的数据,但只有零作为值。所以我认为连接状态是一个参数,我可以确认我的连接是否成功。嘿,GrooverFromHooland,谢谢你您的答案。我正在使用一个广告观察者
var-watcher=new BluetoothLEAdvertisementWatcher();
我可以随时找到我的设备,如果我没有手动将其连接到windows。在连接结束时,我会执行`mCharacteristic.Service.Dispose();mGattService.Dispose();device?.Dispose();device=null;GC.Collect();`完全删除ble堆栈。但是在第一次连接之后,我要么只收到零作为值,要么我的应用程序停止,因为in-Readoperation没有可执行代码。如果我仔细阅读您的代码,会有很多疑问,但最令人担心的是使用while(status1==GattCommunicationStatus.Success)。这意味着反复阅读。您必须等待结果。请改用if。