GattCharacteristic.ValueChanged 不再接到电话

GattCharacteristic.ValueChanged stops getting called

我正在尝试使用 Adafruit Bluefruit LE(蓝牙 4 模块)与 arduino 进行通信,一切都已设置并配对等等,但是我在 GattCharacteristic 上遇到了 ValueChanged 事件,它停止了在 30 到 40 次之间的某处后发射。

下面是设置代码:

public class Bluetooth
{
    async public void Initialize()
    {
        var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")), null);

        GattDeviceService firstService = await GattDeviceService.FromIdAsync(devices[0].Id);

        GattCharacteristic rxCharacteristic = firstService.GetCharacteristics(new Guid("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")).First();

        await rxCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);

        rxCharacteristic.ValueChanged += rxCharacteristicValueChanged;
    }

    private void rxCharacteristicValueChanged(GattCharacteristic characteristic, GattValueChangedEventArgs e)
    {
        Console.WriteLine(e.CharacteristicValue.ToArray()[6]);
    }
}

是否需要清除某种缓冲区或类似的东西?它看起来不像它的缓冲区相关,因为如果我将发送的数据减半,我不会得到两倍的调用,但我可能是错的。 Arduino 报告说它仍在发送数据(通过串行 link,我可以看到蓝牙库仍在尝试发送数据,无论如何。我不确定如何验证数据是否确实是正在发送)

我们将不胜感激任何帮助,甚至是关于检查事项的建议。

根据您的代码,听起来与 GC 相关。

firstService 或其他内容设为字段级实例。

这是范围问题,似乎 GattCharacteristic 对象在未知时间后被处理...

将你的特性设为全局,问题解决。

"You may find that your application stops receiving updates from the device after a period of time. This is a scoping issue where objects are being disposed of that shouldn't. This is because the GattCharacteristic is disposed of before it should be, set the characteristic as a global rather than relying on it being copied in."