Search code examples
c#bluetooth-lowenergygatt

GattCharacteristic.ValueChanged stops getting called


I am attempting communication with an arduino using an Adafruit Bluefruit LE (a bluetooth 4 module), everything is set up and paired and all that, but I'm having trouble with the ValueChanged event on my GattCharacteristic, it stops firing after somewhere between 30 and 40 times.

Below is the setup code for this:

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

Is there some kind of buffer I need to clear or something like that? It doesnt look like its buffer related, because If I halve the data being sent, I dont get twice the calls, but I could be wrong. The Arduino reports that it is still sending data (through a serial link, I can see that the bluetooth library is still attempting to send data, at any rate. I am not sure how I would verify that the data is actually getting sent)

Any help would be appreciated, even suggestions on things to check.


Solution

  • Sounds GC related given your code.

    Make firstService or something else a field-level instance.